Skip to content

refactor(logging): encapsulate conditional logic into methods for bet… #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Handler/AbstractFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ public function __construct(
protected function ensureDirectoryExists(): void
{
$directory = dirname($this->filePath);
if (!is_dir($directory)) {
if (!$this->createDirectory($directory)) {
throw new LoggingException("Unable to create log directory: $directory");
}
} elseif (!$this->isDirectoryWritable($directory)) {

if ($this->directoryDoesNotExist($directory) && !$this->createDirectory($directory)) {
throw new LoggingException("Unable to create log directory: $directory");
}

if (!$this->isDirectoryWritable($directory)) {
throw new LoggingException("Log directory is not writable: $directory");
}
}

private function directoryDoesNotExist(string $directory): bool
{
return !is_dir($directory);
}

protected function createDirectory($path)
{
return mkdir($path, 0777, true);
Expand Down
7 changes: 6 additions & 1 deletion src/LoggerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ public function log(LogLevel $level, string|\Stringable $message, array $context
private function passesThreshold(array $context): bool
{
foreach ($this->thresholds as $key => $threshold) {
if (isset($context[$key]) && $context[$key] < $threshold) {
if ($this->contextBelowThreshold($context, $key, $threshold)) {
return false;
}
}

return true;
}

private function contextBelowThreshold(array $context, string $key, int $threshold): bool
{
return isset($context[$key]) && $context[$key] < $threshold;
}

public function addHandler(HandlerAware $handler): self
{
$this->handlers[] = $handler;
Expand Down
7 changes: 6 additions & 1 deletion src/Util/ReflectionFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,18 @@ private function filterConstructorParameters(\ReflectionClass $reflectionClass,
*/
protected function getClassFromMap(array $map, string $key): string
{
if (!isset($map[$key])) {
if ($this->configurationNotFound($map, $key)) {
throw new InvalidConfigurationException("Configuration not found for key: $key");
}

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

private function configurationNotFound(array $map, string $key): bool
{
return !isset($map[$key]);
}

/**
* Validates and extracts the class from a configuration value.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Util/SlackClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function sendRequest(array $payload): array
];

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

private function postToSlack(array $payload, array $headers): array
{
return $this->curlClient->post(self::SLACK_API_URL, $payload, $headers);
}

private function handleResponse(array $response): void
{
$httpCode = $response['status'];
Expand Down
Loading