Passed
Push — master ( d15464...d64dd2 )
by Alexey
05:25
created

Inji::event()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0036

Importance

Changes 0
Metric Value
cc 10
eloc 25
nc 8
nop 2
dl 0
loc 34
ccs 29
cts 30
cp 0.9667
crap 10.0036
rs 4.8196
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
2
3
/**
4
 * Inji core
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Inji {
12
13
    /**
14
     * Static storage for core object
15
     * 
16
     * @var Inji
17
     */
18
    public static $inst = null;
19
20
    /**
21
     * Dynamic events listeners
22
     * 
23
     * @var array
24
     */
25
    private $_listeners = [];
26
27
    /**
28
     * Core config
29
     * 
30
     * @var array
31
     */
32
    public static $config = [];
33
34
    /**
35
     * Static storage for anything
36
     * 
37
     * @var array 
38
     */
39
    public static $storage = [];
40
41
    /**
42
     * Stop executing code if this true after use Inji::$inst->stop() constuction in code
43
     * 
44
     * @var boolean
45
     */
46
    public $exitOnStop = true;
47
48
    /**
49
     * Add event listener
50
     * 
51
     * @param string $eventName
52
     * @param string $listenCode
53
     * @param array|closure $callback
54
     * @param boolean $save
55
     */
56 3
    public function listen($eventName, $listenCode, $callback, $save = false) {
57 3
        if ($save) {
58 1
            $config = Config::custom(App::$primary->path . '/config/events.php');
59 1
            $config[$eventName][$listenCode] = serialize($callback);
60 1
            Config::save(App::$primary->path . '/config/events.php', $config);
61 1
        } else {
62 2
            $this->_listeners[$eventName][$listenCode] = $callback;
63
        }
64 3
    }
65
66
    /**
67
     * Throw event
68
     * 
69
     * @param string $eventName
70
     * @param mixed $eventObject
71
     * @return mixed
72
     */
73 3
    public function event($eventName, $eventObject = null) {
74
        $event = [
75 3
            'eventName' => $eventName,
76 3
            'eventObject' => $eventObject,
77 3
        ];
78
79 3
        $listeners = [];
80 3
        if (!empty($this->_listeners[$eventName])) {
81 2
            $listeners = $this->_listeners[$eventName];
82 2
        }
83 3
        $config = Config::custom(App::$primary->path . '/config/events.php');
84 3
        if (!empty($config[$eventName])) {
85 1
            foreach ($config[$eventName] as $listenCode => $callback) {
86 1
                $listeners[$listenCode] = (@unserialize($callback) !== false) ? unserialize($callback) : $callback;
87 1
            }
88 1
        }
89 3
        if ($listeners) {
90 3
            $iteration = 0;
91 3
            $calledBefore = [];
92 3
            foreach ($listeners as $listenCode => $callback) {
93 3
                $event['iteration'] = ++$iteration;
94 3
                $event['calledBefore'] = $calledBefore;
95 3
                if (is_callable($callback)) {
96 2
                    $event['eventObject'] = $callback($event);
97 3
                } elseif (is_array($callback) && isset($callback['callback'])) {
98 1
                    $event['eventObject'] = $callback['callback']($event, $callback);
99 1
                } else {
100
                    $event['eventObject'] = App::$cur->{$callback['module']}->{$callback['method']}($event, $callback);
101
                }
102 3
                $calledBefore[$iteration] = $listenCode;
103 3
            }
104 3
        }
105 3
        return $event['eventObject'];
106
    }
107
108
    /**
109
     * Unlisten event
110
     * 
111
     * @param string $eventName
112
     * @param string $listenCode
113
     * @param boolean $save
114
     */
115 2
    public function unlisten($eventName, $listenCode, $save = false) {
116 2
        if ($save) {
117 1
            $config = Config::custom(App::$primary->path . '/config/events.php');
118 1
            if (!empty($config[$eventName][$listenCode])) {
119 1
                unset($config[$eventName][$listenCode]);
120 1
                Config::save(App::$primary->path . '/config/events.php', $config);
121 1
            }
122 1
        }
123 2
        if (!empty($this->_listeners[$eventName][$listenCode])) {
124 1
            unset($this->_listeners[$eventName][$listenCode]);
125 1
        }
126 2
    }
127
128
    public function stop() {
129
        if ($this->exitOnStop) {
130
            exit();
1 ignored issue
show
Coding Style Compatibility introduced by
The method stop() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
131
        }
132
        return false;
133
    }
134
}