From 666e4948a0c0c0ae30f0640b334a9117bee77e80 Mon Sep 17 00:00:00 2001 From: Martijn Smidt Date: Tue, 25 Aug 2020 13:02:18 +0200 Subject: [PATCH] Adding binding management Fixed queue creation Fixed queue retrieval Added Response class with HTTP status codes for readability --- .gitignore | 3 + src/Bindings/Binding.php | 182 ++++++++++++++++++++++++++++++++ src/Bindings/BindingService.php | 127 ++++++++++++++++++++++ src/Queue/QueueService.php | 29 +++-- src/Response.php | 77 ++++++++++++++ src/Vhost/VhostService.php | 7 +- 6 files changed, 412 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100644 src/Bindings/Binding.php create mode 100644 src/Bindings/BindingService.php create mode 100644 src/Response.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e51a97 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.lock +vendor/ +.idea/ \ No newline at end of file diff --git a/src/Bindings/Binding.php b/src/Bindings/Binding.php new file mode 100644 index 0000000..9621912 --- /dev/null +++ b/src/Bindings/Binding.php @@ -0,0 +1,182 @@ +data = $data; + + $this->source = $data['source'] ?? ''; + $this->vhost = $data['vhost'] ?? ''; + $this->destination = $data['destination'] ?? ''; + $this->destinationType = $data['destination_type'] ?? ''; + $this->routing_key = $data['routing_key'] ?? ''; + $this->arguments = $data['arguments'] ?? []; + $this->propertiesKey = $data['properties_key'] ?? ''; + } + + /** + * @return bool + */ + public function isDefaultExchangeBinding() { + if($this->data['source'] === '' && $this->data['destination'] === $this->data['routing_key']) { + return true; + } + + return false; + } + + /** + * @return string + */ + public function getSource(): string { + return $this->source; + } + + /** + * @param string $source + */ + public function setSource(string $source): void { + $this->source = $source; + } + + /** + * @return array + */ + public function getArguments(): ?array { + return $this->arguments; + } + + /** + * @param array $arguments + */ + public function setArguments(?array $arguments): void { + $this->arguments = $arguments; + } + + public function addArguments(string $key, $value): void { + $this->arguments[$key] = $value; + } + + /** + * @return string + */ + public function getVhost(): string { + return $this->vhost; + } + + /** + * @param string $vhost + */ + public function setVhost(string $vhost): void { + $this->vhost = $vhost; + } + + /** + * @return string + */ + public function getDestinationType(): string { + return $this->destinationType; + } + + /** + * @param string $destinationType + */ + public function setDestinationType(string $destinationType): void { + $this->destinationType = $destinationType; + } + + /** + * @return string + */ + public function getDestination(): string { + return $this->destination; + } + + /** + * @param string $destination + */ + public function setDestination(string $destination): void { + $this->destination = $destination; + } + + /** + * @return string + */ + public function getRoutingKey(): string { + return $this->routing_key; + } + + /** + * @param string $routing_key + */ + public function setRoutingKey(string $routing_key): void { + $this->routing_key = $routing_key; + } + + public function getRawData(): array { + return $this->data; + } + + /** + * @return string + */ + public function getPropertiesKey(): string { + return $this->propertiesKey; + } + + /** + * @param string $propertiesKey + */ + public function setPropertiesKey(string $propertiesKey): void { + $this->propertiesKey = $propertiesKey; + } +} \ No newline at end of file diff --git a/src/Bindings/BindingService.php b/src/Bindings/BindingService.php new file mode 100644 index 0000000..efd5fae --- /dev/null +++ b/src/Bindings/BindingService.php @@ -0,0 +1,127 @@ +sendAuthenticatedRequest($context); + } catch(GuzzleException $e) { + return []; + } + + $bindings = []; + + foreach($rawBindings as $rawBinding) { + $binding = new Binding($rawBinding); + $bindings[] = $binding; + } + + return $bindings; + } + + /** + * @param Queue $queue + * + * @return Binding[]|null + * @throws Exception + */ + public function getBindingsByQueue(Queue $queue) { + try { + $rawBindings = $this->sendAuthenticatedRequest('queues/' . $queue->getVhost() . '/' . $queue->getName() . '/bindings'); + } catch(GuzzleException $e) { + return null; + } + + $bindings = []; + + foreach($rawBindings as $rawBinding) { + $bindings[] = new Binding($rawBinding); + } + + return $bindings ?: null; + } + + /** + * @param Binding $binding + * + * @return bool + * @throws GuzzleException + */ + public function createBinding(Binding $binding) { + switch($binding->getDestinationType()) { + case Binding::DESTINATION_TYPE_QUEUE: + $context = 'bindings/' . $binding->getVhost() . '/e/' . $binding->getSource() . '/q/' . $binding->getDestination(); + break; + case Binding::DESTINATION_TYPE_EXCHANGE: + $context = 'bindings/' . $binding->getVhost() . '/e/' . $binding->getSource() . '/e/' . $binding->getDestination(); + break; + default: + throw new Exception('Binding destination type not implemented: ' . $binding->getDestinationType()); + } + + $additionConfig = [ + 'routing_key' => $binding->getRoutingKey(), + 'arguments' => $binding->getArguments() ?: [] + ]; + + try { + $res = $this->sendAuthenticatedRequest($context, 'POST', $additionConfig, false); + } catch(Exception $e){ + var_dump((string) $e->getResponse()->getBody()); + } + if($res->getStatusCode() == Response::HTTP_CREATED || $res->getStatusCode() == Response::HTTP_NO_CONTENT) { + return true; + } + + return false; + } + + /** + * @param Binding $binding + * + * @return bool + * @throws GuzzleException + */ + public function deleteBinding(Binding $binding) { + switch($binding->getDestinationType()) { + case Binding::DESTINATION_TYPE_QUEUE: + $context = 'bindings/' . $binding->getVhost() . '/e/' . $binding->getSource() . '/q/' . $binding->getDestination() . '/' . $binding->getPropertiesKey(); + break; + case Binding::DESTINATION_TYPE_EXCHANGE: + $context = 'bindings/' . $binding->getVhost() . '/e/' . $binding->getSource() . '/e/' . $binding->getDestination() . '/' . $binding->getPropertiesKey(); + break; + default: + throw new Exception('Binding destination type not implemented: ' . $binding->getDestinationType()); + } + + $res = $this->sendAuthenticatedRequest( + $context, + 'DELETE', + [], + false + ); + if($res->getStatusCode() == Response::HTTP_CREATED || $res->getStatusCode() == Response::HTTP_NO_CONTENT) { + return true; + } + + return false; + } + +} \ No newline at end of file diff --git a/src/Queue/QueueService.php b/src/Queue/QueueService.php index 4112859..52472ac 100644 --- a/src/Queue/QueueService.php +++ b/src/Queue/QueueService.php @@ -4,9 +4,11 @@ namespace Squeezely\RabbitMQ\Management\Queue; +use Exception; use GuzzleHttp\Exception\GuzzleException; -use GuzzleHttp\Psr7\Response; +use Psr\Http\Message\ResponseInterface; use Squeezely\RabbitMQ\Management\Client; +use Squeezely\RabbitMQ\Management\Response; class QueueService extends Client { @@ -15,7 +17,7 @@ class QueueService extends Client { * @param string $vhost * * @return Queue|false - * @throws \Exception + * @throws Exception */ public function getQueue(string $name, string $vhost) { try { @@ -25,8 +27,7 @@ public function getQueue(string $name, string $vhost) { } if(isset($vhostGeneralData['name']) && $vhostGeneralData['name'] == $name) { - $queue = new Queue($vhostGeneralData, $this->getVhostPermissions($name), $this->getVhostTopicPermissions($name)); - return $queue; + return new Queue($vhostGeneralData); } return false; @@ -61,25 +62,33 @@ public function getQueues(string $vhost = null) { * * @param string $name * @param string $vHost - * @param array $additionConfig + * @param array $additionConfig + * * @return bool * @throws GuzzleException */ public function createQueue(string $name, string $vHost, array $additionConfig = []) { - /** @var Response $res */ - $res = $this->sendAuthenticatedRequest('queues/' . '/' . $vHost . '/' . $name, 'PUT', $additionConfig, false); - if($res->getStatusCode() == 201 || $res->getStatusCode() == 204) { + /** @var ResponseInterface $res */ + $res = $this->sendAuthenticatedRequest('queues/' . $vHost . '/' . $name, 'PUT', $additionConfig, false); + if($res->getStatusCode() == Response::HTTP_CREATED || $res->getStatusCode() == Response::HTTP_NO_CONTENT) { return true; } return false; } + /** + * @param Queue $queue + * + * @return bool + * @throws GuzzleException + */ public function deleteQueue(Queue $queue) { $res = $this->sendAuthenticatedRequest('queues/' . $queue->getVhost() . '/' . $queue->getName(), 'DELETE', [], false); - if($res->getStatusCode() == 201 || $res->getStatusCode() == 204) { + if($res->getStatusCode() == Response::HTTP_CREATED || $res->getStatusCode() == Response::HTTP_NO_CONTENT) { return true; } - } + return false; + } } \ No newline at end of file diff --git a/src/Response.php b/src/Response.php new file mode 100644 index 0000000..1666a75 --- /dev/null +++ b/src/Response.php @@ -0,0 +1,77 @@ + true]; } - /** @var Response $res */ + /** @var ResponseInterface $res */ $res = $this->sendAuthenticatedRequest('vhosts/' . $name, 'PUT', $body, false); - if($res->getStatusCode() == 201 || $res->getStatusCode() == 204) { + if($res->getStatusCode() == Response::HTTP_CREATED || $res->getStatusCode() == Response::HTTP_NO_CONTENT) { return true; }