Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch events around publishing #598

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Event/AMQPEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OldSound\RabbitMqBundle\Event;

use OldSound\RabbitMqBundle\RabbitMq\Consumer;
use OldSound\RabbitMqBundle\RabbitMq\Producer;
use PhpAmqpLib\Message\AMQPMessage;
use Symfony\Component\EventDispatcher\Event;

Expand All @@ -18,6 +19,8 @@ class AMQPEvent extends Event
const ON_IDLE = 'on_idle';
const BEFORE_PROCESSING_MESSAGE = 'before_processing';
const AFTER_PROCESSING_MESSAGE = 'after_processing';
const BEFORE_PUBLISHING_MESSAGE = 'before_publishing';
const AFTER_PUBLISHING_MESSAGE = 'after_publishing';

/**
* @var AMQPMessage
Expand All @@ -29,6 +32,11 @@ class AMQPEvent extends Event
*/
protected $consumer;

/**
* @var Producer
*/
protected $producer;

/**
* @return AMQPMessage
*/
Expand Down Expand Up @@ -68,4 +76,24 @@ public function setConsumer(Consumer $consumer)

return $this;
}

/**
* @return Producer
*/
public function getProducer()
{
return $this->producer;
}

/**
* @param Producer $producer
*
* @return AMQPEvent
*/
public function setProducer(Producer $producer)
{
$this->producer = $producer;

return $this;
}
}
28 changes: 28 additions & 0 deletions Event/AfterPublishingMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace OldSound\RabbitMqBundle\Event;

use OldSound\RabbitMqBundle\RabbitMq\Producer;
use PhpAmqpLib\Message\AMQPMessage;

/**
* Class AfterPublishingMessageEvent
*
* @package OldSound\RabbitMqBundle\Command
*/
class AfterPublishingMessageEvent extends AMQPEvent
{
const NAME = AMQPEvent::AFTER_PUBLISHING_MESSAGE;

/**
* AfterPublishingMessageEvent constructor.
*
* @param Producer $producer
* @param AMQPMessage $AMQPMessage
*/
public function __construct(Producer $producer, AMQPMessage $AMQPMessage)
{
$this->setProducer($producer);
$this->setAMQPMessage($AMQPMessage);
}
}
28 changes: 28 additions & 0 deletions Event/BeforePublishingMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace OldSound\RabbitMqBundle\Event;

use OldSound\RabbitMqBundle\RabbitMq\Producer;
use PhpAmqpLib\Message\AMQPMessage;

/**
* Class BeforePublishingMessageEvent
*
* @package OldSound\RabbitMqBundle\Command
*/
class BeforePublishingMessageEvent extends AMQPEvent
{
const NAME = AMQPEvent::BEFORE_PUBLISHING_MESSAGE;

/**
* BeforePublishingMessageEvent constructor.
*
* @param Producer $producer
* @param AMQPMessage $AMQPMessage
*/
public function __construct(Producer $producer, AMQPMessage $AMQPMessage)
{
$this->setProducer($producer);
$this->setAMQPMessage($AMQPMessage);
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ If you need to use a custom class for a producer (which should inherit from `Old
...
```

#### Producer Events ####

TODO


The next piece of the puzzle is to have a consumer that will take the message out of the queue and process it accordingly.

Expand Down
4 changes: 4 additions & 0 deletions RabbitMq/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OldSound\RabbitMqBundle\RabbitMq;

use OldSound\RabbitMqBundle\Event\AfterPublishingMessageEvent;
use OldSound\RabbitMqBundle\Event\BeforePublishingMessageEvent;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;

Expand Down Expand Up @@ -53,6 +55,7 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra
$msg->set('application_headers', $headersTable);
}

$this->dispatchEvent(BeforePublishingMessageEvent::NAME, new BeforePublishingMessageEvent($this, $msg));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep in mind that the signature of the EventDispatcherInterface::dispatch() method has been changed to dispatch($event, string $eventName = null): object since 5.0.0

$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey);
$this->logger->debug('AMQP message published', array(
'amqp' => array(
Expand All @@ -62,5 +65,6 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra
'headers' => $headers
)
));
$this->dispatchEvent(AfterPublishingMessageEvent::NAME, new AfterPublishingMessageEvent($this, $msg));
}
}