From e9ffb8c5347f2b01e3cc3b79c3b5d4e909b9405a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 24 May 2024 10:40:53 +0200 Subject: [PATCH] use a clock to create datetime instances --- .../Notifier/Bridge/Bluesky/BlueskyTransport.php | 8 +++++++- .../Bridge/Bluesky/Tests/BlueskyTransportTest.php | 9 +++++---- .../Component/Notifier/Bridge/Bluesky/composer.json | 1 + .../Component/Notifier/Bridge/Ntfy/NtfyOptions.php | 8 +++++++- src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json | 1 + .../Notifier/Bridge/PagerDuty/PagerDutyOptions.php | 8 +++++++- .../Component/Notifier/Bridge/PagerDuty/composer.json | 1 + .../Component/Notifier/Bridge/Smsbox/SmsboxOptions.php | 8 +++++++- .../Component/Notifier/Bridge/Smsbox/composer.json | 1 + 9 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyTransport.php b/src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyTransport.php index 71e6af7ede51e..881ac725be658 100644 --- a/src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyTransport.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Notifier\Bridge\Bluesky; use Psr\Log\LoggerInterface; +use Symfony\Component\Clock\Clock; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Mime\Part\File; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; @@ -32,6 +34,7 @@ final class BlueskyTransport extends AbstractTransport { private array $authSession = []; + private ClockInterface $clock; public function __construct( #[\SensitiveParameter] private string $user, @@ -39,8 +42,11 @@ public function __construct( private LoggerInterface $logger, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null, + ?ClockInterface $clock = null, ) { parent::__construct($client, $dispatcher); + + $this->clock = $clock ?? Clock::get(); } public function __toString(): string @@ -66,7 +72,7 @@ protected function doSend(MessageInterface $message): SentMessage $post = [ '$type' => 'app.bsky.feed.post', 'text' => $message->getSubject(), - 'createdAt' => \DateTimeImmutable::createFromFormat('U', time())->format('Y-m-d\\TH:i:s.u\\Z'), + 'createdAt' => $this->clock->now()->format('Y-m-d\\TH:i:s.u\\Z'), ]; if ([] !== $facets = $this->parseFacets($post['text'])) { $post['facets'] = $facets; diff --git a/src/Symfony/Component/Notifier/Bridge/Bluesky/Tests/BlueskyTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Bluesky/Tests/BlueskyTransportTest.php index 59a6e76194e2f..622d498143b1a 100644 --- a/src/Symfony/Component/Notifier/Bridge/Bluesky/Tests/BlueskyTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Bluesky/Tests/BlueskyTransportTest.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Notifier\Bridge\Bluesky\Tests; use Psr\Log\NullLogger; -use Symfony\Bridge\PhpUnit\ClockMock; +use Symfony\Component\Clock\MockClock; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\JsonMockResponse; use Symfony\Component\Mime\Part\File; @@ -28,15 +28,16 @@ final class BlueskyTransportTest extends TransportTestCase { + private static $clock; + protected function setUp(): void { - ClockMock::register(self::class); - ClockMock::withClockMock(1714293617); + self::$clock = new MockClock(new \DateTimeImmutable('@1714293617')); } public static function createTransport(?HttpClientInterface $client = null): BlueskyTransport { - $blueskyTransport = new BlueskyTransport('username', 'password', new NullLogger(), $client ?? new MockHttpClient()); + $blueskyTransport = new BlueskyTransport('username', 'password', new NullLogger(), $client ?? new MockHttpClient(), null, self::$clock); $blueskyTransport->setHost('bsky.social'); return $blueskyTransport; diff --git a/src/Symfony/Component/Notifier/Bridge/Bluesky/composer.json b/src/Symfony/Component/Notifier/Bridge/Bluesky/composer.json index 3f5fa25583790..4672c33a35426 100644 --- a/src/Symfony/Component/Notifier/Bridge/Bluesky/composer.json +++ b/src/Symfony/Component/Notifier/Bridge/Bluesky/composer.json @@ -22,6 +22,7 @@ "require": { "php": ">=8.2", "psr/log": "^1|^2|^3", + "symfony/clock": "^6.4|^7.0", "symfony/http-client": "^6.4|^7.0", "symfony/notifier": "^7.2", "symfony/string": "^6.4|^7.0" diff --git a/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyOptions.php b/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyOptions.php index f3b6138b9665f..a269967189dff 100644 --- a/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyOptions.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Notifier\Bridge\Ntfy; +use Symfony\Component\Clock\Clock; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Message\MessageOptionsInterface; use Symfony\Component\Notifier\Notification\Notification; @@ -26,9 +28,13 @@ final class NtfyOptions implements MessageOptionsInterface public const PRIORITY_LOW = 2; public const PRIORITY_MIN = 1; + private ClockInterface $clock; + public function __construct( private array $options = [], + ?ClockInterface $clock = null, ) { + $this->clock = $clock ?? Clock::get(); } public static function fromNotification(Notification $notification): self @@ -103,7 +109,7 @@ public function setTags(array $tags): self public function setDelay(\DateTimeInterface $dateTime): self { - if ($dateTime > (new \DateTime())) { + if ($dateTime > $this->clock->now()) { $this->options['delay'] = (string) $dateTime->getTimestamp(); } else { throw new LogicException('Delayed date must be defined in the future.'); diff --git a/src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json b/src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json index 50bfd414c9548..6964e83d70582 100644 --- a/src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json +++ b/src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json @@ -17,6 +17,7 @@ ], "require": { "php": ">=8.2", + "symfony/clock": "^6.4|^7.0", "symfony/http-client": "^6.4|^7.0", "symfony/notifier": "^6.4|^7.0" }, diff --git a/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyOptions.php b/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyOptions.php index 774e602fe0f8c..1f71d1da0e96d 100644 --- a/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyOptions.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Notifier\Bridge\PagerDuty; +use Symfony\Component\Clock\Clock; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Message\MessageOptionsInterface; @@ -19,7 +21,9 @@ */ final class PagerDutyOptions implements MessageOptionsInterface { - public function __construct(string $routingKey, string $eventAction, string $severity, private array $options = []) + private ClockInterface $clock; + + public function __construct(string $routingKey, string $eventAction, string $severity, private array $options = [], ?ClockInterface $clock = null) { if (!\in_array($eventAction, ['trigger', 'acknowledge', 'resolve'], true)) { throw new InvalidArgumentException('Invalid "event_action" option given.'); @@ -52,6 +56,8 @@ public function __construct(string $routingKey, string $eventAction, string $sev if (null === $dedupKey && \in_array($eventAction, ['acknowledge', 'resolve'], true)) { throw new InvalidArgumentException('Option "dedup_key" must be set for event actions: "acknowledge" & "resolve".'); } + + $this->clock = $clock ?? Clock::get(); } public function toArray(): array diff --git a/src/Symfony/Component/Notifier/Bridge/PagerDuty/composer.json b/src/Symfony/Component/Notifier/Bridge/PagerDuty/composer.json index c230357b622f8..93ca2a3771a89 100644 --- a/src/Symfony/Component/Notifier/Bridge/PagerDuty/composer.json +++ b/src/Symfony/Component/Notifier/Bridge/PagerDuty/composer.json @@ -17,6 +17,7 @@ ], "require": { "php": ">=8.2", + "symfony/clock": "^6.4|^7.0", "symfony/http-client": "^6.4|^7.0", "symfony/notifier": "^6.4|^7.0" }, diff --git a/src/Symfony/Component/Notifier/Bridge/Smsbox/SmsboxOptions.php b/src/Symfony/Component/Notifier/Bridge/Smsbox/SmsboxOptions.php index 400c69291929a..b752c54b2f0fe 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsbox/SmsboxOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsbox/SmsboxOptions.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Notifier\Bridge\Smsbox; +use Symfony\Component\Clock\Clock; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Intl\Countries; use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Charset; use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Day; @@ -28,9 +30,13 @@ */ final class SmsboxOptions implements MessageOptionsInterface { + private ClockInterface $clock; + public function __construct( private array $options = [], + ?ClockInterface $clock = null, ) { + $this->clock = $clock ?? Clock::get(); } public function getRecipientId(): null @@ -103,7 +109,7 @@ public function dateTime(\DateTimeImmutable $dateTime): static throw new InvalidArgumentException(sprintf('Either %1$s::dateTime() or %1$s::date() and %1$s::hour() must be called, but not both.', self::class)); } - if ($dateTime < new \DateTimeImmutable('now')) { + if ($dateTime < $this->clock->now()) { throw new InvalidArgumentException('The given DateTime must be greater to the current date.'); } diff --git a/src/Symfony/Component/Notifier/Bridge/Smsbox/composer.json b/src/Symfony/Component/Notifier/Bridge/Smsbox/composer.json index 35d74d2a9dd3b..c6e27462a7518 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsbox/composer.json +++ b/src/Symfony/Component/Notifier/Bridge/Smsbox/composer.json @@ -25,6 +25,7 @@ ], "require": { "php": ">=8.2", + "symfony/clock": "^6.4|^7.0", "symfony/http-client": "^6.4|^7.0", "symfony/notifier": "^7.1", "symfony/polyfill-php83": "^1.28"