Completed
Push — master ( 2f8bc7...807794 )
by Mihail
02:57
created

EventManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Ffcms\Core\Managers;
3
4
use Ffcms\Core\App;
5
use Ffcms\Core\Helper\Type\Obj;
6
use Ffcms\Core\Helper\Type\Str;
7
use Ffcms\Core\Helper\FileSystem\Directory;
8
use Ffcms\Core\Helper\FileSystem\File;
9
10
/**
11
 * Class EventManager. Control and run events.
12
 * @package \Ffcms\Core\Event
13
 * @author zenn
14
 */
15
class EventManager
16
{
17
    /** @var array $events */
18
    private $events;
19
    private $runned;
20
21
    /**
22
     * EventManager constructor. Get always initiated data from memory storage.
23
     */
24
    public function __construct()
25
    {
26
        // get events from memory object saver
27
        $this->events = App::$Memory->get('events.catched.save');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Ffcms\Core\App::$Memory...('events.catched.save') of type * is incompatible with the declared type array of property $events.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        $this->runned = App::$Memory->get('events.runned.save');
29
    }
30
31
    /** Catch the event if it occurred after this initiation of interception
32
     * @param string|array $event            
33
     * @param \Closure $callback            
34
     */
35
    public function on($event, \Closure $callback)
36
    {
37
        // check if event is a single string and parse it to array single item
38
        if (!Obj::isArray($event)) {
39
            $event = [$event];
40
        }
41
        
42
        foreach ($event as $item) {
0 ignored issues
show
Bug introduced by
The expression $event of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
43
            $this->events[$item][] = $callback;
44
        }
45
    }
46
47
    /**
48
     * Catch the event if it occurred before the initiation of interception
49
     * @param string|array $event
50
     * @param \Closure $callback
51
     * @return mixed
52
     */
53
    public function listen($event, \Closure $callback)
54
    {
55
        // check if $event is a single string and set it as array with one item
56
        if (!Obj::isArray($event)) {
57
            $event = [$event];
58
        }
59
        
60
        // each every one event in array
61
        foreach ($event as $item) {
0 ignored issues
show
Bug introduced by
The expression $event of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

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