Skip to content

Commit 552564e

Browse files
Remove the withChannelName() method from QueueInterface (#225)
* Remove the withChannelName() method from QueueInterface * Apply fixes from StyleCI * Bugfix * Bugfix * Apply fixes from StyleCI * Bugfix * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 3602957 commit 552564e

18 files changed

Lines changed: 62 additions & 80 deletions

src/Adapter/AdapterInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function runExisting(callable $handlerCallback): void;
2020
/**
2121
* Returns status code of a message with the given id.
2222
*
23-
* @param string $id ID of a job message.
23+
* @param int|string $id ID of a job message.
2424
*
2525
* @throws InvalidArgumentException When there is no such id in the adapter.
2626
*
@@ -41,4 +41,6 @@ public function push(MessageInterface $message): MessageInterface;
4141
public function subscribe(callable $handlerCallback): void;
4242

4343
public function withChannel(string $channel): self;
44+
45+
public function getChannelName(): string;
4446
}

src/Adapter/SynchronousAdapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,9 @@ public function withChannel(string $channel): self
8686

8787
return $new;
8888
}
89+
90+
public function getChannelName(): string
91+
{
92+
return $this->channel;
93+
}
8994
}

src/Debug/QueueCollector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ public function collectStatus(string $id, JobStatus $status): void
5151
}
5252

5353
public function collectPush(
54-
string $channel,
54+
?string $channel,
5555
MessageInterface $message,
5656
string|array|callable|MiddlewarePushInterface ...$middlewareDefinitions,
5757
): void {
5858
if (!$this->isActive()) {
5959
return;
6060
}
61+
if ($channel === null) {
62+
$channel = 'null';
63+
}
64+
6165
$this->pushes[$channel][] = [
6266
'message' => $message,
6367
'middlewares' => $middlewareDefinitions,
@@ -69,7 +73,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac
6973
if (!$this->isActive()) {
7074
return;
7175
}
72-
$this->processingMessages[$queue->getChannelName()][] = $message;
76+
$this->processingMessages[$queue->getChannelName() ?? 'null'][] = $message;
7377
}
7478

7579
private function reset(): void

src/Debug/QueueDecorator.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(
2121
public function status(string|int $id): JobStatus
2222
{
2323
$result = $this->queue->status($id);
24-
$this->collector->collectStatus($id, $result);
24+
$this->collector->collectStatus((string) $id, $result);
2525

2626
return $result;
2727
}
@@ -50,15 +50,8 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface
5050
return new self($this->queue->withAdapter($adapter), $this->collector);
5151
}
5252

53-
public function getChannelName(): string
53+
public function getChannelName(): ?string
5454
{
5555
return $this->queue->getChannelName();
5656
}
57-
58-
public function withChannelName(string $channel): QueueInterface
59-
{
60-
$new = clone $this;
61-
$new->queue = $this->queue->withChannelName($channel);
62-
return $new;
63-
}
6457
}

src/Middleware/FailureHandling/FailureMiddlewareDispatcher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function dispatch(
3737
FailureHandlingRequest $request,
3838
MessageFailureHandlerInterface $finishHandler
3939
): FailureHandlingRequest {
40+
/** @var string $channelName It is always string in this context */
4041
$channelName = $request->getQueue()->getChannelName();
4142
if (!isset($this->middlewareDefinitions[$channelName]) || $this->middlewareDefinitions[$channelName] === []) {
4243
$channelName = self::DEFAULT_PIPELINE;

src/Provider/AdapterFactoryQueueProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function getOrTryToCreate(string $channel): QueueInterface|null
8585
),
8686
);
8787
}
88-
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter)->withChannelName($channel);
88+
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter->withChannel($channel));
8989
} else {
9090
$this->queues[$channel] = null;
9191
}

src/Provider/PrototypeQueueProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Queue\Provider;
66

7+
use Yiisoft\Queue\Adapter\AdapterInterface;
78
use Yiisoft\Queue\QueueInterface;
89

910
/**
@@ -17,12 +18,13 @@ final class PrototypeQueueProvider implements QueueProviderInterface
1718
*/
1819
public function __construct(
1920
private readonly QueueInterface $baseQueue,
21+
private readonly AdapterInterface $baseAdapter,
2022
) {
2123
}
2224

2325
public function get(string $channel): QueueInterface
2426
{
25-
return $this->baseQueue->withChannelName($channel);
27+
return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel));
2628
}
2729

2830
public function has(string $channel): bool

src/Queue.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ public function __construct(
3232
private LoggerInterface $logger,
3333
private PushMiddlewareDispatcher $pushMiddlewareDispatcher,
3434
private ?AdapterInterface $adapter = null,
35-
private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME,
3635
MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions
3736
) {
3837
$this->middlewareDefinitions = $middlewareDefinitions;
3938
$this->adapterPushHandler = new AdapterPushHandler();
4039
}
4140

42-
public function getChannelName(): string
41+
public function getChannelName(): ?string
4342
{
44-
return $this->channelName;
43+
return $this->adapter?->getChannelName();
4544
}
4645

4746
public function push(
@@ -136,14 +135,6 @@ public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|stri
136135
return $instance;
137136
}
138137

139-
public function withChannelName(string $channel): self
140-
{
141-
$instance = clone $this;
142-
$instance->channelName = $channel;
143-
$instance->adapter = $this->adapter?->withChannel($channel);
144-
return $instance;
145-
}
146-
147138
private function handle(MessageInterface $message): bool
148139
{
149140
$this->worker->process($message, $this);

src/QueueInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function run(int $max = 0): int;
3636
public function listen(): void;
3737

3838
/**
39-
* @param string $id A message id
39+
* @param int|string $id A message id
4040
*
4141
* @throws InvalidArgumentException when there is no such id in the adapter
4242
*
@@ -46,7 +46,5 @@ public function status(string|int $id): JobStatus;
4646

4747
public function withAdapter(AdapterInterface $adapter): self;
4848

49-
public function getChannelName(): string;
50-
51-
public function withChannelName(string $channel): self;
49+
public function getChannelName(): ?string;
5250
}

stubs/StubAdapter.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
use Yiisoft\Queue\Adapter\AdapterInterface;
88
use Yiisoft\Queue\Enum\JobStatus;
99
use Yiisoft\Queue\Message\MessageInterface;
10+
use Yiisoft\Queue\QueueInterface;
1011

1112
/**
1213
* Stub adapter that does nothing. Job status is always "done".
1314
*/
1415
final class StubAdapter implements AdapterInterface
1516
{
17+
public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME)
18+
{
19+
}
20+
1621
public function runExisting(callable $handlerCallback): void
1722
{
1823
}
@@ -33,6 +38,14 @@ public function subscribe(callable $handlerCallback): void
3338

3439
public function withChannel(string $channel): AdapterInterface
3540
{
36-
return clone $this;
41+
$new = clone $this;
42+
$new->channelName = $channel;
43+
44+
return $new;
45+
}
46+
47+
public function getChannelName(): string
48+
{
49+
return $this->channelName;
3750
}
3851
}

0 commit comments

Comments
 (0)