EventManager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 25
dl 0
loc 78
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 9 3
A fire() 0 12 5
A trigger() 0 19 5
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\EventManager;
13
14
/**
15
 * Event manager
16
 *
17
 * @package  Bluz\EventManager
18
 * @link     https://github.com/bluzphp/framework/wiki/EventManager
19
 */
20
class EventManager
21
{
22
    /**
23
     * @var array list of listeners
24
     */
25
    protected $listeners = [];
26
27
    /**
28
     * Attach callback to event
29
     *
30
     * @param string $eventName
31
     * @param callable $callback
32
     * @param integer $priority
33
     *
34
     * @return void
35
     */
36 7
    public function attach(string $eventName, callable $callback, int $priority = 1): void
37
    {
38 7
        if (!isset($this->listeners[$eventName])) {
39 7
            $this->listeners[$eventName] = [];
40
        }
41 7
        if (!isset($this->listeners[$eventName][$priority])) {
42 7
            $this->listeners[$eventName][$priority] = [];
43
        }
44 7
        $this->listeners[$eventName][$priority][] = $callback;
45 7
    }
46
47
    /**
48
     * Trigger event
49
     *
50
     * @param  string|Event  $event
51
     * @param  string|object $target
52
     * @param  array|object  $params
53
     *
54
     * @return string|object
55
     * @throws EventException
56
     */
57 8
    public function trigger($event, $target = null, $params = null)
58
    {
59 8
        if (!$event instanceof Event) {
60 8
            $event = new Event($event, $target, $params);
61
        }
62
63 7
        if (false !== strpos($event->getName(), ':')) {
64 1
            $namespace = substr($event->getName(), 0, strpos($event->getName(), ':'));
65
66 1
            if (isset($this->listeners[$namespace])) {
67 1
                $this->fire($this->listeners[$namespace], $event);
68
            }
69
        }
70
71 7
        if (isset($this->listeners[$event->getName()])) {
72 7
            $this->fire($this->listeners[$event->getName()], $event);
73
        }
74
75 7
        return $event->getTarget();
76
    }
77
78
    /**
79
     * Fire!
80
     *
81
     * @param array $listeners
82
     * @param Event $event
83
     *
84
     * @return void
85
     */
86 7
    protected function fire(array $listeners, Event $event): void
87
    {
88 7
        ksort($listeners);
89 7
        foreach ($listeners as $list) {
90 7
            foreach ($list as $listener) {
91 7
                $result = $listener($event);
92 7
                if (null === $result) {
93
                    // continue;
94 3
                } elseif (false === $result) {
95 1
                    break 2;
96
                } else {
97 2
                    $event->setTarget($result);
98
                }
99
            }
100
        }
101 7
    }
102
}
103