Events   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 12
c 1
b 0
f 1
dl 0
loc 48
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 7 2
A keys() 0 3 1
A has() 0 3 1
A notify() 0 6 2
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