CallQueuedListener::setListener()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 10
c 1
b 0
f 1
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