-
Notifications
You must be signed in to change notification settings - Fork 8
/
AsyncProcessor.php
53 lines (42 loc) · 1.5 KB
/
AsyncProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Enqueue\AsyncEventDispatcher;
use Enqueue\Consumption\Result;
use Interop\Queue\Context;
use Interop\Queue\Message;
use Interop\Queue\Processor;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class AsyncProcessor implements Processor
{
/**
* @var Registry
*/
private $registry;
/**
* @var AsyncEventDispatcher
*/
private $dispatcher;
public function __construct(Registry $registry, EventDispatcherInterface $dispatcher)
{
$this->registry = $registry;
if (false == $dispatcher instanceof AsyncEventDispatcher) {
throw new \InvalidArgumentException(sprintf(
'The dispatcher argument must be instance of "%s" but got "%s"',
AsyncEventDispatcher::class,
get_class($dispatcher)
));
}
$this->dispatcher = $dispatcher;
}
public function process(Message $message, Context $context)
{
if (false == $eventName = $message->getProperty('event_name')) {
return Result::reject('The message is missing "event_name" property');
}
if (false == $transformerName = $message->getProperty('transformer_name')) {
return Result::reject('The message is missing "transformer_name" property');
}
$event = $this->registry->getTransformer($transformerName)->toEvent($eventName, $message);
$this->dispatcher->dispatchAsyncListenersOnly($eventName, $event);
return self::ACK;
}
}