8000 [Messenger] Add AvailableAtStamp to delay messages until fixed DateTime by delolmo · Pull Request #36512 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next Next commit
Add an AvailableAtStamp to delay messages until a fixed date and time…
… is reached
  • Loading branch information
Antonio del Olmo García committed Apr 21, 2020
commit 0ef17be67071187bcc9b723175bd62527b26077e
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Symfony\Component\Messenger\Middleware;

use DateTime;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
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);
}
}
38 changes: 38 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/AvailableAtStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Symfony\Component\Messenger\Stamp;

use DateTime;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
* 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;

/**
*
* @param \DateTime $availableAt
*/
public function __construct(DateTime $availableAt)
{
$this->availableAt = $availableAt;
}

/**
* The date and time on which the message will be available
*
* @return \DateTime
*/
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>
*
10000 * 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());
}
}
0