|
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 Jellyfish\Lock\LockFactoryInterface; |
|
9
|
|
|
use Jellyfish\Lock\LockTrait; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
|
|
15
|
|
|
class EventQueueConsumeCommand extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
use LockTrait; |
|
18
|
|
|
|
|
19
|
|
|
public const NAME = 'event:queue:consume'; |
|
20
|
|
|
public const DESCRIPTION = 'Consume from event queue'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param \Jellyfish\Event\EventDispatcherInterface $eventDispatcher |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $eventDispatcher; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $eventQueueConsumer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \Jellyfish\Event\EventDispatcherInterface $eventDispatcher |
|
34
|
|
|
* @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer |
|
35
|
|
|
* @param \Jellyfish\Lock\LockFactoryInterface $lockFactory |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
39
|
|
|
EventQueueConsumerInterface $eventQueueConsumer, |
|
40
|
|
|
LockFactoryInterface $lockFactory |
|
41
|
|
|
) { |
|
42
|
|
|
parent::__construct(); |
|
43
|
|
|
|
|
44
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
45
|
|
|
$this->eventQueueConsumer = $eventQueueConsumer; |
|
46
|
|
|
$this->lockFactory = $lockFactory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function configure(): void |
|
53
|
|
|
{ |
|
54
|
|
|
parent::configure(); |
|
55
|
|
|
|
|
56
|
|
|
$this->setName(static::NAME); |
|
57
|
|
|
$this->setDescription(static::DESCRIPTION); |
|
58
|
|
|
|
|
59
|
|
|
$this->addArgument('eventName', InputArgument::REQUIRED, 'Event name'); |
|
60
|
|
|
$this->addArgument('listenerIdentifier', InputArgument::REQUIRED, 'Listener identifier'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
65
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
66
|
|
|
* |
|
67
|
|
|
* @return int|null |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
|
70
|
|
|
{ |
|
71
|
|
|
$eventName = (string) $input->getArgument('eventName'); |
|
72
|
|
|
$listenerIdentifier = (string) $input->getArgument('listenerIdentifier'); |
|
73
|
|
|
$lockIdentifier = $this->createIdentifier([static::NAME, $eventName, $listenerIdentifier]); |
|
74
|
|
|
|
|
75
|
|
|
if (!$this->acquire($lockIdentifier)) { |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$result = $this->executeLockablePart($eventName, $listenerIdentifier); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$this->release(); |
|
82
|
|
|
|
|
83
|
|
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $eventName |
|
88
|
|
|
* @param string $listenerIdentifier |
|
89
|
|
|
* |
|
90
|
|
|
* @return int|null |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function executeLockablePart(string $eventName, string $listenerIdentifier): ?int |
|
93
|
|
|
{ |
|
94
|
|
|
$event = $this->eventQueueConsumer->dequeueEvent($eventName, $listenerIdentifier); |
|
95
|
|
|
|
|
96
|
|
|
if ($event === null) { |
|
97
|
|
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$listener = $this->eventDispatcher |
|
101
|
|
|
->getListener(EventListenerInterface::TYPE_ASYNC, $eventName, $listenerIdentifier); |
|
102
|
|
|
|
|
103
|
|
|
if ($listener !== null) { |
|
104
|
|
|
$listener->handle($event); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.