-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed queue creation Fixed queue retrieval Added Response class with HTTP status codes for readability
- Loading branch information
Showing
6 changed files
with
412 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
vendor/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.