Skip to content

Commit

Permalink
Bug fixes + better timezone support
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed May 4, 2023
1 parent 0314521 commit 3445542
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Laravel/Configs/ApiSdkConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ApiSdkConfig extends AbstractConfig

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

public function getLogging(): string
Expand Down
5 changes: 5 additions & 0 deletions src/Laravel/Configs/api_sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@
* Base directory name where FileLogger will store log files. Stores in local app storage.
*/
ApiSdkConfig::KeyKeepLogFilesForDays => env('API_SDK_KEEP_LOG_FILES_FOR_DAYS', 14),

/**
* Time of the day when log files will be cleared.
*/
ApiSdkConfig::KeyTimeForClearLogSchedule => '00:10',
],
];
4 changes: 4 additions & 0 deletions src/Laravel/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function register(): void
$this->bindApi();
$this->bindEvents();
$this->bindLogs();

if ($this->app->runningInConsole()) {
$this->commands([ClearFileLogsCommand::class]);
}
}

public function boot(): void
Expand Down
6 changes: 2 additions & 4 deletions src/Log/Actions/ClearFileLogsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ public function execute(LoggerConfigEntity $config): bool
$directory = end($pathComponents);

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

if (array_key_exists($directory, $leaveDirectoriesMap)) {
continue;
}

$this->logger->debug('Deleting log requests folder', [
'path' => $path,
]);
$this->logger->debug('Deleting log requests folder at ' . $path);
$this->filesystemOperator->deleteDirectory($path);
++$deleted;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Log/Actions/GetExtensionFromContentTypeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class GetExtensionFromContentTypeAction
public function execute(string $contentType): string
{
return match (true) {
str_contains($contentType, 'application/json') => 'json',
str_contains($contentType, 'text/xml') => 'xml',
str_contains($contentType, '/json') => 'json',
str_contains($contentType, '/xml') => 'xml',
str_contains($contentType, '/html') => 'html',
default => 'txt',
};
}
Expand Down
11 changes: 7 additions & 4 deletions src/Log/Services/FileLogPathService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class FileLogPathService implements FileLogPathServiceContract
public function getFilePath(string $baseDir, RequestInterface $request, string $id, ?string $type): string
{
$date = new DateTimeImmutable();
$cleanPath = str_replace(search: ['/', DIRECTORY_SEPARATOR], replace: '-', subject: $request->getUri()
->getPath());
$uriPath = $request->getUri()
->getPath();
$cleanPath = str_replace(search: ['/', DIRECTORY_SEPARATOR], replace: '-', subject: $uriPath);

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

public function getRootDirectoryName(DateTimeInterface $date): string
{
return $date->format('Y-m-d');
// Ensure that the date is in local timezone
return (new DateTimeImmutable())->setTimestamp($date->getTimestamp())
->format('Y-m-d');
}

public function isRootDirectory(string $directory): bool
{
return preg_match('#^\d{4}-\d{2}-\d{2}$#', $directory) !== 1;
return preg_match('#^\d{4}-\d{2}-\d{2}$#', $directory) === 1;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Log/Actions/GetExtensionFromContentTypeActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace WrkFlow\ApiSdkBuilderTests\Log\Actions;

use PHPUnit\Framework\TestCase;
use WrkFlow\ApiSdkBuilder\Log\Actions\GetExtensionFromContentTypeAction;

final class GetExtensionFromContentTypeActionTest extends TestCase
{
private GetExtensionFromContentTypeAction $getExtensionFromContentTypeAction;

protected function setUp(): void
{
$this->getExtensionFromContentTypeAction = new GetExtensionFromContentTypeAction();
}

/**
* @dataProvider contentTypeProvider
*/
public function testExecute(string $contentType, mixed $expectedExtension): void
{
$this->assertEquals($expectedExtension, $this->getExtensionFromContentTypeAction->execute($contentType));
}

public function contentTypeProvider(): array
{
return [
['application/json', 'json'],
['text/json', 'json'],
['text/xml', 'xml'],
['application/xml', 'xml'],
['text/plain', 'txt'],
['application/pdf', 'txt'],
['text/html', 'html'],
];
}
}
58 changes: 58 additions & 0 deletions tests/Log/Services/FileLogPathServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use PHPUnit\Framework\TestCase;
use WrkFlow\ApiSdkBuilder\Log\Entities\LoggerConfigEntity;
Expand All @@ -20,6 +21,63 @@ protected function setUp(): void
$this->fileLogPathService = new FileLogPathService();
}

public function dataIsRootDirectory(): array
{
return [
['2023-03-20', true],
['2022-12-10', true],
['2022-12-01', true],
['0000-00-00', true],
['2022-12-', false],
['2022-12-204', false],
['test', false],
['my-directory', false],
['00-00-00', false],
['01', false],
['', false],
];
}


/**
* @dataProvider dataIsRootDirectory
*/
public function testIsRootDirectory(string $directory, bool $expected): void
{
$this->assertEquals(expected: $expected, actual: $this->fileLogPathService->isRootDirectory($directory));
}

public function dataGetRootDirectoryName(): array
{
return [
['2022-12-10T00:30:00-03:00', '2022-12-10'],
['2022-12-10T00:30:00+03:00', '2022-12-09'],
['2022-12-10T00:30:00+00:00', '2022-12-10'],
['2022-12-10T23:30:00+01:00', '2022-12-10'],
['2022-12-10T23:30:00+02:00', '2022-12-10'],
['2022-12-10T23:30:00+03:00', '2022-12-10'],
['2022-12-10T23:30:00+04:00', '2022-12-10'],
['2022-12-10T23:30:00-01:00', '2022-12-11'],
['2022-12-10T23:30:00-02:00', '2022-12-11'],
['2022-12-10T23:30:00-03:00', '2022-12-11'],
['2022-12-10T23:30:00-04:00', '2022-12-11'],
];
}


/**
* @dataProvider dataGetRootDirectoryName
*/
public function testGetRootDirectoryName(string $date, string $expected): void
{
date_default_timezone_set('Europe/Prague');

$this->assertEquals(
expected: $expected,
actual: $this->fileLogPathService->getRootDirectoryName(new DateTimeImmutable($date))
);
}

/**
* @return array<string|int, array{0: Closure(static):void}>
*/
Expand Down

0 comments on commit 3445542

Please sign in to comment.