Skip to content

[Messenger] Add AvailableAtStamp to delay messages until fixed DateTime #36512

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

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ Security
* Removed `ROLE_PREVIOUS_ADMIN` role in favor of `IS_IMPERSONATOR` attribute
* Removed `LogoutSuccessHandlerInterface` and `LogoutHandlerInterface`, register a listener on the `LogoutEvent` event instead.
* Removed `DefaultLogoutSuccessHandler` in favor of `DefaultLogoutListener`.

Yaml
----

* Removed support for using the `!php/object` and `!php/const` tags without a value.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\Messenger\Middleware;

use DateTime;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\AvailableAtStamp;
use Symfony\Component\Messenger\Stamp\DelayStamp;

/**
* @author Antonio del Olmo García <adelolmog@gmail.com>
*/
class AvailableAtStampMiddleware implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$availableAtStamp = $envelope->last(AvailableAtStamp::class);

if (null !== $availableAtStamp) {
$availableAt = $availableAtStamp->getAvailableAt();
$now = new DateTime();

$delay = $availableAt->getTimestamp() - $now->getTimestamp();

$envelope = $envelope->with(
new DelayStamp($delay * 1000)
);
}

return $stack->next()->handle($envelope, $stack);
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/AvailableAtStamp.php
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\Messenger\Stamp;

use DateTime;

/**
* Stamp used to identify when a message should become available.
*
* @author Antonio del Olmo García <adelolmog@gmail.com>
*/
class AvailableAtStamp implements StampInterface
{
/**
* @var \DateTime
*/
protected $availableAt;

public function __construct(DateTime $availableAt)
{
$this->availableAt = $availableAt;
}

/**
* The date and time on which the message will be available.
*/
public function getAvailableAt(): DateTime
{
return $this->availableAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\Messenger\Tests\Middleware;

use DateTime;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\AvailableAtStampMiddleware;
use Symfony\Component\Messenger\Stamp\AvailableAtStamp;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

/**
* @author Antonio del Olmo García <adelolmog@gmail.com>
*/
class AvailableAtStampMiddlewareTest extends MiddlewareTestCase
{
public function testAvailableAtAndDelayStampAreAdded()
{
$now = new DateTime();
$availableAt = (clone $now)->modify('+1000 seconds');
$availableAtStamp = new AvailableAtStamp($availableAt);

$middleware = new AvailableAtStampMiddleware();

$envelope = new Envelope(
new DummyMessage('the message'),
[
$availableAtStamp,
]
);

$finalEnvelope = $middleware->handle($envelope, $this->getStackMock());

/** @var AvailableAtStamp $availableAtStamp */
$availableAtStamp = $finalEnvelope->last(AvailableAtStamp::class);
$this->assertNotNull($availableAtStamp);
$this->assertSame($availableAt, $availableAtStamp->getAvailableAt());

/** @var DelayStamp $delayStamp */
$delayStamp = $finalEnvelope->last(DelayStamp::class);
$this->assertNotNull($delayStamp);
$this->assertSame(1000 * 1000, $delayStamp->getDelay());

// the stamp should not be added over and over again
$finalEnvelope = $middleware->handle($finalEnvelope, $this->getStackMock());
$this->assertCount(1, $finalEnvelope->all(AvailableAtStamp::class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Messenger\Tests\Stamp;

use DateTime;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Stamp\AvailableAtStamp;

/**
* @author Antonio del Olmo García <adelolmog@gmail.com>
*/
class AvailableAtStampTest extends TestCase
{
public function testStamp()
{
$stamp = new AvailableAtStamp($availableAt = new DateTime());
$this->assertSame($availableAt, $stamp->getAvailableAt());
}
}