Completed
Push — master ( 489a99...99d7d3 )
by Ryan
07:23
created

FiresCallbacks::setCallbacks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Anomaly\Streams\Platform\Traits;
2
3
use Illuminate\Contracts\Bus\SelfHandling;
4
5
/**
6
 * Class FiresCallbacks
7
 *
8
 * @link          http://anomaly.is/streams-platform
9
 * @author        AnomalyLabs, Inc. <[email protected]>
10
 * @author        Ryan Thompson <[email protected]>
11
 * @package       Anomaly\Streams\Platform\Traits
12
 */
13
trait FiresCallbacks
14
{
15
16
    /**
17
     * The local callbacks.
18
     *
19
     * @var array
20
     */
21
    protected $callbacks = [];
22
23
    /**
24
     * The static callbacks.
25
     *
26
     * @var array
27
     */
28
    protected static $listeners = [];
29
30
    /**
31
     * Register a new callback.
32
     *
33
     * @param $trigger
34
     * @param $callback
35
     * @return $this
36
     */
37
    public function on($trigger, $callback)
38
    {
39
        if (!isset($this->callbacks[$trigger])) {
40
            $this->callbacks[$trigger] = [];
41
        }
42
43
        $this->callbacks[$trigger][] = $callback;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Register a new listener.
50
     *
51
     * @param $trigger
52
     * @param $callback
53
     * @return $this
54
     */
55
    public function listen($trigger, $callback)
56
    {
57
        if (!isset(self::$listeners[$trigger])) {
58
            self::$listeners[$trigger] = [];
59
        }
60
61
        self::$listeners[$trigger][] = $callback;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Fire a set of closures by trigger.
68
     *
69
     * @param       $trigger
70
     * @param array $parameters
71
     * @return $this
72
     */
73
    public function fire($trigger, array $parameters = [])
74
    {
75
76
        /**
77
         * Fire listeners first.
78
         */
79 View Code Duplication
        foreach (array_get(self::$listeners, $trigger, []) as $callback) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
81
            if (is_string($callback) || $callback instanceof \Closure) {
82
                app()->call($callback, $parameters);
83
            }
84
85
            if ($callback instanceof SelfHandling) {
86
                app()->call([$callback, 'handle'], $parameters);
87
            }
88
        }
89
90
        $method = camel_case('on_' . $trigger);
91
92
        if (method_exists($this, $method)) {
93
            app()->call([$this, $method], $parameters);
94
        }
95
96
        $handler = get_class($this) . ucfirst(camel_case('on_' . $trigger));
97
98
        if (class_exists($handler)) {
99
            app()->call($handler . '@handle', $parameters);
100
        }
101
102
        $observer = get_class($this) . 'Callbacks';
103
104
        if (class_exists($observer) && $observer = app($observer, $parameters)) {
105
            if (method_exists($observer, $method)) {
106
                app()->call([$observer, $method], $parameters);
107
            }
108
        }
109
110 View Code Duplication
        foreach (array_get($this->callbacks, $trigger, []) as $callback) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
112
            if (is_string($callback) || $callback instanceof \Closure) {
113
                app()->call($callback, $parameters);
114
            }
115
116
            if ($callback instanceof SelfHandling) {
117
                app()->call([$callback, 'handle'], $parameters);
118
            }
119
        }
120
121
        return $this;
122
    }
123
124
    /**
125
     * Return if the callback exists.
126
     *
127
     * @param $trigger
128
     * @return bool
129
     */
130
    public function hasCallback($trigger)
131
    {
132
        return isset(self::$callbacks[$trigger]);
133
    }
134
}
135