Skip to content

Commit 856cb67

Browse files
authored
Merge pull request #3 from KaririCode-Framework/develop
refactor(logging): encapsulate conditional logic into methods for bet…
2 parents f23cac3 + cd0c76e commit 856cb67

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/Handler/AbstractFileHandler.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ public function __construct(
2424
protected function ensureDirectoryExists(): void
2525
{
2626
$directory = dirname($this->filePath);
27-
if (!is_dir($directory)) {
28-
if (!$this->createDirectory($directory)) {
29-
throw new LoggingException("Unable to create log directory: $directory");
30-
}
31-
} elseif (!$this->isDirectoryWritable($directory)) {
27+
28+
if ($this->directoryDoesNotExist($directory) && !$this->createDirectory($directory)) {
29+
throw new LoggingException("Unable to create log directory: $directory");
30+
}
31+
32+
if (!$this->isDirectoryWritable($directory)) {
3233
throw new LoggingException("Log directory is not writable: $directory");
3334
}
3435
}
3536

37+
private function directoryDoesNotExist(string $directory): bool
38+
{
39+
return !is_dir($directory);
40+
}
41+
3642
protected function createDirectory($path)
3743
{
3844
return mkdir($path, 0777, true);

src/LoggerManager.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ public function log(LogLevel $level, string|\Stringable $message, array $context
5151
private function passesThreshold(array $context): bool
5252
{
5353
foreach ($this->thresholds as $key => $threshold) {
54-
if (isset($context[$key]) && $context[$key] < $threshold) {
54+
if ($this->contextBelowThreshold($context, $key, $threshold)) {
5555
return false;
5656
}
5757
}
5858

5959
return true;
6060
}
6161

62+
private function contextBelowThreshold(array $context, string $key, int $threshold): bool
63+
{
64+
return isset($context[$key]) && $context[$key] < $threshold;
65+
}
66+
6267
public function addHandler(HandlerAware $handler): self
6368
{
6469
$this->handlers[] = $handler;

src/Util/ReflectionFactoryTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,18 @@ private function filterConstructorParameters(\ReflectionClass $reflectionClass,
103103
*/
104104
protected function getClassFromMap(array $map, string $key): string
105105
{
106-
if (!isset($map[$key])) {
106+
if ($this->configurationNotFound($map, $key)) {
107107
throw new InvalidConfigurationException("Configuration not found for key: $key");
108108
}
109109

110110
return self::validateAndExtractClass($map[$key], $key);
111111
}
112112

113+
private function configurationNotFound(array $map, string $key): bool
114+
{
115+
return !isset($map[$key]);
116+
}
117+
113118
/**
114119
* Validates and extracts the class from a configuration value.
115120
*

src/Util/SlackClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function sendRequest(array $payload): array
7575
];
7676

7777
try {
78-
return $this->curlClient->post(self::SLACK_API_URL, $payload, $headers);
78+
return $this->postToSlack($payload, $headers);
7979
} catch (\JsonException $e) {
8080
$this->circuitBreaker->recordFailure();
8181
throw new LoggingException('Failed to encode message for Slack: ' . $e->getMessage(), 0, $e);
@@ -85,6 +85,11 @@ private function sendRequest(array $payload): array
8585
}
8686
}
8787

88+
private function postToSlack(array $payload, array $headers): array
89+
{
90+
return $this->curlClient->post(self::SLACK_API_URL, $payload, $headers);
91+
}
92+
8893
private function handleResponse(array $response): void
8994
{
9095
$httpCode = $response['status'];

0 commit comments

Comments
 (0)