Skip to content

Commit

Permalink
bug #38141 [Messenger] Added factory methods to DelayStamp for better…
Browse files Browse the repository at this point in the history
… DX (Toflar)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[Messenger] Added factory methods to DelayStamp for better DX

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        | not necessary imho

I often find myself delaying messages for a few minutes or so and I always have to remember the `DelayStamp` uses milliseconds. I guess these simple factories could improve DX quite a bit :)

Commits
-------

fdc8da0 [Messenger] Added factory methods to DelayStamp for better DX
  • Loading branch information
fabpot committed Sep 10, 2020
2 parents 474e877 + fdc8da0 commit 9cd3e67
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added `FlattenExceptionNormalizer` to give more information about the exception on Messenger background processes. The `FlattenExceptionNormalizer` has a higher priority than `ProblemNormalizer` and it is only used when the Messenger serialization context is set.
* Added factory methods to `DelayStamp`.

5.1.0
-----
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/DelayStamp.php
Expand Up @@ -30,4 +30,19 @@ public function getDelay(): int
{
return $this->delay;
}

public static function delayForSeconds(int $seconds): self
{
return new self($seconds * 1000);
}

public static function delayForMinutes(int $minutes): self
{
return self::delayForSeconds($minutes * 60);
}

public static function delayForHours(int $hours): self
{
return self::delayForMinutes($hours * 60);
}
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php
@@ -0,0 +1,39 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Stamp\DelayStamp;

/**
* @author Yanick Witschi <yanick.witschi@terminal42.ch>
*/
class DelayStampTest extends TestCase
{
public function testSeconds()
{
$stamp = DelayStamp::delayForSeconds(30);
$this->assertSame(30000, $stamp->getDelay());
}

public function testMinutes()
{
$stamp = DelayStamp::delayForMinutes(30);
$this->assertSame(1800000, $stamp->getDelay());
}

public function testHours()
{
$stamp = DelayStamp::delayForHours(30);
$this->assertSame(108000000, $stamp->getDelay());
}
}

0 comments on commit 9cd3e67

Please sign in to comment.