Skip to content

Commit

Permalink
[Scheduler] have TriggerInterface extend \Stringable
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Apr 23, 2023
1 parent f2ff84f commit 60dfc7f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public static function messagesProvider(): \Generator
],
'schedule' => [
RecurringMessage::trigger(new class() implements TriggerInterface {
public function __toString(): string
{
return 'foo';
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Scheduler/Trigger/CallbackTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function __construct(callable $callback)
$this->callback = $callback(...);
}

public function __toString(): string
{
return spl_object_hash($this->callback);
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
return ($this->callback)($run);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @experimental
*/
final class CronExpressionTrigger implements TriggerInterface, \Stringable
final class CronExpressionTrigger implements TriggerInterface
{
public function __construct(
private readonly CronExpression $expression = new CronExpression('* * * * *'),
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Scheduler/Trigger/DatePeriodTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public function __construct(
) {
}

public function __toString(): string
{
return sprintf(
'%s - %s, %s',
$this->period->getStartDate()->format(\DateTimeInterface::ATOM),
$this->period->getEndDate()?->format(\DateTimeInterface::ATOM) ?? '(inf)',
'-',
);
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
$iterator = $this->period->getIterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function __construct(
) {
}

public function __toString(): string
{
return sprintf('%s, from: %s, until: %s', $this->inner, $this->from->format(\DateTimeInterface::ATOM), $this->until->format(\DateTimeInterface::ATOM));
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
$nextRun = $this->inner->getNextRunDate($run);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @experimental
*/
interface TriggerInterface
interface TriggerInterface extends \Stringable
{
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable;
}

0 comments on commit 60dfc7f

Please sign in to comment.