Skip to content

Commit 23f6c9f

Browse files
committed
refactor: improve code readability and dependency management in utility classes
- Refactored SlackClient with Builder Pattern for better dependency injection - Improved error handling and added constants in ConfigGenerator - Cleaned up redundant code in CurlClient and added proper error handling - Enhanced MetricsProcessor logic for better performance - Simplified HttpRequest and ServerHttpRequest classes - Improved asset handling logic in AssetPublisher - Refined test cases in test_config.php for improved test coverage and clarity
1 parent bbf2f95 commit 23f6c9f

File tree

8 files changed

+59
-38
lines changed

8 files changed

+59
-38
lines changed

src/Processor/MetricsProcessor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use KaririCode\Contract\ImmutableValue;
88
use KaririCode\Contract\Logging\LogProcessor;
9-
use KaririCode\Logging\LogRecord;
109

1110
class MetricsProcessor implements LogProcessor
1211
{

src/Util/AssetPublisher.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,53 @@
88

99
class AssetPublisher
1010
{
11+
private const DIRECTORY_PERMISSIONS = 0755;
12+
1113
public static function publishAssets(Event $event): void
1214
{
1315
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
1416
$sourceDir = $vendorDir . '/kariricode/logging/resources';
1517
$targetDir = dirname($vendorDir) . '/resources/logging';
1618

1719
if (!is_dir($sourceDir)) {
18-
$event->getIO()->write('<error>Source directory not found: ' . $sourceDir . '</error>');
20+
$event->getIO()->writeError(sprintf('Source directory not found: %s', $sourceDir));
1921

2022
return;
2123
}
2224

23-
if (!is_dir($targetDir) && !@mkdir($targetDir, 0755, true) && !is_dir($targetDir)) {
24-
throw new \RuntimeException(sprintf('Directory "%s" was not created', $targetDir));
25-
}
25+
self::createDirectory($targetDir);
2626

27-
/** @var RecursiveDirectoryIterator $iterator */
2827
$iterator = new \RecursiveIteratorIterator(
2928
new \RecursiveDirectoryIterator($sourceDir, \RecursiveDirectoryIterator::SKIP_DOTS),
3029
\RecursiveIteratorIterator::SELF_FIRST
3130
);
3231

32+
/** @var \RecursiveDirectoryIterator $iterator */
3333
foreach ($iterator as $item) {
3434
$subPathName = $iterator->getSubPathName();
35+
$targetPath = $targetDir . DIRECTORY_SEPARATOR . $subPathName;
36+
3537
if ($item->isDir()) {
36-
$targetPath = $targetDir . DIRECTORY_SEPARATOR . $subPathName;
37-
if (!is_dir($targetPath) && !@mkdir($targetPath, 0755, true) && !is_dir($targetPath)) {
38-
throw new \RuntimeException(sprintf('Directory "%s" was not created', $targetPath));
39-
}
38+
self::createDirectory($targetPath);
4039
} else {
41-
$targetPath = $targetDir . DIRECTORY_SEPARATOR . $subPathName;
42-
if (!copy($item->getPathname(), $targetPath)) {
43-
throw new \RuntimeException(sprintf('Failed to copy "%s" to "%s"', $item->getPathname(), $targetPath));
44-
}
40+
self::copyFile($item->getPathname(), $targetPath);
4541
}
4642
}
4743

48-
$event->getIO()->write('<info>Published assets to: ' . $targetDir . '</info>');
44+
$event->getIO()->write(sprintf('<info>Published assets to: %s</info>', $targetDir));
45+
}
46+
47+
private static function createDirectory(string $path): void
48+
{
49+
if (!is_dir($path) && !mkdir($path, self::DIRECTORY_PERMISSIONS, true)) {
50+
throw new \RuntimeException(sprintf('Directory "%s" was not created', $path));
51+
}
52+
}
53+
54+
private static function copyFile(string $source, string $destination): void
55+
{
56+
if (!copy($source, $destination)) {
57+
throw new \RuntimeException(sprintf('Failed to copy "%s" to "%s"', $source, $destination));
58+
}
4959
}
5060
}

src/Util/ConfigGenerator.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,43 @@
88

99
class ConfigGenerator
1010
{
11+
private const DIRECTORY_PERMISSION = 0755;
12+
private const FILE_PERMISSION = 0664;
13+
1114
public static function generateConfig(Event $event): void
1215
{
1316
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
1417
$projectRoot = dirname($vendorDir);
1518
$configDir = $projectRoot . '/config';
1619
$configFile = $configDir . '/logging.php';
1720

18-
if (!is_dir($configDir) && !mkdir($configDir, 0755, true) && !is_dir($configDir)) {
19-
throw new \RuntimeException(sprintf('Directory "%s" was not created', $configDir));
20-
}
21+
self::ensureConfigDirectoryExists($configDir);
2122

2223
if (!file_exists($configFile)) {
23-
$configContent = self::getConfigContent();
24-
if (false === file_put_contents($configFile, $configContent)) {
25-
throw new \RuntimeException(sprintf('Failed to write config file: %s', $configFile));
26-
}
27-
$event->getIO()->write('<info>Created config file: ' . $configFile . '</info>');
24+
self::createConfigFile($configFile, $event);
2825
} else {
2926
$event->getIO()->write('<info>Config file already exists: ' . $configFile . '</info>');
3027
}
3128
}
3229

30+
private static function ensureConfigDirectoryExists(string $configDir): void
31+
{
32+
if (!is_dir($configDir) && !mkdir($configDir, self::DIRECTORY_PERMISSION, true) && !is_dir($configDir)) {
33+
throw new \RuntimeException(sprintf('Directory "%s" was not created', $configDir));
34+
}
35+
}
36+
37+
private static function createConfigFile(string $configFile, Event $event): void
38+
{
39+
$configContent = self::getConfigContent();
40+
if (false === file_put_contents($configFile, $configContent)) {
41+
throw new \RuntimeException(sprintf('Failed to write config file: %s', $configFile));
42+
}
43+
44+
chmod($configFile, self::FILE_PERMISSION); // Aplicando a permissão ao arquivo
45+
$event->getIO()->write('<info>Created config file: ' . $configFile . '</info>');
46+
}
47+
3348
private static function getConfigContent(): string
3449
{
3550
return <<<'EOT'
@@ -190,6 +205,7 @@ private static function getConfigContent(): string
190205
'channels' => ['single', 'daily'],
191206
],
192207
];
208+
193209
EOT;
194210
}
195211
}

src/Util/CurlClient.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
class CurlClient
88
{
99
private const TIMEOUT = 30;
10-
private const DEFAULT_HEADERS = ['Content-Type: application/json'];
1110

1211
public function post(string $url, array $data, array $headers = []): array
1312
{

src/Util/Http/HttpRequest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
interface HttpRequest
88
{
99
public function getUrl(): string;
10+
1011
public function getIp(): ?string;
12+
1113
public function getMethod(): string;
14+
1215
public function getServerName(): ?string;
16+
1317
public function getReferrer(): ?string;
1418
}

src/Util/Http/ServerHttpRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function getUrl(): string
2020
$scheme = ($this->serverParams['HTTPS'] ?? 'off') === 'on' ? 'https://' : 'http://';
2121
$host = $this->serverParams['HTTP_HOST'] ?? 'localhost';
2222
$uri = $this->serverParams['REQUEST_URI'] ?? '/';
23+
2324
return $scheme . $host . $uri;
2425
}
2526

src/Util/SlackClient.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,20 @@ class SlackClient
1616
public function __construct(
1717
private string $botToken,
1818
private string $channel,
19-
private CircuitBreaker $circuitBreaker,
20-
private Retry $retry,
21-
private Fallback $fallback,
22-
private CurlClient $curlClient
19+
private CircuitBreaker $circuitBreaker = new CircuitBreaker(3, 60),
20+
private Retry $retry = new Retry(3, 1000, 2, 100),
21+
private Fallback $fallback = new Fallback(),
22+
private CurlClient $curlClient = new CurlClient()
2323
) {
2424
}
2525

2626
public static function create(
2727
string $botToken,
2828
string $channel,
29-
?CircuitBreaker $circuitBreaker = null,
30-
?Retry $retry = null,
31-
?Fallback $fallback = null,
32-
?CurlClient $curlClient = null
3329
): self {
3430
return new self(
3531
$botToken,
36-
$channel,
37-
$circuitBreaker ?? new CircuitBreaker(3, 60),
38-
$retry ?? new Retry(3, 1000, 2, 100),
39-
$fallback ?? new Fallback(),
40-
$curlClient ?? new CurlClient()
32+
$channel
4133
);
4234
}
4335

tests/Logger/test_config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return ['key' => 'value'];
1+
<?php return ['key' => 'value'];

0 commit comments

Comments
 (0)