1
0
Fork 0

Make sure that stored timestamps are always in UTC time zone

This commit is contained in:
Krzysztof Sikorski 2022-04-17 22:24:43 +02:00
parent 2280822ab4
commit 3d838d175e
Signed by: krzysztof-sikorski
GPG Key ID: 4EB564BD08FE8476
4 changed files with 19 additions and 3 deletions

View File

@ -5,8 +5,11 @@ declare(strict_types=1);
namespace App\Contract\Service;
use DateTimeImmutable;
use DateTimeZone;
interface ClockInterface
{
public function getUtcTimeZone(): DateTimeZone;
public function getCurrentDateTime(): DateTimeImmutable;
}

View File

@ -10,9 +10,14 @@ use DateTimeZone;
final class Clock implements ClockInterface
{
public function getUtcTimeZone(): DateTimeZone
{
return new DateTimeZone(timezone: 'UTC');
}
public function getCurrentDateTime(): DateTimeImmutable
{
$timezone = new DateTimeZone(timezone: 'UTC');
$timezone = $this->getUtcTimeZone();
return new DateTimeImmutable(datetime: 'now', timezone: $timezone);
}
}

View File

@ -7,11 +7,13 @@ namespace App\Service;
use App\Doctrine\Entity\NexusRawData;
use DateTimeImmutable;
use DateTimeZone;
use function array_key_exists;
final class NexusRawDataFactory
{
public function createFromJsonDataSubmission(object $decodedJsonData): NexusRawData
public function createFromJsonDataSubmission(object $decodedJsonData, DateTimeZone $timeZone): NexusRawData
{
$nexusRawData = new NexusRawData();
@ -19,10 +21,12 @@ final class NexusRawDataFactory
if (array_key_exists(key: 'requestStartedAt', array: $data)) {
$requestStartedAt = new DateTimeImmutable(datetime: $data['requestStartedAt']);
$requestStartedAt->setTimezone(timezone: $timeZone);
$nexusRawData->setRequestStartedAt(requestStartedAt: $requestStartedAt);
}
if (array_key_exists(key: 'responseCompletedAt', array: $data)) {
$responseCompletedAt = new DateTimeImmutable(datetime: $data['responseCompletedAt']);
$responseCompletedAt->setTimezone(timezone: $timeZone);
$nexusRawData->setResponseCompletedAt(responseCompletedAt: $responseCompletedAt);
}
if (array_key_exists(key: 'method', array: $data)) {

View File

@ -23,9 +23,13 @@ final class NexusRawDataManager
string $userAccessTokenValue,
object $decodedJsonData
): NexusRawDataSubmissionResult {
$timeZone = $this->clock->getUtcTimeZone();
$currentDateTime = $this->clock->getCurrentDateTime();
$nexusRawData = $this->nexusRawDataFactory->createFromJsonDataSubmission(decodedJsonData: $decodedJsonData);
$nexusRawData = $this->nexusRawDataFactory->createFromJsonDataSubmission(
decodedJsonData: $decodedJsonData,
timeZone: $timeZone
);
$userAccessToken = $this->userAccessTokenRepository->findByValue(value: $userAccessTokenValue);
if (null === $userAccessToken) {