Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the withChannelName() method from QueueInterface #225

Merged
merged 8 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function runExisting(callable $handlerCallback): void;
/**
* Returns status code of a message with the given id.
*
* @param string $id ID of a job message.
* @param int|string $id ID of a job message.
*
* @throws InvalidArgumentException When there is no such id in the adapter.
*
Expand All @@ -41,4 +41,6 @@ public function push(MessageInterface $message): MessageInterface;
public function subscribe(callable $handlerCallback): void;

public function withChannel(string $channel): self;

public function getChannelName(): string;
}
5 changes: 5 additions & 0 deletions src/Adapter/SynchronousAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public function withChannel(string $channel): self

return $new;
}

public function getChannelName(): string
{
return $this->channel;
}
}
8 changes: 6 additions & 2 deletions src/Debug/QueueCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ public function collectStatus(string $id, JobStatus $status): void
}

public function collectPush(
string $channel,
?string $channel,
MessageInterface $message,
string|array|callable|MiddlewarePushInterface ...$middlewareDefinitions,
): void {
if (!$this->isActive()) {
return;
}
if ($channel === null) {
$channel = 'null';
}

$this->pushes[$channel][] = [
'message' => $message,
'middlewares' => $middlewareDefinitions,
Expand All @@ -69,7 +73,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac
if (!$this->isActive()) {
return;
}
$this->processingMessages[$queue->getChannelName()][] = $message;
$this->processingMessages[$queue->getChannelName() ?? 'null'][] = $message;
}

private function reset(): void
Expand Down
11 changes: 2 additions & 9 deletions src/Debug/QueueDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
public function status(string|int $id): JobStatus
{
$result = $this->queue->status($id);
$this->collector->collectStatus($id, $result);
$this->collector->collectStatus((string) $id, $result);

return $result;
}
Expand Down Expand Up @@ -50,15 +50,8 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface
return new self($this->queue->withAdapter($adapter), $this->collector);
}

public function getChannelName(): string
public function getChannelName(): ?string
{
return $this->queue->getChannelName();
}

public function withChannelName(string $channel): QueueInterface
{
$new = clone $this;
$new->queue = $this->queue->withChannelName($channel);
return $new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function dispatch(
FailureHandlingRequest $request,
MessageFailureHandlerInterface $finishHandler
): FailureHandlingRequest {
/** @var string $channelName It is always string in this context */
$channelName = $request->getQueue()->getChannelName();
if (!isset($this->middlewareDefinitions[$channelName]) || $this->middlewareDefinitions[$channelName] === []) {
$channelName = self::DEFAULT_PIPELINE;
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/AdapterFactoryQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function getOrTryToCreate(string $channel): QueueInterface|null
),
);
}
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter)->withChannelName($channel);
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter->withChannel($channel));
} else {
$this->queues[$channel] = null;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Provider/PrototypeQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Provider;

use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\QueueInterface;

/**
Expand All @@ -17,12 +18,13 @@ final class PrototypeQueueProvider implements QueueProviderInterface
*/
public function __construct(
private readonly QueueInterface $baseQueue,
private readonly AdapterInterface $baseAdapter,
) {
}

public function get(string $channel): QueueInterface
{
return $this->baseQueue->withChannelName($channel);
return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel));
}

public function has(string $channel): bool
Expand Down
13 changes: 2 additions & 11 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ public function __construct(
private LoggerInterface $logger,
private PushMiddlewareDispatcher $pushMiddlewareDispatcher,
private ?AdapterInterface $adapter = null,
private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME,
MiddlewarePushInterface|callable|array|string ...$middlewareDefinitions
) {
$this->middlewareDefinitions = $middlewareDefinitions;
$this->adapterPushHandler = new AdapterPushHandler();
}

public function getChannelName(): string
public function getChannelName(): ?string
{
return $this->channelName;
return $this->adapter?->getChannelName();
}

public function push(
Expand Down Expand Up @@ -136,14 +135,6 @@ public function withMiddlewaresAdded(MiddlewarePushInterface|callable|array|stri
return $instance;
}

public function withChannelName(string $channel): self
{
$instance = clone $this;
$instance->channelName = $channel;
$instance->adapter = $this->adapter?->withChannel($channel);
return $instance;
}

private function handle(MessageInterface $message): bool
{
$this->worker->process($message, $this);
Expand Down
6 changes: 2 additions & 4 deletions src/QueueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function run(int $max = 0): int;
public function listen(): void;

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

public function withAdapter(AdapterInterface $adapter): self;

public function getChannelName(): string;

public function withChannelName(string $channel): self;
public function getChannelName(): ?string;
}
15 changes: 14 additions & 1 deletion stubs/StubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Enum\JobStatus;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\QueueInterface;

/**
* Stub adapter that does nothing. Job status is always "done".
*/
final class StubAdapter implements AdapterInterface
{
public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME)
{
}

public function runExisting(callable $handlerCallback): void
{
}
Expand All @@ -33,6 +38,14 @@ public function subscribe(callable $handlerCallback): void

public function withChannel(string $channel): AdapterInterface
{
return clone $this;
$new = clone $this;
$new->channelName = $channel;

return $new;
}

public function getChannelName(): string
{
return $this->channelName;
}
}
21 changes: 5 additions & 16 deletions stubs/StubQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
final class StubQueue implements QueueInterface
{
public function __construct(
private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME,
private ?AdapterInterface $adapter = null,
) {
public function __construct(private ?AdapterInterface $adapter = null)
{
}

public function push(
Expand Down Expand Up @@ -51,21 +49,12 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface
{
$new = clone $this;
$new->adapter = $adapter;
return $new;
}

public function getChannelName(): string
{
return $this->channelName;
return $new;
}

public function withChannelName(string $channel): QueueInterface
public function getChannelName(): ?string
{
$new = clone $this;
$new->channelName = $channel;
if ($new->adapter !== null) {
$new->adapter = $new->adapter->withChannel($channel);
}
return $new;
return $this->adapter?->getChannelName();
}
}
5 changes: 0 additions & 5 deletions tests/App/DummyQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,4 @@ public function getChannelName(): string
{
return $this->channelName;
}

public function withChannelName(string $channel): QueueInterface
{
throw new Exception('`withChannelName()` method is not implemented yet.');
}
}
5 changes: 5 additions & 0 deletions tests/App/FakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function withChannel(string $channel): AdapterInterface

return $instance;
}

public function getChannelName(): string
{
return $this->channel;
}
}
5 changes: 5 additions & 0 deletions tests/Benchmark/Support/VoidAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function withChannel(string $channel): AdapterInterface
{
throw new RuntimeException('Method is not implemented');
}

public function getChannelName(): string
{
throw new RuntimeException('Method is not implemented');
}
}
14 changes: 0 additions & 14 deletions tests/Unit/Debug/QueueDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,12 @@ public function testGetChannelName(): void
$this->assertEquals('getChannelName', $decorator->getChannelName());
}

public function testWithChannelName(): void
{
$queue = $this->createMock(QueueInterface::class);
$queue->expects($this->once())->method('withChannelName')->willReturn($queue);
$collector = new QueueCollector();
$decorator = new QueueDecorator(
$queue,
$collector,
);

$this->assertInstanceOf(QueueInterface::class, $decorator->withChannelName('test'));
}

public function testImmutable(): void
{
$queueDecorator = new QueueDecorator(
$this->createMock(QueueInterface::class),
new QueueCollector()
);
$this->assertNotSame($queueDecorator, $queueDecorator->withAdapter(new FakeAdapter()));
$this->assertNotSame($queueDecorator, $queueDecorator->withChannelName('test'));
}
}
7 changes: 5 additions & 2 deletions tests/Unit/Provider/CompositeQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class CompositeQueueProviderTest extends TestCase
{
public function testBase(): void
{
$queue = new StubQueue('channel');
$queue = new StubQueue(new StubAdapter());
$provider = new CompositeQueueProvider(
new AdapterFactoryQueueProvider(
$queue,
Expand All @@ -38,7 +38,10 @@ public function testBase(): void
public function testNotFound(): void
{
$provider = new CompositeQueueProvider(
new AdapterFactoryQueueProvider(new StubQueue('channel'), ['channel1' => new StubAdapter()]),
new AdapterFactoryQueueProvider(
new StubQueue(new StubAdapter()),
['channel1' => new StubAdapter()]
),
);

$this->expectException(ChannelNotFoundException::class);
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Provider/PrototypeQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Provider\PrototypeQueueProvider;
use Yiisoft\Queue\Stubs\StubAdapter;
use Yiisoft\Queue\Stubs\StubQueue;

final class PrototypeQueueProviderTest extends TestCase
Expand All @@ -14,6 +15,7 @@ public function testBase(): void
{
$provider = new PrototypeQueueProvider(
new StubQueue(),
new StubAdapter(),
);

$queue = $provider->get('test-channel');
Expand Down
14 changes: 1 addition & 13 deletions tests/Unit/Stubs/StubQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\Stubs\StubQueue;
use Yiisoft\Queue\Stubs\StubAdapter;

Expand All @@ -20,7 +19,7 @@ public function testBase(): void
$this->assertSame($message, $queue->push($message));
$this->assertSame(0, $queue->run());
$this->assertTrue($queue->status('test')->isDone());
$this->assertSame(QueueInterface::DEFAULT_CHANNEL_NAME, $queue->getChannelName());
$this->assertNull($queue->getChannelName());
$this->assertNull($queue->getAdapter());
$queue->listen();
}
Expand All @@ -34,15 +33,4 @@ public function testWithAdapter(): void
$this->assertNotSame($queue, $sourceQueue);
$this->assertInstanceOf(StubAdapter::class, $queue->getAdapter());
}

public function testWithChannelName(): void
{
$sourceQueue = new StubQueue();

$queue = $sourceQueue->withChannelName('test');

$this->assertNotSame($queue, $sourceQueue);
$this->assertSame(QueueInterface::DEFAULT_CHANNEL_NAME, $sourceQueue->getChannelName());
$this->assertSame('test', $queue->getChannelName());
}
}
Loading