Completed
Push — 4.0 ( 38d142...8ab097 )
by Marco
03:25
created

EventsManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
use \Psr\Log\LoggerInterface;
4
use \League\Event\Emitter;
5
use \League\Event\ListenerInterface;
6
7
/**
8
 * @package     Comodojo Dispatcher
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @author      Marco Castiello <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
30
class EventsManager extends Emitter {
31
32
    public $logger;
33
34 1
    public function __construct(LoggerInterface $logger) {
35
36 1
        $this->logger = $logger;
37
38 1
    }
39
40 1 View Code Duplication
    public function subscribe($event, $class, $priority = 0) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
42 1
        $callable = $this->convertToListener($class, $event);
43
44 1
        if ( $callable === false ) return null;
45
46 1
        return $this->addListener($event, $callable, $priority);
0 ignored issues
show
Documentation introduced by
$callable is of type object<League\Event\ListenerInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
48
    }
49
50 View Code Duplication
    public function subscribeOnce($event, $class, $priority = 0) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
52
        $callable = $this->convertToListener($class, $event);
53
54
        if ( $callable === false ) return null;
55
56
        return $this->addOneTimeListener($event, $callable, $priority);
0 ignored issues
show
Documentation introduced by
$callable is of type object<League\Event\ListenerInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
58
    }
59
60 1
    protected function convertToListener($class, $event) {
61
62 1
        if ( !class_exists($class) ) {
63
64
            $this->logger->error("Cannot subscribe class $class to event $event: cannot find class");
65
66
            return false;
67
68
        }
69
70 1
        $callable = new $class();
71
72 1
        if ($callable instanceof ListenerInterface) {
73
74 1
            $this->logger->debug("Subscribing handler $class to event $event");
75
76 1
            return $callable;
77
78
        }
79
80
        $this->logger->error("Cannot subscribe class $class to event $event: class is not an instance of \League\Event\ListenerInterface");
81
82
        return false;
83
84
    }
85
86
    public function load($plugins) {
87
88
        if ( !empty($plugins) ) {
89
90
            foreach( $plugins as $name => $plugin ) {
91
92
                if ( !isset($plugin['class']) || !isset($plugin["event"]) ) {
93
94
                    $this->logger->error("Invalid plugin definition", $plugin);
95
96
                    continue;
97
98
                }
99
100
                $priority = isset($plugin['priority']) ? $plugin['priority'] : 0;
101
                $onetime = isset($plugin['onetime']) ? $plugin['onetime'] : 0;
102
103
                if ( $onetime ) $this->subscribeOnce($plugin['event'], $plugin['class'], $priority);
104
                else $this->subscribe($plugin['event'], $plugin['class'], $priority);
105
106
            }
107
108
        }
109
110
    }
111
112
}
113