Example #1
0
 /**
  * @param string $message
  * @param null   $routingKeyArgument::type(Publisher::class)
  * @param int    $flags
  * @param array  $attributes
  *
  * @throws \Exception
  *
  * @return bool
  */
 public function publish($message, $routingKey = null, $flags = AMQP_NOPARAM, array $attributes = [])
 {
     $hasEventDispatcher = null !== $this->eventDispatcher;
     if (!empty($this->routingKeyPrefix)) {
         $routingKey = $this->routingKeyPrefix . $routingKey;
     }
     if (!$this->isInitialized()) {
         $this->initialize();
     }
     if (true === $hasEventDispatcher) {
         $event = new PublisherEvent($message, $routingKey, $flags, $attributes, $this->exchange);
     }
     try {
         if (true === $hasEventDispatcher) {
             $this->eventDispatcher->dispatch(PublisherEvents::PRE_PUBLISH, $event);
             $result = $this->exchange->getWrappedExchange()->publish($event->getMessage(), $event->getRoutingKey(), $event->getFlags(), $event->getAttributes());
         } else {
             $result = $this->exchange->getWrappedExchange()->publish($message, $routingKey, $flags, $attributes);
         }
         if (!$result) {
             if (true === $hasEventDispatcher) {
                 $this->eventDispatcher->dispatch(PublisherEvents::FAIL_PUBLISH, new FailedPublisherEvent($event, null, $this));
             }
         } else {
             if (true === $hasEventDispatcher) {
                 $this->eventDispatcher->dispatch(PublisherEvents::SUCCESS_PUBLISH, new SuccessPublisherEvent($event));
             }
         }
     } catch (\Exception $e) {
         if (true === $hasEventDispatcher) {
             $this->eventDispatcher->dispatch(PublisherEvents::FAIL_PUBLISH, new FailedPublisherEvent($event, $e, $this));
         }
         throw $e;
     }
     return $result;
 }
Example #2
0
 public function it_should_publish_message_with_routing_key_null(EventDispatcher $eventDispatcher, Exchange $exchange, Channel $channel, \AMQPExchange $AMQPExchange, Connection $connection, Identity $identity)
 {
     $message = 'foo.bar';
     $routingKey = null;
     $flags = AMQP_NOPARAM;
     $attributes = ['x-expires' => 1000];
     $event = new PublisherEvent($message, $routingKey, $flags, $attributes, $exchange->getWrappedObject());
     $successEvent = new SuccessPublisherEvent($event);
     $connection->connect()->shouldBeCalled();
     $channel->isInitialized()->willReturn(true);
     $exchange->isInitialized()->willReturn(true);
     $eventDispatcher->dispatch(PublisherEvents::PRE_PUBLISH, $event)->shouldBeCalled();
     $AMQPExchange->publish($message, $routingKey, $flags, $attributes)->willReturn(true);
     $exchange->getWrappedExchange()->willReturn($AMQPExchange);
     $eventDispatcher->dispatch(PublisherEvents::SUCCESS_PUBLISH, $successEvent)->shouldBeCalled();
     $this->publish($message, $routingKey, $flags, $attributes, false)->shouldReturn(true);
 }