|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace IntegrationTesting\Driver; |
| 4 | + |
| 5 | +use PhpAmqpLib\Channel\AMQPChannel; |
| 6 | +use PhpAmqpLib\Connection\AMQPStreamConnection; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class AMQPConnection |
| 10 | + * @package IntegrationTesting\Driver |
| 11 | + * @internal NOT FOR PUBLIC USE |
| 12 | + */ |
| 13 | +class AMQPConnection |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var AMQPStreamConnection |
| 17 | + */ |
| 18 | + private $connection; |
| 19 | + |
| 20 | + private function __construct( |
| 21 | + AMQPStreamConnection $connection |
| 22 | + ) { |
| 23 | + $this->connection = $connection; |
| 24 | + $this->connection->set_close_on_destruct(true); |
| 25 | + } |
| 26 | + |
| 27 | + public static function create( |
| 28 | + string $host, |
| 29 | + int $port, |
| 30 | + string $user, |
| 31 | + string $password, |
| 32 | + string $vHost = '/', |
| 33 | + bool $insist = false, |
| 34 | + string $loginMethod = 'AMQPLAIN', |
| 35 | + string $locale = 'en_US', |
| 36 | + float $connectionTimeout = 3.0, |
| 37 | + float $readWriteTimeout = 3.0, |
| 38 | + bool $keepAlive = false, |
| 39 | + int $heartbeat = 0 |
| 40 | + ): self { |
| 41 | + return new self( |
| 42 | + new AMQPStreamConnection( |
| 43 | + $host, |
| 44 | + $port, |
| 45 | + $user, |
| 46 | + $password, |
| 47 | + $vHost, |
| 48 | + $insist, |
| 49 | + $loginMethod, |
| 50 | + null, |
| 51 | + $locale, |
| 52 | + $connectionTimeout, |
| 53 | + $readWriteTimeout, |
| 54 | + null, |
| 55 | + $keepAlive, |
| 56 | + $heartbeat |
| 57 | + ) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function connect(): void |
| 62 | + { |
| 63 | + if (!$this->connection->isConnected()) { |
| 64 | + $this->connection->reconnect(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public function disconnect(): void |
| 69 | + { |
| 70 | + if ($this->connection->isConnected()) { |
| 71 | + $this->connection->close(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param int $channelId |
| 77 | + * @return AMQPChannel |
| 78 | + */ |
| 79 | + public function getChannel(int $channelId = null): AMQPChannel |
| 80 | + { |
| 81 | + return $this->connection->channel($channelId); |
| 82 | + } |
| 83 | +} |
0 commit comments