Event::on()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * This file is part of dimtrovich/db-dumper".
5
 *
6
 * (c) 2024 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\DbDumper;
13
14
class Event
15
{
16
    /**
17
     * @var array<string, list<callable>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<callable>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
18
     */
19
    private array $listeners = [];
20
21
    /**
22
     * Register a listener for event
23
     */
24
    public function on(string $event, callable $callable): void
25
    {
26
        if (! array_key_exists($event, $this->listeners)) {
27
            $this->listeners[$event] = [];
28
        }
29
30
        $this->listeners[$event][] = $callable;
31
    }
32
33
    /**
34
     * Trigger event
35
     */
36
    public function emit(string $event, ...$args)
37
    {
38
        $listeners = $this->listeners[$event] ?? [];
39
40
        foreach ($listeners as $callable) {
41
            $callable(...$args);
42
        }
43
    }
44
}
45