|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FiveLab Amqp package |
| 5 | + * |
| 6 | + * (c) FiveLab |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types = 1); |
| 13 | + |
| 14 | +namespace FiveLab\Component\Amqp; |
| 15 | + |
| 16 | +use FiveLab\Component\Amqp\Adapter\Amqp\Channel\AmqpChannelFactory; |
| 17 | +use FiveLab\Component\Amqp\Adapter\Amqp\Connection\AmqpConnectionFactory; |
| 18 | +use FiveLab\Component\Amqp\Adapter\Amqp\Exchange\AmqpExchangeFactory; |
| 19 | +use FiveLab\Component\Amqp\Adapter\Amqp\Queue\AmqpQueueFactory; |
| 20 | +use FiveLab\Component\Amqp\Channel\ChannelFactoryInterface; |
| 21 | +use FiveLab\Component\Amqp\Channel\Definition\ChannelDefinition; |
| 22 | +use FiveLab\Component\Amqp\Connection\ConnectionFactoryInterface; |
| 23 | +use FiveLab\Component\Amqp\Connection\Dsn; |
| 24 | +use FiveLab\Component\Amqp\Exchange\Definition\ExchangeDefinition; |
| 25 | +use FiveLab\Component\Amqp\Exchange\ExchangeFactoryInterface; |
| 26 | +use FiveLab\Component\Amqp\Publisher\Middleware\PublisherMiddlewares; |
| 27 | +use FiveLab\Component\Amqp\Publisher\Publisher; |
| 28 | +use FiveLab\Component\Amqp\Publisher\PublisherInterface; |
| 29 | +use FiveLab\Component\Amqp\Queue\Definition\QueueDefinition; |
| 30 | +use FiveLab\Component\Amqp\Queue\QueueFactoryInterface; |
| 31 | + |
| 32 | +class AmqpBuilder |
| 33 | +{ |
| 34 | + private readonly Dsn $dsn; |
| 35 | + private ?ConnectionFactoryInterface $connectionFactory = null; |
| 36 | + private ?ChannelFactoryInterface $channelFactory = null; |
| 37 | + |
| 38 | + public function __construct(Dsn|string $dsn) |
| 39 | + { |
| 40 | + if (\is_string($dsn)) { |
| 41 | + $dsn = Dsn::fromDsn($dsn); |
| 42 | + } |
| 43 | + |
| 44 | + $this->dsn = $dsn; |
| 45 | + } |
| 46 | + |
| 47 | + public function createConnection(): ConnectionFactoryInterface |
| 48 | + { |
| 49 | + if (!$this->connectionFactory) { |
| 50 | + $this->connectionFactory = new AmqpConnectionFactory($this->dsn); |
| 51 | + } |
| 52 | + |
| 53 | + return $this->connectionFactory; |
| 54 | + } |
| 55 | + |
| 56 | + public function createChannel(): ChannelFactoryInterface |
| 57 | + { |
| 58 | + if (!$this->channelFactory) { |
| 59 | + $this->channelFactory = new AmqpChannelFactory($this->createConnection(), new ChannelDefinition()); |
| 60 | + } |
| 61 | + |
| 62 | + return $this->channelFactory; |
| 63 | + } |
| 64 | + |
| 65 | + public function createExchange(ExchangeDefinition $definition): ExchangeFactoryInterface |
| 66 | + { |
| 67 | + return new AmqpExchangeFactory($this->createChannel(), $definition); |
| 68 | + } |
| 69 | + |
| 70 | + public function createPublisher(ExchangeDefinition $definition, ?PublisherMiddlewares $middlewares = null): PublisherInterface |
| 71 | + { |
| 72 | + $exchange = $this->createExchange($definition); |
| 73 | + |
| 74 | + return new Publisher($exchange, $middlewares ?: new PublisherMiddlewares()); |
| 75 | + } |
| 76 | + |
| 77 | + public function createQueue(QueueDefinition $definition): QueueFactoryInterface |
| 78 | + { |
| 79 | + $channel = $this->createChannel(); |
| 80 | + |
| 81 | + return new AmqpQueueFactory($channel, $definition); |
| 82 | + } |
| 83 | +} |
0 commit comments