Events::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Net events
4
 * User: moyo
5
 * Date: 02/08/2017
6
 * Time: 12:04 PM
7
 */
8
9
namespace Carno\Net;
10
11
use Carno\Net\Contracts\Conn;
12
13
class Events
14
{
15
    /**
16
     * @var callable[][]
17
     */
18
    private $observers = [];
19
20
    /**
21
     * @param int $evk
22
     * @return bool
23
     */
24
    public function has(int $evk) : bool
25
    {
26
        return isset($this->observers[$evk]);
27
    }
28
29
    /**
30
     * @return string[]
31
     */
32
    public function keys() : array
33
    {
34
        return array_keys($this->observers);
35
    }
36
37
    /**
38
     * @param int $evk
39
     * @param callable[] $observers
40
     * @return self
41
     */
42
    public function attach(int $evk, callable ...$observers) : self
43
    {
44
        $this->has($evk)
45
            ? array_push($this->observers[$evk], ...$observers)
46
            : $this->observers[$evk] = $observers
47
        ;
48
        return $this;
49
    }
50
51
    /**
52
     * @param int $evk
53
     * @param Conn $conn
54
     */
55
    public function notify(int $evk, Conn $conn) : void
56
    {
57
        if ($this->has($evk)) {
58
            array_map(static function (callable $observer) use ($conn) {
59
                $observer($conn);
60
            }, $this->observers[$evk]);
61
        }
62
    }
63
}
64