Completed
Push — 4.0 ( 610126...89799b )
by Marco
15:21
created

EventsManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 89
Duplicated Lines 20.22 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 2
dl 18
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A logger() 0 5 1
A subscribe() 9 9 2
A subscribeOnce() 9 9 2
B convertToListener() 0 25 3
C load() 0 25 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Dispatcher\Components;
2
3
use \Monolog\Logger;
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
    protected $logger;
33
34
    public function __construct(Logger $logger) {
35
36
        $this->logger = $logger;
37
38
    }
39
40
    public function logger() {
41
42
        return $this->logger;
43
44
    }
45
46 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...
47
48
        $callable = $this->convertToListener($class, $event);
49
50
        if ( $callable === false ) return null;
51
52
        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...
53
54
    }
55
56 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...
57
58
        $callable = $this->convertToListener($class, $event);
59
60
        if ( $callable === false ) return null;
61
62
        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...
63
64
    }
65
66
    protected function convertToListener($class, $event) {
67
68
        if ( !class_exists($class) ) {
69
70
            $this->logger->error("Cannot subscribe class $class to event $event: cannot find class");
71
72
            return false;
73
74
        }
75
76
        $callable = new $class();
77
78
        if ($callable instanceof ListenerInterface) {
79
80
            $this->logger->debug("Subscribing handler $class to event $event");
81
82
            return $callable;
83
84
        }
85
86
        $this->logger->error("Cannot subscribe class $class to event $event: class is not an instance of \League\Event\ListenerInterface");
87
88
        return false;
89
90
    }
91
92
    public function load($plugins) {
93
94
        if ( !empty($plugins) ) {
95
96
            foreach( $plugins as $name => $plugin ) {
97
98
                if ( !isset($plugin['class']) || !isset($plugin["event"]) ) {
99
100
                    $this->logger->error("Invalid plugin definition", $plugin);
101
102
                    continue;
103
104
                }
105
106
                $priority = isset($plugin['priority']) ? $plugin['priority'] : 0;
107
                $onetime = isset($plugin['onetime']) ? $plugin['onetime'] : 0;
108
109
                if ( $onetime ) $this->subscribeOnce($plugin['event'], $plugin['class'], $priority);
110
                else $this->subscribe($plugin['event'], $plugin['class'], $priority);
111
112
            }
113
114
        }
115
116
    }
117
118
}
119