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

Inji   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 90.74%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 49
cts 54
cp 0.9074
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A listen() 0 9 2
D event() 0 34 10
A unlisten() 0 12 4
A stop() 0 6 2
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
}