88use Psr \Log \LoggerInterface ;
99use Yiisoft \Queue \Adapter \AdapterInterface ;
1010use Yiisoft \Queue \Cli \LoopInterface ;
11- use Yiisoft \Queue \Exception \AdapterConfiguration \AdapterNotConfiguredException ;
1211use Yiisoft \Queue \Message \MessageInterface ;
1312use Yiisoft \Queue \Middleware \Push \AdapterPushHandler ;
1413use Yiisoft \Queue \Middleware \Push \MessageHandlerPushInterface ;
@@ -29,12 +28,12 @@ final class Queue implements QueueInterface
2928 private string $ name ;
3029
3130 public function __construct (
31+ private readonly AdapterInterface $ adapter ,
3232 private readonly WorkerInterface $ worker ,
3333 private readonly LoopInterface $ loop ,
3434 private readonly LoggerInterface $ logger ,
3535 private readonly PushMiddlewareDispatcher $ pushMiddlewareDispatcher ,
3636 string |BackedEnum $ name = QueueProviderInterface::DEFAULT_QUEUE ,
37- private ?AdapterInterface $ adapter = null ,
3837 MiddlewarePushInterface |callable |array |string ...$ middlewareDefinitions ,
3938 ) {
4039 $ this ->name = StringNormalizer::normalize ($ name );
@@ -51,7 +50,6 @@ public function push(
5150 MessageInterface $ message ,
5251 MiddlewarePushInterface |callable |array |string ...$ middlewareDefinitions ,
5352 ): MessageInterface {
54- $ this ->checkAdapter ();
5553 $ this ->logger ->debug (
5654 'Preparing to push message with handler name "{handlerName}". ' ,
5755 ['handlerName ' => $ message ->getHandlerName ()],
@@ -74,8 +72,6 @@ public function push(
7472
7573 public function run (int $ max = 0 ): int
7674 {
77- $ this ->checkAdapter ();
78-
7975 $ this ->logger ->debug ('Start processing queue messages. ' );
8076 $ count = 0 ;
8177
@@ -100,30 +96,16 @@ public function run(int $max = 0): int
10096
10197 public function listen (): void
10298 {
103- $ this ->checkAdapter ();
104-
10599 $ this ->logger ->info ('Start listening to the queue. ' );
106100 $ this ->adapter ->subscribe (fn (MessageInterface $ message ) => $ this ->handle ($ message ));
107101 $ this ->logger ->info ('Finish listening to the queue. ' );
108102 }
109103
110104 public function status (string |int $ id ): JobStatus
111105 {
112- $ this ->checkAdapter ();
113106 return $ this ->adapter ->status ($ id );
114107 }
115108
116- public function withAdapter (AdapterInterface $ adapter , string |BackedEnum |null $ queueName = null ): static
117- {
118- $ new = clone $ this ;
119- $ new ->adapter = $ adapter ;
120- if ($ queueName !== null ) {
121- $ new ->name = StringNormalizer::normalize ($ queueName );
122- }
123-
124- return $ new ;
125- }
126-
127109 public function withMiddlewares (MiddlewarePushInterface |callable |array |string ...$ middlewareDefinitions ): self
128110 {
129111 $ instance = clone $ this ;
@@ -147,16 +129,6 @@ private function handle(MessageInterface $message): bool
147129 return $ this ->loop ->canContinue ();
148130 }
149131
150- /**
151- * @psalm-assert AdapterInterface $this->adapter
152- */
153- private function checkAdapter (): void
154- {
155- if ($ this ->adapter === null ) {
156- throw new AdapterNotConfiguredException ();
157- }
158- }
159-
160132 private function createPushHandler (MiddlewarePushInterface |callable |array |string ...$ middlewares ): MessageHandlerPushInterface
161133 {
162134 return new class (
0 commit comments