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

EventsEmitter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 78
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A bootEvents() 0 6 1
A bootListeners() 0 8 2
A emit() 0 4 1
A on() 0 4 1
A off() 0 4 1
A makeStatisticsStorage() 0 11 3
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