CallQueuedListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 18
dl 0
loc 46
ccs 0
cts 19
cp 0
rs 10
c 4
b 0
f 1
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 3 1
A setListener() 0 7 2
A queueEvent() 0 16 4
1
<?php
2
3
namespace ByTIC\EventDispatcher\Queue\Listeners;
4
5
use ByTIC\EventDispatcher\Events\EventInterface;
6
use ByTIC\EventDispatcher\Listeners\ListenerInterface;
7
use ByTIC\Queue\JobQueue\Bus\PendingDispatch;
8
use ByTIC\Queue\JobQueue\Jobs\Job;
9
use ReflectionClass;
10
11
/**
12
 * Class CallQueuedListener
13
 * @package ByTIC\EventDispatcher\Queue\Listeners
14
 */
15
class CallQueuedListener implements ListenerInterface
16
{
17
    protected $listener;
18
19
    protected $listenerObject;
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function handle(EventInterface $event)
25
    {
26
        $this->queueEvent($event);
27
    }
28
29
    /**
30
     * @param mixed $listener
31
     */
32
    public function setListener($listener): void
33
    {
34
        $this->listener = $listener;
35
        $class = $listener[0];
36
        try {
37
            $this->listenerObject = (new ReflectionClass($class))->newInstanceWithoutConstructor();
38
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
39
        }
40
    }
41
42
    /**
43
     * @param EventInterface $event
44
     */
45
    protected function queueEvent(EventInterface $event)
46
    {
47
        $job = new Job($this->listener);
48
49
        if (isset($this->listenerObject->connection)) {
50
            $job->onConnection($this->listenerObject->connection);
51
        }
52
        if (isset($this->listenerObject->queue)) {
53
            $job->onQueue($this->listenerObject->queue);
54
        }
55
        if (isset($this->listenerObject->delay)) {
56
            $job->delay($this->listenerObject->delay);
57
        }
58
        $job->arguments([$event]);
59
60
        (new PendingDispatch($job));
61
    }
62
}
63