Completed
Push — master ( 65034b...7b1ef3 )
by Maxim
02:33
created

EventsEmitter::on()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Notimatica\Driver\Support;
4
5
use League\Event\Emitter;
6
use League\Event\EventInterface;
7
use League\Event\ListenerInterface;
8
use Notimatica\Driver\StatisticsStorages\AbstractStorage;
9
use Notimatica\Driver\StatisticsStoragesFactory;
10
11
trait EventsEmitter
12
{
13
    /**
14
     * @var Emitter
15
     */
16
    public static $events;
17
18
    /**
19
     * Boot events emitter.
20
     */
21 9
    public function bootEvents()
22
    {
23 9
        static::$events = new Emitter();
24
25 9
        $this->bootListeners();
26 9
    }
27
28
    /**
29
     * Boot event listeners.
30
     */
31 9
    protected function bootListeners()
32
    {
33 9
        $statisticsStorage = $this->makeStatisticsStorage();
34
35 9
        if (!is_null($statisticsStorage)) {
36 9
            static::$events->useListenerProvider($statisticsStorage);
37 9
        }
38 9
    }
39
40
    /**
41
     * Emit event.
42
     *
43
     * @return \League\Event\EventInterface|string
44
     */
45 4
    public static function emit()
46
    {
47 4
        return call_user_func_array([static::$events, 'emit'], func_get_args());
48
    }
49
50
    /**
51
     * Emit event.
52
     *
53
     * @param  string $event
54
     * @param  ListenerInterface|callable $listener
55
     * @return EventInterface|string
56
     */
57 1
    public static function on($event, $listener)
58
    {
59 1
        return static::$events->addListener($event, $listener);
60
    }
61
62
    /**
63
     * Remove listeners.
64
     *
65
     * @param $event
66
     */
67 1
    public static function off($event)
68
    {
69 1
        static::$events->removeAllListeners($event);
70 1
    }
71
72
    /**
73
     * Make statistics storage.
74
     *
75
     * @return AbstractStorage|null
76
     */
77 9
    protected function makeStatisticsStorage()
78
    {
79 9
        if (!empty($this->project->config['statistics']['storage'])
80 9
            || class_exists($this->project->config['statistics']['storage'])) {
81 9
            $storage = $this->project->config['statistics']['storage'];
0 ignored issues
show
Bug introduced by
The property project does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
83 9
            return new $storage;
84
        }
85
86
        return null;
87
    }
88
}
89