Completed
Push — 4.0 ( 63fdb6...88351d )
by Marco
05:36
created

EventsManager::subscribeOnce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

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

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
            "CALLABLE" => $callable,
51
            "PRIORITY" => $priority
52
        ));
53
54
        return $this->addListener($event, $callable, $priority);
55
56
    }
57
58 View Code Duplication
    public function subscribeOnce($event, $class, $method = null, $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...
59
60
        $callable = ( is_null($method) ) ? $class : array($class, $method);
61
62
        $this->logger->debug("Subscribing once handler $calss to event $event", array(
0 ignored issues
show
Bug introduced by
The variable $calss does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
            "CALLABLE" => $callable,
64
            "PRIORITY" => $priority
65
        ));
66
67
        return $this->addOneTimeListener($event, $callable, $priority);
68
69
    }
70
71
    public function unsubscribe($event, $class = null, $method = null) {
72
73
        if ( is_null($class) ) {
74
75
            $this->logger->debug("Unsubscribing all handlers from event $event");
76
77
            return $this->removeAllListeners($event);
78
79
        } else {
80
81
            $callable = ( is_null($method) ) ? $class : array($class, $method);
82
83
            $this->logger->debug("Unsubscribing handler $calss from event $event", array(
0 ignored issues
show
Bug introduced by
The variable $calss does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
84
                "CALLABLE" => $callable
85
            ));
86
87
            return $this->removeListener($event, $callable);
88
89
        }
90
91
    }
92
93
    public function load($plugins) {
94
95
        if ( !empty($plugins) ) {
96
97
            foreach( $plugins as $name => $event ) {
98
99
                $callable = ( is_null($event['method']) ) ? $event["class"] : array($event["class"], $event["method"]);
100
101
                $this->addListener($event["event"], $callable, $event["priority"]);
102
103
            }
104
105
        }
106
107
        return $this;
108
109
    }
110
111
}
112