Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.00% |
24 / 25 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RequestContextExtender | |
96.00% |
24 / 25 |
|
66.67% |
2 / 3 |
12 | |
0.00% |
0 / 1 |
| withServerData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| extend | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getContextRequestData | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
8.02 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\log\logger\extender; |
| 24 | |
| 25 | class RequestContextExtender implements ContextExtenderInterface |
| 26 | { |
| 27 | /** @var array */ |
| 28 | private $requestData = []; |
| 29 | |
| 30 | /** @var array */ |
| 31 | private $serverData; |
| 32 | |
| 33 | public function withServerData(array $serverData): self |
| 34 | { |
| 35 | $this->serverData = $serverData; |
| 36 | |
| 37 | return $this; |
| 38 | } |
| 39 | |
| 40 | public function extend(array $context): array |
| 41 | { |
| 42 | if (isset($context[self::CONTEXT_REQUEST_DATA]) && is_array($context[self::CONTEXT_REQUEST_DATA])) { |
| 43 | $context[self::CONTEXT_REQUEST_DATA] = array_merge( |
| 44 | $context[self::CONTEXT_REQUEST_DATA], |
| 45 | $this->getContextRequestData() |
| 46 | ); |
| 47 | |
| 48 | return $context; |
| 49 | } |
| 50 | |
| 51 | $context[self::CONTEXT_REQUEST_DATA] = $this->getContextRequestData(); |
| 52 | |
| 53 | return $context; |
| 54 | } |
| 55 | |
| 56 | private function getContextRequestData(): array |
| 57 | { |
| 58 | if (!empty($this->requestData)) { |
| 59 | return $this->requestData; |
| 60 | } |
| 61 | |
| 62 | $serverData = $this->serverData ?? $_SERVER; |
| 63 | |
| 64 | $this->requestData = []; |
| 65 | |
| 66 | if (isset($serverData['SERVER_ADDR'])) { |
| 67 | $this->requestData['serverIp'] = $serverData['SERVER_ADDR']; |
| 68 | } |
| 69 | |
| 70 | if (isset($serverData['SERVER_NAME'])) { |
| 71 | $this->requestData['serverName'] = $serverData['SERVER_NAME']; |
| 72 | } |
| 73 | |
| 74 | if (isset($serverData['REQUEST_URI'])) { |
| 75 | $this->requestData['requestUri'] = substr($serverData['REQUEST_URI'], 0, 500); |
| 76 | } |
| 77 | |
| 78 | if (isset($serverData['REQUEST_METHOD'])) { |
| 79 | $this->requestData['requestMethod'] = $serverData['REQUEST_METHOD']; |
| 80 | } |
| 81 | |
| 82 | if (isset($serverData['argv']) && !isset($this->requestData['requestUri'])) { |
| 83 | $this->requestData['argv'] = $serverData['argv']; |
| 84 | } |
| 85 | |
| 86 | return $this->requestData; |
| 87 | } |
| 88 | } |