EventManager::on()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Ffcms\Core\Managers;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Any;
7
8
/**
9
 * Class EventManager. Control and run events.
10
 * @package \Ffcms\Core\Event
11
 * @author zenn
12
 */
13
class EventManager
14
{
15
    /** @var array $events */
16
    private $events;
17
    private $runned;
18
19
    /**
20
     * EventManager constructor. Get always initiated data from memory storage.
21
     */
22
    public function __construct()
23
    {
24
        // get events from memory object saver
25
        $this->events = App::$Memory->get('events.catched.save');
26
        $this->runned = App::$Memory->get('events.runned.save');
27
    }
28
29
    /**
30
     * Catch the event if it occurred after this initiation of interception
31
     * @param string|array $event
32
     * @param \Closure $callback
33
     */
34
    public function on($event, \Closure $callback): void
35
    {
36
        // check if event is a single string and parse it to array single item
37
        if (!Any::isArray($event)) {
38
            $event = [$event];
39
        }
40
        
41
        foreach ($event as $item) {
42
            $this->events[$item][] = $callback;
43
        }
44
    }
45
46
    /**
47
     * Catch the event if it occurred before the initiation of interception
48
     * @param string|array $event
49
     * @param \Closure $callback
50
     * @return void
51
     */
52
    public function listen($event, \Closure $callback): void
53
    {
54
        // check if $event is a single string and set it as array with one item
55
        if (!Any::isArray($event)) {
56
            $event = [$event];
57
        }
58
        
59
        // each every one event in array
60
        foreach ($event as $item) {
61
            if (Any::isArray($this->runned) && array_key_exists($item, $this->runned)) {
0 ignored issues
show
Bug introduced by
It seems like $this->runned can also be of type null; however, parameter $search of array_key_exists() does only seem to accept array, 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

61
            if (Any::isArray($this->runned) && array_key_exists($item, /** @scrutinizer ignore-type */ $this->runned)) {
Loading history...
62
                call_user_func_array($callback, $this->runned[$item]);
63
            }
64
        }
65
    }
66
    
67
    /**
68
     * Process event on happens
69
     * @return void
70
     */
71
    public function run(): void
72
    {
73
        // dynamicly parse input params
74
        $args = func_get_args();
75
        
76
        if (count($args) < 1) {
77
            return;
78
        }
79
        
80
        // get event name
81
        $eventName = array_shift($args);
82
        // get event args as array if passed
83
        $eventArgs = @array_shift($args);
84
85
        // if event is registered
86
        if (isset($this->events[$eventName]) && Any::isArray($this->events[$eventName])) {
87
            foreach ($this->events[$eventName] as $callback) {
88
                // call anonymous function with args if passed
89
                call_user_func_array($callback, $eventArgs);
90
            }
91
        }
92
        
93
        // set to post runned actions
94
        $this->runned[$eventName] = $eventArgs;
95
    }
96
97
    /**
98
     * Save events data in memory to prevent any sh@ts ;D
99
     */
100
    public function __destruct()
101
    {
102
        App::$Memory->set('events.catched.save', $this->events);
103
        App::$Memory->set('events.runned.save', $this->runned);
104
    }
105
}
106