Completed
Push — master ( 3d1ae9...528b53 )
by Ryan
05:30
created

FiresCallbacks::fire()   C

Complexity

Conditions 11
Paths 60

Size

Total Lines 46
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 17
nc 60
nop 2
dl 0
loc 46
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Anomaly\Streams\Platform\Traits;
2
3
/**
4
 * Class FiresCallbacks
5
 *
6
 * @link   http://pyrocms.com/
7
 * @author PyroCMS, Inc. <[email protected]>
8
 * @author Ryan Thompson <[email protected]>
9
 */
10
trait FiresCallbacks
11
{
12
13
    /**
14
     * The local callbacks.
15
     *
16
     * @var array
17
     */
18
    protected $callbacks = [];
19
20
    /**
21
     * The static callbacks.
22
     *
23
     * @var array
24
     */
25
    protected static $listeners = [];
26
27
    /**
28
     * Register a new callback.
29
     *
30
     * @param $trigger
31
     * @param $callback
32
     * @return $this
33
     */
34
    public function on($trigger, $callback)
35
    {
36
        if (!isset($this->callbacks[$trigger])) {
37
            $this->callbacks[$trigger] = [];
38
        }
39
40
        $this->callbacks[$trigger][] = $callback;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Register a new listener.
47
     *
48
     * @param $trigger
49
     * @param $callback
50
     * @return $this
51
     */
52
    public function listen($trigger, $callback)
53
    {
54
	    $trigger = get_class($this) . '::' . $trigger;
55
56
        if (!isset(self::$listeners[$trigger])) {
57
            self::$listeners[$trigger] = [];
58
        }
59
60
        self::$listeners[$trigger][] = $callback;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Fire a set of closures by trigger.
67
     *
68
     * @param        $trigger
69
     * @param  array $parameters
70
     * @return $this
71
     */
72
    public function fire($trigger, array $parameters = [])
73
    {
74
75
        /*
76
         * First, fire global listeners.
77
         */
78
	    $classes = array_merge(class_parents($this), [get_class($this) => get_class($this)]);
79
80
	    foreach (array_keys($classes) as $caller) {
81
		    foreach (array_get(self::$listeners, $caller . '::' . $trigger, []) as $callback) {
82
			    if (is_string($callback) || $callback instanceof \Closure) {
83
				    app()->call($callback, $parameters);
84
			    }
85
86
			    if (method_exists($callback, 'handle')) {
87
				    app()->call([$callback, 'handle'], $parameters);
88
			    }
89
		    }
90
	    }
91
92
        /*
93
         * Next, check if the method
94
         * exists and run it if it does.
95
         */
96
        $method = camel_case('on_' . $trigger);
97
98
        if (method_exists($this, $method)) {
99
            app()->call([$this, $method], $parameters);
100
        }
101
102
        /*
103
         * Finally, run through all of
104
         * the registered callbacks.
105
         */
106
        foreach (array_get($this->callbacks, $trigger, []) as $callback) {
107
            if (is_string($callback) || $callback instanceof \Closure) {
108
                app()->call($callback, $parameters);
109
            }
110
111
            if (method_exists($callback, 'handle')) {
112
                app()->call([$callback, 'handle'], $parameters);
113
            }
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * Return if the callback exists.
121
     *
122
     * @param $trigger
123
     * @return bool
124
     */
125
    public function hasCallback($trigger)
126
    {
127
        return isset($this->callbacks[$trigger]);
128
    }
129
130
    /**
131
     * Return if the listener exists.
132
     *
133
     * @param $trigger
134
     * @return bool
135
     */
136
    public function hasListener($trigger)
137
    {
138
        return isset(self::$listeners[get_class($this) . '::' . $trigger]);
139
    }
140
}
141