Passed
Push — master ( 69d06b...aefa73 )
by Gabriel
06:27 queued 12s
created

MakeListenerTrait::makeListener()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\EventDispatcher\Dispatcher\Traits;
4
5
use ByTIC\EventDispatcher\Queue\Dispatcher\QueuedListenerTrait;
6
use Closure;
7
use Nip\Utility\Str;
8
9
/**
10
 * Trait MakeListenerTrait
11
 * @package ByTIC\EventDispatcher\Dispatcher\Traits
12
 */
13
trait MakeListenerTrait
14
{
15
    use QueuedListenerTrait;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function addListener(string $eventName, $listener, int $priority = 0)
21
    {
22
        $listener = $this->makeListener($listener);
23
        parent::addListener($eventName, $listener, $priority);
24
    }
25
26
    /**
27
     * Register an event listener with the dispatcher.
28
     *
29
     * @param Closure|string|array $listener
30
     * @return Closure
31
     */
32
    public function makeListener($listener): Closure
33
    {
34
        if (is_string($listener) || is_array($listener)) {
35
            return $this->createClassListener($listener);
0 ignored issues
show
Bug introduced by
It seems like $listener can also be of type array; however, parameter $listener of ByTIC\EventDispatcher\Di...::createClassListener() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            return $this->createClassListener(/** @scrutinizer ignore-type */ $listener);
Loading history...
36
        }
37
        if (is_object($listener)) {
38
            return $this->createObjectListener($listener);
39
        }
40
41
        return function ($event) use ($listener) {
42
            return $listener($event);
43
        };
44
    }
45
46
    /**
47
     * Create a class based listener using the IoC container.
48
     *
49
     * @param string $listener
50
     * @return Closure
51
     */
52
    protected function createClassListener($listener): Closure
53
    {
54
        return function ($event) use ($listener) {
55
            return call_user_func_array(
56
                $this->createClassCallable($listener),
57
                [$event]
58
            );
59
        };
60
    }
61
62
    /**
63
     * @param $listener
64
     * @return Closure
65
     */
66
    protected function createObjectListener($listener): Closure
67
    {
68
        return function ($event) use ($listener) {
69
            return $listener($event);
70
        };
71
    }
72
73
    /**
74
     * Create the class based event callable.
75
     *
76
     * @param string|array $listener
77
     * @return callable
78
     */
79
    protected function createClassCallable($listener)
80
    {
81
        [$class, $method] = $this->parseClassCallable($listener);
82
83
        if ($this->handlerShouldBeQueued($class)) {
84
            return $this->createQueuedHandlerCallable($class, $method);
85
        }
86
87
        $listenerObject = app($class);
88
        return [$listenerObject, $method];
89
    }
90
91
    /**
92
     * Parse the class listener into class and method.
93
     *
94
     * @param string|array $listener
95
     * @return array
96
     */
97
    protected function parseClassCallable($listener)
98
    {
99
        if (is_array($listener)) {
100
            return $listener;
101
        }
102
        return Str::parseCallback($listener, 'handle');
103
    }
104
}
105