Skip to content

Commit

Permalink
Bind to all or specified multiple events (#115)
Browse files Browse the repository at this point in the history
* Allow to bind to all or multiple events

* Documentation

* Refactoring

* Fix topic creation method call

* Fix typos

---------

Co-authored-by: Eugene Kirdzei <e.ki@nuwber.com>
  • Loading branch information
masterjus and eugene-nuwber authored Jul 11, 2023
1 parent a398ff8 commit 1c99041
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 267 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
run: phpunit -c phpunit.xml.dist

- name: Upload Codecov coverage
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
file: './coverage.xml'
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Once again, the RabbitEvents library helps you to publish an event and handle it
1. [Upgrade from 7.x to 8.x](#upgrade_7.x-8.x)
1. [Publisher component](#publisher)
1. [Listener component](#listener)
1. [Examples](./examples)
1. [Non-standard use](#non-standard-use)

## Installation via Composer<a name="installation"></a>
Expand Down Expand Up @@ -118,5 +119,5 @@ If you're using only one of parts of RabbitEvents, you should know a few things:
]
```

There'e 3 elements of an array, so 3 variables will be passed to a Listener (array, string and integer).
There's 3 elements of an array, so 3 variables will be passed to a Listener (array, string and integer).
If an associative array is being passed, the Dispatcher wraps this array by itself.
20 changes: 11 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage includeUncoveredFiles="false" ignoreDeprecatedCodeUnits="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix="ServiceProvider.php">src</directory>
<directory suffix="Exception.php">src</directory>
<directory>src/Rabbitevents/*/Commands</directory>
</exclude>
<report>
<text outputFile="php://stdout" showUncoveredFiles="false" showOnlySummary="true"/>
<clover outputFile="coverage.xml"/>
Expand All @@ -20,4 +12,14 @@
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix="ServiceProvider.php">src</directory>
<directory suffix="Exception.php">src</directory>
<directory>src/Rabbitevents/*/Commands</directory>
</exclude>
</source>
</phpunit>
22 changes: 0 additions & 22 deletions src/RabbitEvents/Foundation/Amqp/BindFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
use Interop\Amqp\AmqpTopic;
use RabbitEvents\Foundation\Context;

class TopicDestinationFactory
class DestinationTopicFactory
{
public function __construct(private Context $context)
public function __construct(private readonly Context $context)
{
}

public function make(): AmqpTopic
public function makeAndDeclare(string $name): AmqpTopic
{
$topic = $this->context->createTopic(
$this->context->connection()->getConfig('exchange')
);
$topic = $this->context->createTopic($name);

$topic->setType(AmqpTopic::TYPE_TOPIC);
$topic->addFlag(AmqpDestination::FLAG_DURABLE);

Expand Down
20 changes: 5 additions & 15 deletions src/RabbitEvents/Foundation/Amqp/QueueFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,26 @@
use Interop\Amqp\AmqpDestination;
use Interop\Amqp\AmqpQueue;
use RabbitEvents\Foundation\Context;
use RabbitEvents\Foundation\Contracts\QueueName as QueueNameInterface;
use RabbitEvents\Foundation\Support\QueueName;
use RabbitEvents\Foundation\Support\EnqueueOptions;

class QueueFactory
{
public function __construct(private Context $context)
public function __construct(private readonly Context $context)
{
}

/**
* @param QueueNameInterface|string $queueName
* @param EnqueueOptions $enqueueOptions
* @return AmqpQueue
*/
public function make(QueueNameInterface|string $queueName): AmqpQueue
public function makeAndDeclare(EnqueueOptions $enqueueOptions): AmqpQueue
{
$queue = $this->context->createQueue($this->resolveName($queueName));
$queue = $this->context->createQueue($enqueueOptions->name);

$queue->addFlag(AmqpDestination::FLAG_DURABLE);

$this->context->declareQueue($queue);

return $queue;
}

protected function resolveName(QueueNameInterface|string $queueName): string
{
if (is_string($queueName)) {
$queueName = new QueueName(env('APP_NAME', 'rabbitevents-app'), $queueName);
}

return $queueName->resolve();
}
}
71 changes: 18 additions & 53 deletions src/RabbitEvents/Foundation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,50 @@
use Interop\Amqp\AmqpQueue;
use Interop\Amqp\AmqpContext;
use Interop\Amqp\AmqpTopic;
use RabbitEvents\Foundation\Amqp\BindFactory;
use RabbitEvents\Foundation\Amqp\TopicDestinationFactory;
use Interop\Amqp\Impl\AmqpBind;
use RabbitEvents\Foundation\Amqp\DestinationTopicFactory;
use RabbitEvents\Foundation\Amqp\QueueFactory;
use RabbitEvents\Foundation\Contracts\QueueName;
use RabbitEvents\Foundation\Contracts\Transport;
use RabbitEvents\Foundation\Support\EnqueueOptions;

/**
* @mixin \Enqueue\AmqpLib\AmqpContext
*/
class Context
{
/**
* @var Transport
* @var AmqpContext
*/
private $sender;
private AmqpContext $amqpContext;

/**
* @var AmqpTopic
*/
private $topic;

/**
* @param AmqpContext
*/
private $amqpContext;

public function __construct(private Connection $connection)
public function __construct(public readonly Connection $connection)
{
$this->amqpContext = $this->connection->createContext();
}

public function __call(string $method, ?array $args)
{
return $this->amqpContext()->$method(...$args);
return $this->amqpContext->$method(...$args);
}

private function amqpContext(): AmqpContext
public function makeTopic(): AmqpTopic
{
if (!$this->amqpContext) {
$this->amqpContext = $this->connection->createContext();
}

return $this->amqpContext;
return (new DestinationTopicFactory($this))
->makeAndDeclare($this->connection->getConfig('exchange'));
}

public function topic(): AmqpTopic
public function makeConsumer(AmqpQueue $queue): Consumer
{
if (!$this->topic) {
$this->topic = (new TopicDestinationFactory($this))->make();
}

return $this->topic;
}

public function makeConsumer(AmqpQueue $queue, string $event): Consumer
{
$this->bind($queue, $event);

return new Consumer($this->createConsumer($queue));
}

public function makeQueue(QueueName $queueName): AmqpQueue
public function makeQueue(AmqpTopic $topic, EnqueueOptions $enqueueOptions): AmqpQueue
{
return (new QueueFactory($this))->make($queueName);
}
$queue = (new QueueFactory($this))->makeAndDeclare($enqueueOptions);

/**
* @param AmqpQueue $queue
* @param string $event
* @return void
*/
private function bind(AmqpQueue $queue, string $event): void
{
$this->amqpContext()->bind(
(new BindFactory())->make($this->topic(), $queue, $event)
);
}
foreach ($enqueueOptions->events as $event) {
$this->bind(new AmqpBind($topic, $queue, $event));
}

public function connection(): Connection
{
return $this->connection;
return $queue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace RabbitEvents\Foundation\Contracts;

interface QueueName
interface QueueNameInterface
{
/**
* Return a queue name event bound to
*
* @return string
*/
public function resolve(): string;
public function resolveQueueName(): string;
}
4 changes: 3 additions & 1 deletion src/RabbitEvents/Foundation/RabbitEventsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Interop\Queue\Topic;
use RabbitEvents\Foundation\Commands\InstallCommand;
use RabbitEvents\Foundation\Support\Sender;

class RabbitEventsServiceProvider extends ServiceProvider
{
Expand All @@ -22,6 +22,8 @@ public function boot(): void
Context::class,
static fn($app) => new Context(new Connection($config))
);

$this->app->singleton(Topic::class);
}

public function register(): void
Expand Down
22 changes: 22 additions & 0 deletions src/RabbitEvents/Foundation/Support/EnqueueOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace RabbitEvents\Foundation\Support;

use RabbitEvents\Foundation\Contracts\QueueNameInterface;

class EnqueueOptions implements QueueNameInterface
{
public readonly string $name;

public function __construct(private readonly string $applicationName, public readonly array $events)
{
$this->name = $this->resolveQueueName();
}

public function resolveQueueName(): string
{
return $this->applicationName . ":" . implode(',', $this->events);
}
}
19 changes: 0 additions & 19 deletions src/RabbitEvents/Foundation/Support/QueueName.php

This file was deleted.

Loading

0 comments on commit 1c99041

Please sign in to comment.