Created
April 22, 2015 12:51
-
-
Save zemd/cf3c0fb4d15f11b0d6f2 to your computer and use it in GitHub Desktop.
Deferred events via rabbitmq
This file contains hidden or 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
parameters: | |
event_dispatcher.class: Opesho\CommonBundle\EventDispatcher |
This file contains hidden or 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
<?php | |
/** | |
* Event that handles asynchronously via rabbitmq queue | |
* | |
* Usage: | |
* <code> | |
* $event = new DeferredEvent(new EmailEvent("[email protected]", "[email protected]", "message")); | |
* $dispatcher->dispatch("email.event", $event); | |
* </code> | |
*/ | |
class DeferredEvent extends Event | |
{ | |
const EVENT = 'deferred_event'; | |
/** @var Event */ | |
protected $internalEvent; | |
/** @var DateTime */ | |
protected $sentAt; | |
/** @var string */ | |
protected $originalName; | |
public function __construct(Event $event) | |
{ | |
$this->internalEvent = $event; | |
$this->sentAt = DateUtils::getDate(); | |
} | |
/** | |
* @return string | |
*/ | |
public function getOriginalName() | |
{ | |
return $this->originalName; | |
} | |
/** | |
* @param string $originalName | |
*/ | |
public function setOriginalName($originalName) | |
{ | |
$this->originalName = $originalName; | |
$this->internalEvent->setName($originalName); | |
} | |
/** | |
* @return Event | |
*/ | |
public function getEvent() | |
{ | |
return $this->internalEvent; | |
} | |
/** | |
* (PHP 5 >= 5.1.0)<br/> | |
* String representation of object | |
* @link http://php.net/manual/en/serializable.serialize.php | |
* @return string the string representation of the object or null | |
*/ | |
public function serialize() | |
{ | |
return serialize(array( | |
$this->internalEvent, | |
$this->sentAt, | |
$this->originalName | |
)); | |
} | |
/** | |
* (PHP 5 >= 5.1.0)<br/> | |
* Constructs the object | |
* @link http://php.net/manual/en/serializable.unserialize.php | |
* @param string $serialized <p> | |
* The string representation of the object. | |
* </p> | |
* @return void | |
*/ | |
public function unserialize($serialized) | |
{ | |
list( | |
$this->internalEvent, | |
$this->sentAt, | |
$this->originalName | |
) = unserialize($serialized); | |
} | |
} |
This file contains hidden or 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
<?php | |
class DeferredSubscriber implements EventSubscriberInterface | |
{ | |
/** @var Producer */ | |
protected $producer; | |
public function __construct(Producer $producer) | |
{ | |
$this->producer = $producer; | |
} | |
/** | |
* Returns an array of event names this subscriber wants to listen to. | |
* | |
* The array keys are event names and the value can be: | |
* | |
* * The method name to call (priority defaults to 0) | |
* * An array composed of the method name to call and the priority | |
* * An array of arrays composed of the method names to call and respective | |
* priorities, or 0 if unset | |
* | |
* For instance: | |
* | |
* * array('eventName' => 'methodName') | |
* * array('eventName' => array('methodName', $priority)) | |
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')) | |
* | |
* @return array The event names to listen to | |
* | |
* @api | |
*/ | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
DeferredEvent::EVENT => 'onDeferredEvent' | |
); | |
} | |
public function onDeferredEvent(DeferredEvent $event, $eventName, $dispatcher) | |
{ | |
$this->producer->publish(serialize($event->getEvent())); | |
} | |
} |
This file contains hidden or 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
<?php | |
use Symfony\Component\EventDispatcher\Event as BaseEvent; | |
abstract class Event extends BaseEvent implements \Serializable | |
{ | |
} |
This file contains hidden or 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
<?php | |
class EventDispatcher extends ContainerAwareEventDispatcher | |
{ | |
public function dispatch($eventName, Event $event = null) | |
{ | |
if ($event instanceof DeferredEvent) { | |
$event->setOriginalName($event); | |
return parent::dispatch(DeferredEvent::EVENT, $event); | |
} | |
return parent::dispatch($eventName, $event); | |
} | |
} |
This file contains hidden or 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
parameters: | |
common_subscriber.deferred_event.class: Opesho\CommonBundle\EventListener\DeferredSubscriber | |
services: | |
common_subscriber.deferred_event: | |
class: %common_subscriber.deferred_event.class% | |
arguments: [ @old_sound_rabbit_mq.deferred_event_producer ] | |
tags: | |
- { name: 'kernel.event_subscriber' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment