8000 use a clock to create datetime instances · symfony/symfony@e9ffb8c · GitHub
[go: up one dir, main page]

Skip to content

Commit e9ffb8c

Browse files
committed
use a clock to create datetime instances
1 parent f8a46ed commit e9ffb8c

File tree

9 files changed

+37
-8
lines changed

9 files changed

+37
-8
lines changed

src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyTransport.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Notifier\Bridge\Bluesky;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Clock\Clock;
16+
use Symfony\Component\Clock\ClockInterface;
1517
use Symfony\Component\Mime\Part\File;
1618
use Symfony\Component\Notifier\Exception\TransportException;
1719
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
@@ -32,15 +34,19 @@
3234
final class BlueskyTransport extends AbstractTransport
3335
{
3436
private array $authSession = [];
37+
private ClockInterface $clock;
3538

3639
public function __construct(
3740
#[\SensitiveParameter] private string $user,
3841
#[\SensitiveParameter] private string $password,
3942
private LoggerInterface $logger,
4043
?HttpClientInterface $client = null,
4144
?EventDispatcherInterface $dispatcher = null,
45+
?ClockInterface $clock = null,
4246
) {
4347
parent::__construct($client, $dispatcher);
48+
49+
$this->clock = $clock ?? Clock::get();
4450
}
4551

4652
public function __toString(): string
@@ -66,7 +72,7 @@ protected function doSend(MessageInterface $message): SentMessage
6672
$post = [
6773
'$type' => 'app.bsky.feed.post',
6874
'text' => $message->getSubject(),
69-
'createdAt' => \DateTimeImmutable::createFromFormat('U', time())->format('Y-m-d\\TH:i:s.u\\Z'),
75+
'createdAt' => $this->clock->now()->format('Y-m-d\\TH:i:s.u\\Z'),
7076
];
7177
if ([] !== $facets = $this->parseFacets($post['text'])) {
7278
$post['facets'] = $facets;

src/Symfony/Component/Notifier/Bridge/Bluesky/Tests/BlueskyTransportTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\Bluesky\Tests;
1313

1414
use Psr\Log\NullLogger;
15-
use Symfony\Bridge\PhpUnit\ClockMock;
15+
use Symfony\Component\Clock\MockClock;
1616
use Symfony\Component\HttpClient\MockHttpClient;
1717
use Symfony\Component\HttpClient\Response\JsonMockResponse;
1818
use Symfony\Component\Mime\Part\File;
@@ -28,15 +28,16 @@
2828

2929
final class BlueskyTransportTest extends TransportTestCase
3030
{
31+
private static $clock;
32+
3133
protected function setUp(): void
3234
{
33-
ClockMock::register(self::class);
34-
ClockMock::withClockMock(1714293617);
35+
self::$clock = new MockClock(new \DateTimeImmutable('@1714293617'));
3536
}
3637

3738
public static function createTransport(?HttpClientInterface $client = null): BlueskyTransport
3839
{
39-
$blueskyTransport = new BlueskyTransport('username', 'password', new NullLogger(), $client ?? new MockHttpClient());
40+
$blueskyTransport = new BlueskyTransport('username', 'password', new NullLogger(), $client ?? new MockHttpClient(), null, self::$clock);
4041
$blueskyTransport->setHost('bsky.social');
4142

4243
return $blueskyTransport;

src/Symfony/Component/Notifier/Bridge/Bluesky/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"require": {
2323
"php": ">=8.2",
2424
"psr/log": "^1|^2|^3",
25+
"symfony/clock": "^6.4|^7.0",
2526
"symfony/http-client": "^6.4|^7.0",
2627
"symfony/notifier": "^7.2",
2728
"symfony/string": "^6.4|^7.0"

src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyOptions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Ntfy;
1313

14+
use Symfony\Component\Clock\Clock;
15+
use Symfony\Component\Clock\ClockInterface;
1416
use Symfony\Component\Notifier\Exception\LogicException;
1517
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
1618
use Symfony\Component\Notifier\Notification\Notification;
@@ -26,9 +28,13 @@ final class NtfyOptions implements MessageOptionsInterface
2628
public const PRIORITY_LOW = 2;
2729
public const PRIORITY_MIN = 1;
2830

31+
private ClockInterface $clock;
32+
2933
public function __construct(
3034
private array $options = [],
35+
?ClockInterface $clock = null,
3136
) {
37+
$this->clock = $clock ?? Clock::get();
3238
}
3339

3440
public static function fromNotification(Notification $notification): self
@@ -103,7 +109,7 @@ public function setTags(array $tags): self
103109

104110
public function setDelay(\DateTimeInterface $dateTime): self
105111
{
106-
if ($dateTime > (new \DateTime())) {
112+
if ($dateTime > $this->clock->now()) {
107113
$this->options['delay'] = (string) $dateTime->getTimestamp();
108114
} else {
109115
throw new LogicException('Delayed date must be defined in the future.');

src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=8.2",
20+
"symfony/clock": "^6.4|^7.0",
2021
"symfony/http-client": "^6.4|^7.0",
2122
"symfony/notifier": "^6.4|^7.0"
2223
},

src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyOptions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\PagerDuty;
1313

14+
use Symfony\Component\Clock\Clock;
15+
use Symfony\Component\Clock\ClockInterface;
1416
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1517
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
1618

@@ -19,7 +21,9 @@
1921
*/
2022
final class PagerDutyOptions implements MessageOptionsInterface
2123
{
22-
public function __construct(string $routingKey, string $eventAction, string $severity, private array $options = [])
24+
private ClockInterface $clock;
25+
26+
public function __construct(string $routingKey, string $eventAction, string $severity, private array $options = [], ?ClockInterface $clock = null)
2327
{
2428
if (!\in_array($eventAction, ['trigger', 'acknowledge', 'resolve'], true)) {
2529
throw new InvalidArgumentException('Invalid "event_action" option given.');
@@ -52,6 +56,8 @@ public function __construct(string $routingKey, string $eventAction, string $sev
5256
if (null === $dedupKey && \in_array($eventAction, ['acknowledge', 'resolve'], true)) {
5357
throw new InvalidArgumentException('Option "dedup_key" must be set for event actions: "acknowledge" & "resolve".');
5458
}
59+
60+
$this->clock = $clock ?? Clock::get();
5561
}
5662

5763
public function toArray(): array

src/Symfony/Component/Notifier/Bridge/PagerDuty/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=8.2",
20+
"symfony/clock": "^6.4|^7.0",
2021
"symfony/http-client": "^6.4|^7.0",
2122
"symfony/notifier": "^6.4|^7.0"
2223
},

src/Symfony/Component/Notifier/Bridge/Smsbox/SmsboxOptions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Smsbox;
1313

14+
use Symfony\Component\Clock\Clock;
15+
use Symfony\Component\Clock\ClockInterface;
1416
use Symfony\Component\Intl\Countries;
1517
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Charset;
1618
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Day;
@@ -28,9 +30,13 @@
2830
*/
2931
final class SmsboxOptions implements MessageOptionsInterface
3032
{
33+
private ClockInterface $clock;
34+
3135
public function __construct(
3236
private array $options = [],
37+
?ClockInterface $clock = null,
3338
) {
39+
$this->clock = $clock ?? Clock::get();
3440
}
3541

3642
public function getRecipientId(): null
@@ -103,7 +109,7 @@ public function dateTime(\DateTimeImmutable $dateTime): static
103109
throw new InvalidArgumentException(sprintf('Either %1$s::dateTime() or %1$s::date() and %1$s::hour() must be called, but not both.', self::class));
104110
}
105111

106-
if ($dateTime < new \DateTimeImmutable('now')) {
112+
if ($dateTime < $this->clock->now()) {
107113
throw new InvalidArgumentException('The given DateTime must be greater to the current date.');
108114
}
109115

src/Symfony/Component/Notifier/Bridge/Smsbox/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"require": {
2727
"php": ">=8.2",
28+
"symfony/clock": "^6.4|^7.0",
2829
"symfony/http-client": "^6.4|^7.0",
2930
"symfony/notifier": "^7.1",
3031
"symfony/polyfill-php83": "^1.28"

0 commit comments

Comments
 (0)
0