13
13
14
14
use Symfony \Component \Messenger \Envelope ;
15
15
use Symfony \Component \Messenger \Exception \LogicException ;
16
+ use Symfony \Component \Messenger \Stamp \DelayStamp ;
16
17
use Symfony \Component \Messenger \Stamp \TransportMessageIdStamp ;
17
18
use Symfony \Component \Messenger \Transport \Serialization \SerializerInterface ;
18
19
use Symfony \Component \Messenger \Transport \TransportInterface ;
@@ -47,6 +48,7 @@ class InMemoryTransport implements TransportInterface, ResetInterface
47
48
48
49
private int $ nextId = 1 ;
49
50
private ?SerializerInterface $ serializer ;
51
+ private array $ availableAt = [];
50
52
51
53
public function __construct (SerializerInterface $ serializer = null )
52
54
{
@@ -55,7 +57,15 @@ public function __construct(SerializerInterface $serializer = null)
55
57
56
58
public function get (): iterable
57
59
{
58
- return array_values ($ this ->decode ($ this ->queue ));
60
+ $ envelopes = [];
61
+ $ now = new \DateTimeImmutable ();
62
+ foreach ($ this ->decode ($ this ->queue ) as $ id => $ envelope ) {
63
+ if (!isset ($ this ->availableAt [$ id ]) || $ now > $ this ->availableAt [$ id ]) {
64
+ $ envelopes [] = $ envelope ;
65
+ }
66
+ }
67
+
68
+ return $ envelopes ;
59
69
}
60
70
61
71
public function ack (Envelope $ envelope ): void
@@ -66,7 +76,7 @@ public function ack(Envelope $envelope): void
66
76
throw new LogicException ('No TransportMessageIdStamp found on the Envelope. ' );
67
77
}
68
78
69
- unset($ this ->queue [$ transportMessageIdStamp ->getId ()]);
79
+ unset($ this ->queue [$ id = $ transportMessageIdStamp ->getId ()], $ this -> availableAt [ $ id ]);
70
80
}
71
81
72
82
public function reject (Envelope $ envelope ): void
@@ -77,7 +87,7 @@ public function reject(Envelope $envelope): void
77
87
throw new LogicException ('No TransportMessageIdStamp found on the Envelope. ' );
78
88
}
79
89
80
- unset($ this ->queue [$ transportMessageIdStamp ->getId ()]);
90
+ unset($ this ->queue [$ id = $ transportMessageIdStamp ->getId ()], $ this -> availableAt [ $ id ]);
81
91
}
82
92
83
93
public function send (Envelope $ envelope ): Envelope
@@ -88,6 +98,11 @@ public function send(Envelope $envelope): Envelope
88
98
$ this ->sent [] = $ encodedEnvelope ;
89
99
$ this ->queue [$ id ] = $ encodedEnvelope ;
90
100
101
+ /** @var DelayStamp|null $delayStamp */
102
+ if ($ delayStamp = $ envelope ->last (DelayStamp::class)) {
103
+ $ this ->availableAt [$ id ] = (new \DateTimeImmutable ())->modify (sprintf ('+%d seconds ' , $ delayStamp ->getDelay () / 1000 ));
104
+ }
105
+
91
106
return $ envelope ;
92
107
}
93
108
0 commit comments