Skip to content

Commit bbf2f95

Browse files
committed
refactor: WebProcessor by extracting context building into a separate method
- Moved the context construction logic from the process() method into a new private method buildContext(). - This enhances code readability and adheres to the Single Responsibility Principle (SRP) by separating the concerns of processing and context building. - Improves maintainability by centralizing the context logic, making future modifications easier.
1 parent 00f2289 commit bbf2f95

File tree

8 files changed

+84
-24
lines changed

8 files changed

+84
-24
lines changed

src/Processor/EncryptionProcessor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function process(ImmutableValue $record): ImmutableValue
1919
{
2020
$encryptedMessage = $this->encryptor->encrypt($record->message);
2121

22-
return new LogRecord($record->level, $encryptedMessage, $record->context, $record->datetime);
22+
return new LogRecord(
23+
$record->level,
24+
$encryptedMessage,
25+
$record->context,
26+
$record->datetime
27+
);
2328
}
2429
}

src/Processor/Metric/CpuUsageProcessor.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
class CpuUsageProcessor extends AbstractProcessor
1212
{
13-
/**
14-
* @param LogRecord $record
15-
*/
1613
public function process(ImmutableValue $record): ImmutableValue
1714
{
1815
$cpuUsage = sys_getloadavg()[0];

src/Processor/Metric/ExecutionTimeProcessor.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ public function __construct(private float $threshold = 1000)
1414
{
1515
}
1616

17-
/**falta cibnfi
18-
* @param LogRecord $record
19-
*/
2017
public function process(ImmutableValue $record): ImmutableValue
2118
{
2219
$executionTime = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000; // Convert to milliseconds

src/Processor/Metric/MemoryUsageProcessor.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
class MemoryUsageProcessor extends AbstractProcessor
1010
{
11-
/**
12-
* @param LogRecord $record
13-
*/
1411
public function process(ImmutableValue $record): ImmutableValue
1512
{
1613
$memoryUsage = memory_get_usage(true);

src/Processor/MetricsProcessor.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010

1111
class MetricsProcessor implements LogProcessor
1212
{
13-
/** @var LogProcessor[] */
1413
private array $processors;
1514

1615
public function __construct(array $processors)
1716
{
1817
$this->processors = $processors;
1918
}
2019

21-
/**
22-
* @param LogRecord $record
23-
*/
2420
public function process(ImmutableValue $record): ImmutableValue
2521
{
2622
foreach ($this->processors as $processor) {

src/Processor/WebProcessor.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44

55
use KaririCode\Contract\ImmutableValue;
66
use KaririCode\Logging\LogRecord;
7+
use KaririCode\Logging\Util\Http\Contract\HttpRequest;
8+
use KaririCode\Logging\Util\Http\ServerHttpRequest;
79

810
class WebProcessor extends AbstractProcessor
911
{
12+
public function __construct(
13+
private HttpRequest $request = new ServerHttpRequest()
14+
) {
15+
}
16+
1017
public function process(ImmutableValue $record): ImmutableValue
1118
{
12-
$server = $_SERVER;
13-
$context = array_merge($record->context, [
14-
'url' => ($server['HTTPS'] ?? 'off') === 'on' ? 'https://' : 'http://' .
15-
($server['HTTP_HOST'] ?? 'localhost') .
16-
($server['REQUEST_URI'] ?? '/'),
17-
'ip' => $server['REMOTE_ADDR'] ?? null,
18-
'http_method' => $server['REQUEST_METHOD'] ?? null,
19-
'server' => $server['SERVER_NAME'] ?? null,
20-
'referrer' => $server['HTTP_REFERER'] ?? null,
21-
]);
19+
$context = $this->buildContext($record->context);
2220

2321
return new LogRecord(
2422
$record->level,
@@ -28,4 +26,15 @@ public function process(ImmutableValue $record): ImmutableValue
2826
$record->extra
2927
);
3028
}
29+
30+
private function buildContext(array $existingContext): array
31+
{
32+
return array_merge($existingContext, [
33+
'url' => $this->request->getUrl(),
34+
'ip' => $this->request->getIp(),
35+
'http_method' => $this->request->getMethod(),
36+
'server' => $this->request->getServerName(),
37+
'referrer' => $this->request->getReferrer(),
38+
]);
39+
}
3140
}

src/Util/Http/HttpRequest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Logging\Util\Http\Contract;
6+
7+
interface HttpRequest
8+
{
9+
public function getUrl(): string;
10+
public function getIp(): ?string;
11+
public function getMethod(): string;
12+
public function getServerName(): ?string;
13+
public function getReferrer(): ?string;
14+
}

src/Util/Http/ServerHttpRequest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Logging\Util\Http;
6+
7+
use KaririCode\Logging\Util\Http\Contract\HttpRequest;
8+
9+
class ServerHttpRequest implements HttpRequest
10+
{
11+
private array $serverParams;
12+
13+
public function __construct(array $serverParams = [])
14+
{
15+
$this->serverParams = array_merge($_SERVER, $serverParams);
16+
}
17+
18+
public function getUrl(): string
19+
{
20+
$scheme = ($this->serverParams['HTTPS'] ?? 'off') === 'on' ? 'https://' : 'http://';
21+
$host = $this->serverParams['HTTP_HOST'] ?? 'localhost';
22+
$uri = $this->serverParams['REQUEST_URI'] ?? '/';
23+
return $scheme . $host . $uri;
24+
}
25+
26+
public function getIp(): ?string
27+
{
28+
return $this->serverParams['REMOTE_ADDR'] ?? null;
29+
}
30+
31+
public function getMethod(): string
32+
{
33+
return $this->serverParams['REQUEST_METHOD'] ?? 'GET';
34+
}
35+
36+
public function getServerName(): ?string
37+
{
38+
return $this->serverParams['SERVER_NAME'] ?? null;
39+
}
40+
41+
public function getReferrer(): ?string
42+
{
43+
return $this->serverParams['HTTP_REFERER'] ?? null;
44+
}
45+
}

0 commit comments

Comments
 (0)