Completed
Push — master ( c2ba48...1fd225 )
by Markus
14s queued 12s
created

EventQueueConsumeCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 19 3
A __construct() 0 8 1
A configure() 0 9 1
1
<?php
2
3
namespace Jellyfish\Event\Command;
4
5
use Jellyfish\Event\EventDispatcherInterface;
6
use Jellyfish\Event\EventListenerInterface;
7
use Jellyfish\Event\EventQueueConsumerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class EventQueueConsumeCommand extends Command
14
{
15
    public const NAME = 'event:queue:consume';
16
    public const DESCRIPTION = 'Consume from event queue';
17
18
    /**
19
     * @param \Jellyfish\Event\EventDispatcherInterface $eventDispatcher
20
     */
21
    protected $eventDispatcher;
22
23
    /**
24
     * @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer
25
     */
26
    protected $eventQueueConsumer;
27
28
    /**
29
     * @param \Jellyfish\Event\EventDispatcherInterface $eventDispatcher
30
     * @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer
31
     */
32
    public function __construct(
33
        EventDispatcherInterface $eventDispatcher,
34
        EventQueueConsumerInterface $eventQueueConsumer
35
    ) {
36
        parent::__construct(null);
37
38
        $this->eventDispatcher = $eventDispatcher;
39
        $this->eventQueueConsumer = $eventQueueConsumer;
40
    }
41
42
    /**
43
     * @return void
44
     */
45
    protected function configure(): void
46
    {
47
        parent::configure();
48
49
        $this->setName(static::NAME);
50
        $this->setDescription(static::DESCRIPTION);
51
52
        $this->addArgument('eventName', InputArgument::REQUIRED, 'Event name');
53
        $this->addArgument('listenerIdentifier', InputArgument::REQUIRED, 'Listener identifier');
54
    }
55
56
    /**
57
     * @param \Symfony\Component\Console\Input\InputInterface $input
58
     * @param \Symfony\Component\Console\Output\OutputInterface $output
59
     *
60
     * @return int|null
61
     */
62
    protected function execute(InputInterface $input, OutputInterface $output): ?int
63
    {
64
        $eventName = (string) $input->getArgument('eventName');
65
        $listenerIdentifier = (string) $input->getArgument('listenerIdentifier');
66
67
        $event = $this->eventQueueConsumer->dequeueEvent($eventName, $listenerIdentifier);
68
69
        if ($event === null) {
70
            return null;
71
        }
72
73
        $listener = $this->eventDispatcher
74
            ->getListener(EventListenerInterface::TYPE_ASYNC, $eventName, $listenerIdentifier);
75
76
        if ($listener !== null) {
77
            $listener->handle($event);
78
        }
79
80
        return null;
81
    }
82
}
83