Passed
Branch develop (4d02c5)
by Schlaefer
02:39
created

Event::trigger()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
nc 3
nop 2
dl 0
loc 7
c 0
b 0
f 0
cc 3
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
1
<?php
2
/**
3
 * The Event class
4
 */
5
namespace Phile\Core;
6
7
use Phile\Gateway\EventObserverInterface;
8
9
/**
10
 * the Event class for implementing a hook/event system
11
 *
12
 * @author  PhileCMS
13
 * @link    https://philecms.com
14
 * @license http://opensource.org/licenses/MIT
15
 * @package Phile\Core
16
 */
17
class Event
18
{
19
20
    /**
21
     * @var Event global instance
22
     * @deprecated static use is deprecated
23
     */
24
    protected static $instance;
25
26
    /**
27
     * Registry object provides storage for objects.
28
     *
29
     * @var array
30
     */
31
    protected $registry = [];
32
33
    /**
34
     * get global event instance
35
     *
36
     * @return Event
37
     * @deprecated static use is deprectated
38
     */
39
    public static function getInstance()
40
    {
41
        return static::$instance;
42
    }
43
44
    /**
45
     * Set global event instance
46
     *
47
     * @param Event $instance
48
     * @deprecated static use is deprecated
49
     */
50 32
    public static function setInstance(Event $instance)
51
    {
52 32
        static::$instance = $instance;
53 32
    }
54
55
    /**
56
     * Global register
57
     *
58
     * @param string $eventName
59
     * @param EventObserverInterface|callable $object observer
60
     * @deprecated static use is deprecated
61
     */
62 3
    public static function registerEvent($eventName, $object)
63
    {
64 3
        static::$instance->register($eventName, $object);
65 2
    }
66
67
    /**
68
     * Global trigger
69
     *
70
     * @param string $eventName
71
     * @param array $data
72
     * @deprecated static use is deprecated
73
     */
74 2
    public static function triggerEvent($eventName, $data = null)
75
    {
76 2
        static::$instance->trigger($eventName, $data);
77 2
    }
78
79
    /**
80
     * method to register an event
81
     *
82
     * @param string $eventName the event to observe
83
     * @param EventObserverInterface|callable $object observer
84
     */
85 37
    public function register(string $eventName, $object): void
86
    {
87 37
        if ($object instanceof EventObserverInterface) {
88 35
            $object = [$object, 'on'];
89
        }
90 37
        if (!is_callable($object)) {
91 1
            throw new \InvalidArgumentException(
92 1
                "Can't register event. Observer is not callable.",
93 1
                1427814905
94
            );
95
        }
96 36
        $this->registry[$eventName][] = $object;
97 36
    }
98
99
    /**
100
     * method to trigger an event
101
     *
102
     * @param string $eventName the event name (register for this name)
103
     * @param array $data array with some additional data
104
     */
105 35
    public function trigger(string $eventName, array $data = null): void
106
    {
107 35
        if (empty($this->registry[$eventName])) {
108 28
            return;
109
        }
110 34
        foreach ($this->registry[$eventName] as $observer) {
111 34
            call_user_func_array($observer, [$eventName, $data]);
112
        }
113 33
    }
114
}
115