Event::subscribe()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace LE_ACME2\Utilities;
4
5
use LE_ACME2\SingletonTrait;
6
7
class Event {
8
9
    const EVENT_CONNECTOR_WILL_REQUEST = 'EVENT_CONNECTOR_WILL_REQUEST';
10
11
    use SingletonTrait;
12
13
    private $_subscriber = [];
14
15
    /**
16
     * @param callable $callable function(string $event, array $payload = null)
17
     */
18
    public function subscribe(string $event, callable $callable) : void {
19
20
        if(!isset($this->_subscriber[$event])) {
21
            $this->_subscriber[$event] = [];
22
        }
23
24
        $this->_subscriber[$event][] = $callable;
25
    }
26
    
27
    public function trigger(string $event, array $payload = null) : void {
28
29
        Logger::getInstance()->add(Logger::LEVEL_DEBUG, 'Event triggered: ' . $event);
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        Logger::getInstance()->/** @scrutinizer ignore-call */ add(Logger::LEVEL_DEBUG, 'Event triggered: ' . $event);
Loading history...
30
31
        if(!isset($this->_subscriber[$event])) {
32
            return;
33
        }
34
35
        foreach($this->_subscriber[$event] as $callable) {
36
            $callable($event, $payload);
37
        }
38
    }
39
}