8000 [Scheduler] Add `AbstractTriggerDecorator` by kbond · Pull Request #51152 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Scheduler] Add AbstractTriggerDecorator #51152

New issue
8000

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
Jul 31, 2023
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Allow setting timezone of next run date in CronExpressionTrigger
* Add `AbstractTriggerDecorator`

6.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Tests\Trigger;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Trigger\ExcludeTimeTrigger;
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class AbstractDecoratedTriggerTest extends TestCase
{
public function testCanGetInnerTrigger()
{
$trigger = new JitterTrigger($inner = $this->createMock(TriggerInterface::class));

$this->assertSame($inner, $trigger->inner());
$this->assertSame([$trigger], iterator_to_array($trigger->decorators()));
}

public function testCanGetNestedInnerTrigger()
{
$trigger = new ExcludeTimeTrigger(
$jitter = new JitterTrigger($inner = $this->createMock(TriggerInterface::class)),
new \DateTimeImmutable(),
new \DateTimeImmutable(),
);

$this->assertSame($inner, $trigger->inner());
$this->assertSame([$trigger, $jitter], iterator_to_array($trigger->decorators()));
}
}
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Trigger;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
abstract class AbstractDecoratedTrigger implements TriggerInterface
{
public function __construct(private TriggerInterface $inner)
{
}

final public function inner(): TriggerInterface
{
$inner = $this->inner;

while ($inner instanceof self) {
$inner = $inner->inner;
}

return $inner;
}

/**
* @return \Traversable<self>
*/
final public function decorators(): \Traversable
{
yield $this;

$inner = $this->inner;

while ($inner instanceof self) {
yield $inner;

$inner = $inner->inner;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
/**
* @experimental
*/
final class ExcludeTimeTrigger implements TriggerInterface
final class ExcludeTimeTrigger extends AbstractDecoratedTrigger
{
public function __construct(
private readonly TriggerInterface $inner,
private readonly \DateTimeImmutable $from,
private readonly \DateTimeImmutable $until,
) {
parent::__construct($inner);
}

public function __toString(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Scheduler/Trigger/JitterTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class JitterTrigger implements TriggerInterface
final class JitterTrigger extends AbstractDecoratedTrigger
{
/**
* @param positive-int $maxSeconds
*/
public function __construct(private readonly TriggerInterface $trigger, private readonly int $maxSeconds = 60)
{
parent::__construct($trigger);
}

public function __toString(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @experimental
*/
class PeriodicalTrigger implements TriggerInterface, \Stringable
class PeriodicalTrigger implements TriggerInterface
{
private float $intervalInSeconds = 0.0;
private \DateTimeImmutable $from;
Expand Down
0