Passed
Push — master ( 70bf0a...4ee951 )
by Marwan
01:37
created

Event   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 57
ccs 16
cts 16
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegisteredEvents() 0 3 1
A listen() 0 3 1
A dispatch() 0 16 5
1
<?php
2
3
/**
4
 * @author Marwan Al-Soltany <[email protected]>
5
 * @copyright Marwan Al-Soltany 2021
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace MAKS\Velox\Backend;
13
14
/**
15
 * A class that offers simple events handling functionality (dispatching and listening).
16
 *
17
 * Example:
18
 * ```
19
 * // listening on an event
20
 * Event::listen('some.event', function ($arg1, $arg2) {
21
 *     // do some stuff ...
22
 * });
23
 *
24
 * // dispatching an event
25
 * Event::dispatch('some.event', [$arg1, $arg2]);
26
 * ```
27
 *
28
 * @since 1.2.0
29
 */
30
class Event
31
{
32
    /**
33
     * Here live all bindings.
34
     */
35
    protected static array $events = [];
36
37
38
    /**
39
     * Dispatches the passed event by executing all attached callbacks after passing them the passed arguments.
40
     *
41
     * @param string $event Event name.
42
     * @param array $arguments [optional] Arguments array. Note that the arguments will be spread (`...$args`) on the callback.
43
     * @param object|null $callbackThis [optional] The object the callback should be bound to.
44
     *
45
     * @return void
46
     */
47 19
    public static function dispatch(string $event, ?array $arguments = null, ?object $callbackThis = null): void
48
    {
49 19
        if (isset(static::$events[$event]) && count(static::$events[$event])) {
50 16
            $callbacks = &static::$events[$event];
51 16
            foreach ($callbacks as $callback) {
52 16
                $parameters = array_merge(array_values($arguments ?? []), [$event]);
53
54 16
                if ($callbackThis) {
55 1
                    $callback->call($callbackThis, ...$parameters);
56 1
                    continue;
57
                }
58
59 16
                $callback(...$parameters);
60
            }
61
        } else {
62 12
            static::$events[$event] = [];
63
        }
64 19
    }
65
66
    /**
67
     * Listens on the passed event and attaches the passed callback to it.
68
     *
69
     * @param string $event Event name.
70
     * @param callable $callback A callback to process the event.
71
     *
72
     * @return void
73
     */
74 1
    public static function listen(string $event, callable $callback): void
75
    {
76 1
        static::$events[$event][] = \Closure::fromCallable($callback);
77 1
    }
78
79
    /**
80
     * Returns array of all registered events as an array `['event.name' => [...$callbacks]]`.
81
     *
82
     * @return array
83
     */
84 1
    public static function getRegisteredEvents(): array
85
    {
86 1
        return static::$events;
87
    }
88
}
89