Commit 3750cf7c authored by dbourdere's avatar dbourdere 🤠

Initial commit

parents
vendor
.idea
\ No newline at end of file
{
"name": "aboutgoods/AGRabbitMQ",
"require": {
"symfony/validator": ">=3.2",
"doctrine/annotations": ">=1.4",
"php-amqplib/php-amqplib": "^2.7"
},
"autoload": {
"psr-4": {
"Aboutgoods\\RabbitMQ\\": "/src",
"Test\\": "tests/"
}
},
"require-dev": {
"symfony/var-dumper": "^4.1",
"phpunit/phpunit": "^7",
"symfony/phpunit-bridge": "*"
}
}
\ No newline at end of file
This diff is collapsed.
<?php
namespace Aboutgoods\RabbitMQ;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class Client
{
/**
* @var AMQPStreamConnection
*/
private $connection;
/**
* @var AMQPChannel
*/
private $channel;
private $channelName;
private $address;
private $port;
private $login;
private $password;
public function __construct($address = "localhost", $port = 5672, $login = "guest", $password = "guest")
{
$this->address = $address;
$this->port = $port;
$this->login = $login;
$this->password = $password;
}
public function connect()
{
$this->connection = new AMQPStreamConnection($this->address, $this->port, $this->login, $this->password);
}
public function setConnection($connection)
{
if ($connection instanceof AMQPStreamConnection) {
$this->connection = $connection;
}
}
public function connectToChannel(
$queue = '',
$passive = false,
$durable = false,
$exclusive = false,
$auto_delete = true
) {
$this->channel = $this->connection->channel();
$this->channelName = $queue;
return $this->channel->queue_declare($queue, $passive, $durable, $exclusive, $auto_delete);
}
public function send($content)
{
$msg = new AMQPMessage($content, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
$this->channel->basic_publish($msg, '', $this->channelName);
return true;
}
public function __destruct()
{
$this->channel->close();
$this->connection->close();
}
}
\ No newline at end of file
<?php
namespace Aboutgoods\RabbitMQ;
use Aboutgoods\RabbitMQ\Exceptions\ClientNotConnectedException;
use Aboutgoods\RabbitMQ\Exceptions\InvalidObjectException;
use Aboutgoods\RabbitMQ\Tools\JsonSerializer;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validation;
use Doctrine\Common\Annotations\AnnotationRegistry;
class Connector
{
private static $client = null;
public static function connect($config = [])
{
if (self::$client == null) {
self::$client = new Client(
$config["address"] ?? "127.0.0.1",
$config["port"] ?? 5672,
$config["login"] ?? "guest",
$config["password"] ?? "guest"
);
self::$client->connect();
self::$client->connectToChannel($config["queue"] ?? "jakku", false, false, false, false);
}
return self::class;
}
public static function setClient(?Client $client)
{
self::$client = $client;
}
protected static function getClient(): Client
{
if (self::$client == null) {
throw new ClientNotConnectedException();
}
return self::$client;
}
public static function emit(DataBag\AbstractDataBag $dataBag)
{
if ( ! class_exists('Doctrine\Common\Annotations\AnnotationRegistry', false)
&& class_exists('Doctrine\Common\Annotations\AnnotationRegistry')
) {
if (method_exists('Doctrine\Common\Annotations\AnnotationRegistry', 'registerUniqueLoader')) {
AnnotationRegistry::registerUniqueLoader('class_exists');
} else {
AnnotationRegistry::registerLoader('class_exists');
}
}
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$violations = $validator->validate($dataBag);
if (0 !== $violations->count()) {
throw new InvalidObjectException($violations);
}
$content = JsonSerializer::stringify($dataBag);
self::getClient()->send($content);
return self::class;
}
}
\ No newline at end of file
<?php
namespace Aboutgoods\RabbitMQ\Exceptions;
class ClientNotConnectedException extends \Exception
{
}
\ No newline at end of file
<?php
namespace Aboutgoods\RabbitMQ\Exceptions;
use Symfony\Component\Validator\ConstraintViolationList;
use Throwable;
class InvalidObjectException extends \Exception
{
/**
* @var ConstraintViolationList
*/
private $constraintViolationList;
public function __construct(ConstraintViolationList $constraintViolationList)
{
$this->constraintViolationList = $constraintViolationList;
parent::__construct("invalid Object for rabbitMQ");
}
public function getErrorsAsArray()
{
$content = [];
foreach ($this->constraintViolationList as $violation) {
$content[] = ["propertyPath" => $violation->getPropertyPath(), "message" => $violation->getMessage()];
}
return $content;
}
public function getErrorsAsString()
{
$message = PHP_EOL;
foreach ($this->constraintViolationList as $violation) {
$message .= "\t* ".$violation->getPropertyPath()." - ".$violation->getMessage().PHP_EOL;
}
return $message;
}
}
\ No newline at end of file
<?php
namespace Aboutgoods\RabbitMQ\Tools;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class JsonSerializer
{
private static $serializer = null;
public static function getInstance()
{
if (self::$serializer == null) {
$encoders = [new JsonEncoder()];
$normalizers = [
new \Symfony\Component\Serializer\Normalizer\DateTimeNormalizer(\DateTime::ISO8601),
new \Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer(),
new ObjectNormalizer(),
];
self::$serializer = new Serializer($normalizers, $encoders);
}
return self::$serializer;
}
/**
* @param $object
*
* @return bool|float|int|string
*/
public static function stringify($object)
{
return self::getInstance()->serialize($object, "json");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment