Passed
Push — master ( aefa73...fefe4c )
by Gabriel
14:43
created

MakeListenerTrait::createClassListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ByTIC\EventDispatcher\ListenerProviders\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\ListenerProviders\Traits
12
 */
13
trait MakeListenerTrait
14
{
15
    use QueuedListenerTrait;
16
17
    /**
18
     * Register an event listener with the dispatcher.
19
     *
20
     * @param Closure|string|array $listener
21
     * @return Closure
22
     */
23
    public function makeListener($listener)
24
    {
25
        if (is_string($listener) || is_array($listener)) {
26
            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\Li...::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

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