Skip to content

Commit

Permalink
Adding binding management
Browse files Browse the repository at this point in the history
Fixed queue creation
Fixed queue retrieval
Added Response class with HTTP status codes for readability
  • Loading branch information
HemeraOne committed Aug 25, 2020
1 parent d7ad909 commit 666e494
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/
.idea/
182 changes: 182 additions & 0 deletions src/Bindings/Binding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php


namespace Squeezely\RabbitMQ\Management\Bindings;


class Binding {
public const DESTINATION_TYPE_EXCHANGE = 'exchange';
public const DESTINATION_TYPE_QUEUE = 'queue';

public const DESTINATION_TYPES = [
self::DESTINATION_TYPE_EXCHANGE,
self::DESTINATION_TYPE_QUEUE
];


/** @var array */
private $data;

/**
* @var string
*/
private $source = '';
/**
* @var array
*/
private $arguments = '';
/**
* @var string
*/
private $vhost = '';
/**
* @var string
*/
private $destinationType = '';
/**
* @var string
*/
private $destination = '';
/**
* @var string
*/
private $routing_key = '';
/**
* @var string
*/
private $propertiesKey = '';

/**
* Binding constructor.
*
* @param $data
*/
public function __construct(array $data = []) {
$this->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;
}
}
127 changes: 127 additions & 0 deletions src/Bindings/BindingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php


namespace Squeezely\RabbitMQ\Management\Bindings;


use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Squeezely\RabbitMQ\Management\Client;
use Squeezely\RabbitMQ\Management\Queue\Queue;
use Squeezely\RabbitMQ\Management\Response;

class BindingService extends Client {

public function getAllBindings(?string $vhost = null) {
if($vhost !== null) {
$context = 'bindings/' . $vhost;
}
else {
$context = 'bindings';
}

try {
$rawBindings = $this->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;
}

}
29 changes: 19 additions & 10 deletions src/Queue/QueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 666e494

Please sign in to comment.