44
55namespace Yiisoft \Queue \Redis ;
66
7+ use BackedEnum ;
78use Yiisoft \Queue \Adapter \AdapterInterface ;
89use Yiisoft \Queue \Cli \LoopInterface ;
9- use Yiisoft \Queue \Enum \ JobStatus ;
10+ use Yiisoft \Queue \JobStatus ;
1011use Yiisoft \Queue \Message \IdEnvelope ;
1112use Yiisoft \Queue \Message \MessageInterface ;
1213use Yiisoft \Queue \Message \MessageSerializerInterface ;
1314
1415final class Adapter implements AdapterInterface
1516{
1617 public function __construct (
17- private QueueProviderInterface $ provider ,
18+ private QueueProviderInterface $ provider ,
1819 private MessageSerializerInterface $ serializer ,
19- private LoopInterface $ loop ,
20- private int $ timeout = 3
21- ) {
20+ private LoopInterface $ loop ,
21+ private int $ timeout = 3
22+ )
23+ {
2224 }
2325
2426 public function runExisting (callable $ handlerCallback ): void
@@ -45,23 +47,21 @@ public function status(int|string $id): JobStatus
4547 }
4648
4749 if ($ this ->provider ->existInReserved ($ id )) {
48- return JobStatus::reserved () ;
50+ return JobStatus::RESERVED ;
4951 }
5052
5153 if ($ this ->provider ->existInWaiting ($ id )) {
52- return JobStatus::waiting () ;
54+ return JobStatus::WAITING ;
5355 }
5456
55- return JobStatus::done () ;
57+ return JobStatus::DONE ;
5658 }
5759
5860 public function push (MessageInterface $ message ): MessageInterface
5961 {
6062 $ payload = $ this ->serializer ->serialize ($ message );
6163 $ id = $ this ->provider ->pushMessage ($ payload , $ message ->getMetadata ());
62- $ envelope = IdEnvelope::fromMessage ($ message );
63- $ envelope ->setId ($ id );
64- return $ envelope ;
64+ return new IdEnvelope ($ message , $ id );
6565 }
6666
6767 public function subscribe (callable $ handlerCallback ): void
@@ -79,10 +79,11 @@ public function subscribe(callable $handlerCallback): void
7979 }
8080 }
8181
82- public function withChannel (string $ channel ): AdapterInterface
82+ public function withChannel (BackedEnum | string $ channel ): AdapterInterface
8383 {
8484 $ adapter = clone $ this ;
85- $ adapter ->provider = $ this ->provider ->withChannelName ($ channel );
85+ $ channelName = is_string ($ channel ) ? $ channel : (string ) $ channel ->value ;
86+ $ adapter ->provider = $ this ->provider ->withChannelName ($ channelName );
8687 return $ adapter ;
8788 }
8889
@@ -94,9 +95,11 @@ private function reserve(): ?IdEnvelope
9495 }
9596
9697 $ message = $ this ->serializer ->unserialize ($ reserve ->payload );
97- $ envelope = IdEnvelope:: fromMessage ($ message );
98- $ envelope -> setId ( $ reserve -> id );
98+ return new IdEnvelope ($ message, $ reserve -> id );
99+ }
99100
100- return $ envelope ;
101+ public function getChannel (): string
102+ {
103+ return $ this ->provider ->getChannelName ();
101104 }
102105}
0 commit comments