EventQueueWorker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
eloc 13
c 2
b 0
f 1
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A start() 0 13 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Event;
6
7
use function count;
8
use function usleep;
9
10
class EventQueueWorker implements EventQueueWorkerInterface
11
{
12
    protected const DELAY_INTERVAL = 1000000;
13
14
    /**
15
     * @var \Jellyfish\Event\EventQueueConsumerInterface
16
     */
17
    protected $eventQueueConsumer;
18
    /**
19
     * @var \Jellyfish\Event\EventListenerProviderInterface
20
     */
21
    protected $eventListenerProvider;
22
23
    /**
24
     * @param \Jellyfish\Event\EventListenerProviderInterface $eventListenerProvider
25
     * @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer
26
     */
27
    public function __construct(
28
        EventListenerProviderInterface $eventListenerProvider,
29
        EventQueueConsumerInterface $eventQueueConsumer
30
    ) {
31
        $this->eventListenerProvider = $eventListenerProvider;
32
        $this->eventQueueConsumer = $eventQueueConsumer;
33
    }
34
35
    /**
36
     * @return void
37
     */
38
    public function start(): void
39
    {
40
        $listeners = $this->eventListenerProvider->getListenersByType(EventListenerInterface::TYPE_ASYNC);
41
42
        if (count($listeners) === 0) {
43
            return;
44
        }
45
46
        while (true) {
47
            foreach ($listeners as $eventName => $listenersPerEvent) {
48
                foreach ($listenersPerEvent as $listenerIdentifier => $listener) {
49
                    $this->eventQueueConsumer->dequeueAsProcess((string)$eventName, $listenerIdentifier);
50
                    usleep(static::DELAY_INTERVAL);
51
                }
52
            }
53
        }
54
    }
55
}
56