Skip to content

Commit 3445542

Browse files
committed
Bug fixes + better timezone support
1 parent 0314521 commit 3445542

8 files changed

+119
-11
lines changed

src/Laravel/Configs/ApiSdkConfig.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApiSdkConfig extends AbstractConfig
1818

1919
public function getLoggers(): LoggersMapEntity
2020
{
21-
return new LoggersMapEntity($this->get(self::KeyLoggers, []));
21+
return new LoggersMapEntity($this->get(keyOrPath: [self::KeyLogging, self::KeyLoggers]));
2222
}
2323

2424
public function getLogging(): string

src/Laravel/Configs/api_sdk.php

+5
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@
4141
* Base directory name where FileLogger will store log files. Stores in local app storage.
4242
*/
4343
ApiSdkConfig::KeyKeepLogFilesForDays => env('API_SDK_KEEP_LOG_FILES_FOR_DAYS', 14),
44+
45+
/**
46+
* Time of the day when log files will be cleared.
47+
*/
48+
ApiSdkConfig::KeyTimeForClearLogSchedule => '00:10',
4449
],
4550
];

src/Laravel/LaravelServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function register(): void
5454
$this->bindApi();
5555
$this->bindEvents();
5656
$this->bindLogs();
57+
58+
if ($this->app->runningInConsole()) {
59+
$this->commands([ClearFileLogsCommand::class]);
60+
}
5761
}
5862

5963
public function boot(): void

src/Log/Actions/ClearFileLogsAction.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ public function execute(LoggerConfigEntity $config): bool
4141
$directory = end($pathComponents);
4242

4343
// Is the folder in correct format (Y-m-d date).
44-
if ($this->fileLogPathService->isRootDirectory($directory)) {
44+
if ($this->fileLogPathService->isRootDirectory($directory) === false) {
4545
continue;
4646
}
4747

4848
if (array_key_exists($directory, $leaveDirectoriesMap)) {
4949
continue;
5050
}
5151

52-
$this->logger->debug('Deleting log requests folder', [
53-
'path' => $path,
54-
]);
52+
$this->logger->debug('Deleting log requests folder at ' . $path);
5553
$this->filesystemOperator->deleteDirectory($path);
5654
++$deleted;
5755
}

src/Log/Actions/GetExtensionFromContentTypeAction.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class GetExtensionFromContentTypeAction
99
public function execute(string $contentType): string
1010
{
1111
return match (true) {
12-
str_contains($contentType, 'application/json') => 'json',
13-
str_contains($contentType, 'text/xml') => 'xml',
12+
str_contains($contentType, '/json') => 'json',
13+
str_contains($contentType, '/xml') => 'xml',
14+
str_contains($contentType, '/html') => 'html',
1415
default => 'txt',
1516
};
1617
}

src/Log/Services/FileLogPathService.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class FileLogPathService implements FileLogPathServiceContract
2020
public function getFilePath(string $baseDir, RequestInterface $request, string $id, ?string $type): string
2121
{
2222
$date = new DateTimeImmutable();
23-
$cleanPath = str_replace(search: ['/', DIRECTORY_SEPARATOR], replace: '-', subject: $request->getUri()
24-
->getPath());
23+
$uriPath = $request->getUri()
24+
->getPath();
25+
$cleanPath = str_replace(search: ['/', DIRECTORY_SEPARATOR], replace: '-', subject: $uriPath);
2526

2627
return implode(
2728
separator: DIRECTORY_SEPARATOR,
@@ -37,12 +38,14 @@ public function getFilePath(string $baseDir, RequestInterface $request, string $
3738

3839
public function getRootDirectoryName(DateTimeInterface $date): string
3940
{
40-
return $date->format('Y-m-d');
41+
// Ensure that the date is in local timezone
42+
return (new DateTimeImmutable())->setTimestamp($date->getTimestamp())
43+
->format('Y-m-d');
4144
}
4245

4346
public function isRootDirectory(string $directory): bool
4447
{
45-
return preg_match('#^\d{4}-\d{2}-\d{2}$#', $directory) !== 1;
48+
return preg_match('#^\d{4}-\d{2}-\d{2}$#', $directory) === 1;
4649
}
4750

4851
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WrkFlow\ApiSdkBuilderTests\Log\Actions;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use WrkFlow\ApiSdkBuilder\Log\Actions\GetExtensionFromContentTypeAction;
9+
10+
final class GetExtensionFromContentTypeActionTest extends TestCase
11+
{
12+
private GetExtensionFromContentTypeAction $getExtensionFromContentTypeAction;
13+
14+
protected function setUp(): void
15+
{
16+
$this->getExtensionFromContentTypeAction = new GetExtensionFromContentTypeAction();
17+
}
18+
19+
/**
20+
* @dataProvider contentTypeProvider
21+
*/
22+
public function testExecute(string $contentType, mixed $expectedExtension): void
23+
{
24+
$this->assertEquals($expectedExtension, $this->getExtensionFromContentTypeAction->execute($contentType));
25+
}
26+
27+
public function contentTypeProvider(): array
28+
{
29+
return [
30+
['application/json', 'json'],
31+
['text/json', 'json'],
32+
['text/xml', 'xml'],
33+
['application/xml', 'xml'],
34+
['text/plain', 'txt'],
35+
['application/pdf', 'txt'],
36+
['text/html', 'html'],
37+
];
38+
}
39+
}

tests/Log/Services/FileLogPathServiceTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use DateTime;
9+
use DateTimeImmutable;
910
use DateTimeInterface;
1011
use PHPUnit\Framework\TestCase;
1112
use WrkFlow\ApiSdkBuilder\Log\Entities\LoggerConfigEntity;
@@ -20,6 +21,63 @@ protected function setUp(): void
2021
$this->fileLogPathService = new FileLogPathService();
2122
}
2223

24+
public function dataIsRootDirectory(): array
25+
{
26+
return [
27+
['2023-03-20', true],
28+
['2022-12-10', true],
29+
['2022-12-01', true],
30+
['0000-00-00', true],
31+
['2022-12-', false],
32+
['2022-12-204', false],
33+
['test', false],
34+
['my-directory', false],
35+
['00-00-00', false],
36+
['01', false],
37+
['', false],
38+
];
39+
}
40+
41+
42+
/**
43+
* @dataProvider dataIsRootDirectory
44+
*/
45+
public function testIsRootDirectory(string $directory, bool $expected): void
46+
{
47+
$this->assertEquals(expected: $expected, actual: $this->fileLogPathService->isRootDirectory($directory));
48+
}
49+
50+
public function dataGetRootDirectoryName(): array
51+
{
52+
return [
53+
['2022-12-10T00:30:00-03:00', '2022-12-10'],
54+
['2022-12-10T00:30:00+03:00', '2022-12-09'],
55+
['2022-12-10T00:30:00+00:00', '2022-12-10'],
56+
['2022-12-10T23:30:00+01:00', '2022-12-10'],
57+
['2022-12-10T23:30:00+02:00', '2022-12-10'],
58+
['2022-12-10T23:30:00+03:00', '2022-12-10'],
59+
['2022-12-10T23:30:00+04:00', '2022-12-10'],
60+
['2022-12-10T23:30:00-01:00', '2022-12-11'],
61+
['2022-12-10T23:30:00-02:00', '2022-12-11'],
62+
['2022-12-10T23:30:00-03:00', '2022-12-11'],
63+
['2022-12-10T23:30:00-04:00', '2022-12-11'],
64+
];
65+
}
66+
67+
68+
/**
69+
* @dataProvider dataGetRootDirectoryName
70+
*/
71+
public function testGetRootDirectoryName(string $date, string $expected): void
72+
{
73+
date_default_timezone_set('Europe/Prague');
74+
75+
$this->assertEquals(
76+
expected: $expected,
77+
actual: $this->fileLogPathService->getRootDirectoryName(new DateTimeImmutable($date))
78+
);
79+
}
80+
2381
/**
2482
* @return array<string|int, array{0: Closure(static):void}>
2583
*/

0 commit comments

Comments
 (0)