EventQueueConsumeCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 12
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Event\Command;
6
7
use InvalidArgumentException;
8
use Jellyfish\Event\EventBulkListenerInterface;
9
use Jellyfish\Event\EventListenerInterface;
10
use Jellyfish\Event\EventListenerProviderInterface;
11
use Jellyfish\Event\EventQueueConsumerInterface;
12
use Jellyfish\Lock\LockFactoryInterface;
13
use Jellyfish\Lock\LockTrait;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Throwable;
20
21
use function is_string;
22
23
class EventQueueConsumeCommand extends Command
24
{
25
    use LockTrait;
26
27
    public const NAME = 'event:queue:consume';
28
    public const DESCRIPTION = 'Consume from event queue';
29
30
    /**
31
     * @var \Jellyfish\Event\EventListenerProviderInterface $eventDispatcher
32
     */
33
    protected $eventDispatcher;
34
35
    /**
36
     * @var \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer
37
     */
38
    protected $eventQueueConsumer;
39
40
    /**
41
     * @var \Psr\Log\LoggerInterface
42
     */
43
    protected $logger;
44
45
    /**
46
     * @param \Jellyfish\Event\EventListenerProviderInterface $eventDispatcher
47
     * @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer
48
     * @param \Jellyfish\Lock\LockFactoryInterface $lockFactory
49
     * @param \Psr\Log\LoggerInterface $logger
50
     */
51
    public function __construct(
52
        EventListenerProviderInterface $eventDispatcher,
53
        EventQueueConsumerInterface $eventQueueConsumer,
54
        LockFactoryInterface $lockFactory,
55
        LoggerInterface $logger
56
    ) {
57
        parent::__construct();
58
59
        $this->eventDispatcher = $eventDispatcher;
60
        $this->eventQueueConsumer = $eventQueueConsumer;
61
        $this->lockFactory = $lockFactory;
62
        $this->logger = $logger;
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    protected function configure(): void
69
    {
70
        parent::configure();
71
72
        $this->setName(static::NAME);
73
        $this->setDescription(static::DESCRIPTION);
74
75
        $this->addArgument('eventName', InputArgument::REQUIRED, 'Event name');
76
        $this->addArgument('listenerIdentifier', InputArgument::REQUIRED, 'Listener identifier');
77
    }
78
79
    /**
80
     * @param \Symfony\Component\Console\Input\InputInterface $input
81
     * @param \Symfony\Component\Console\Output\OutputInterface $output
82
     *
83
     * @return int|null
84
     */
85
    protected function execute(InputInterface $input, OutputInterface $output): ?int
86
    {
87
        $eventName = $input->getArgument('eventName');
88
        $listenerIdentifier = $input->getArgument('listenerIdentifier');
89
90
        if (!is_string($eventName) || !is_string($listenerIdentifier)) {
91
            throw new InvalidArgumentException('Unsupported type for given arguments');
92
        }
93
94
        $lockIdentifierParts = [static::NAME, $eventName, $listenerIdentifier];
95
96
        if (!$this->acquire($lockIdentifierParts)) {
97
            return null;
98
        }
99
100
        $result = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
101
102
        try {
103
            $result = $this->executeLockablePart($eventName, $listenerIdentifier);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->executeLockablePa...e, $listenerIdentifier) targeting Jellyfish\Event\Command\...::executeLockablePart() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

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.

Loading history...
104
        } catch (Throwable $e) {
105
            $this->logger->error((string)$e);
106
        } finally {
107
            $this->release();
108
        }
109
110
        return $result;
111
    }
112
113
    /**
114
     * @param string $eventName
115
     * @param string $listenerIdentifier
116
     *
117
     * @return int|null
118
     */
119
    protected function executeLockablePart(string $eventName, string $listenerIdentifier): ?int
120
    {
121
        $listener = $this->eventDispatcher
122
            ->getListener(EventListenerInterface::TYPE_ASYNC, $eventName, $listenerIdentifier);
123
124
        if ($listener === null) {
125
            return null;
126
        }
127
128
        if ($listener instanceof EventBulkListenerInterface) {
129
            $events = $this->eventQueueConsumer
130
                ->dequeueBulk($eventName, $listenerIdentifier, $listener->getChunkSize());
131
132
            $listener->handleBulk($events);
133
134
            return null;
135
        }
136
137
        $event = $this->eventQueueConsumer->dequeue($eventName, $listenerIdentifier);
138
139
        if ($event === null) {
140
            return null;
141
        }
142
143
        $listener->handle($event);
144
145
        return null;
146
    }
147
}
148