Skip to content

[Notifier] use a clock to create datetime instances #57099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,15 +34,19 @@
final class BlueskyTransport extends AbstractTransport
{
private array $authSession = [];
private ClockInterface $clock;

public function __construct(
#[\SensitiveParameter] private string $user,
#[\SensitiveParameter] private string $password,
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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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.');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Notifier/Bridge/Ntfy/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.');
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading