ClassDispatcher::connect()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
c 4
b 4
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 6
nc 4
nop 2
1
<?php
2
3
namespace PEIP\Dispatcher;
4
5
use PEIP\Util\Reflection;
6
use PEIP\Util\Test;
7
8
class ClassDispatcher extends \PEIP\ABS\Dispatcher\MapDispatcher
9
{
10
    /**
11
     * Connects a listener to a given event-name.
12
     *
13
     * @param string                            $name     name of the class
14
     * @param callable|PEIP\INF\Handler\Handler $listener listener to connect
15
     *
16
     * @return
17
     */
18
    public function connect($name, $listener)
19
    {
20
        $name = is_object($name) ? get_class($name) : (string) $name;
21
        if (Test::assertClassOrInterfaceExists($name)) {
22
            parent::connect($name, $listener);
23
        } else {
24
            throw new \InvalidArgumentException($name.' is not an Class nor Interface');
25
        }
26
    }
27
28
    /**
29
     * notifies all listeners on a event on a subject.
30
     *
31
     * @param string $name    name of the class
32
     * @param mixed  $subject the subject
33
     *
34
     * @return
35
     */
36 View Code Duplication
    public function notify($name, $subject)
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...
37
    {
38
        $res = false;
39
        foreach (Reflection::getImplementedClassesAndInterfaces($name) as $cls) {
40
            if (parent::hasListeners($cls)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (hasListeners() instead of notify()). Are you sure this is correct? If so, you might want to change this to $this->hasListeners().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
41
                self::doNotify($this->getListeners($cls), $subject);
42
                $res = true;
43
            }
44
        }
45
46
        return $res;
47
    }
48
49
    /**
50
     * notifies all listeners on a event on a subject until one returns a boolean true value.
51
     *
52
     * @param string $name    name of the event
53
     * @param mixed  $subject the subject
54
     *
55
     * @return \PEIP\INF\Handler\Handler listener which returned a boolean true value
56
     */
57 View Code Duplication
    public function notifyUntil($name, $subject)
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...
58
    {
59
        $res = null;
60
        foreach (Reflection::getImplementedClassesAndInterfaces($name) as $cls) {
61
            if (!$res && parent::hasListeners($cls)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (hasListeners() instead of notifyUntil()). Are you sure this is correct? If so, you might want to change this to $this->hasListeners().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
62
                $res = self::doNotifyUntil($this->getListeners($cls), $subject);
63
            }
64
        }
65
66
        return $res;
67
    }
68
69
    /**
70
     * notifies all listeners on a event on a subject.
71
     *
72
     * @param mixed $subject the subject
73
     *
74
     * @return
75
     */
76
    public function notifyOfInstance($subject)
77
    {
78
        return $this->notify(get_class($subject), $subject);
79
    }
80
81
    /**
82
     * Checks wether any listener is registered for a given event-name.
83
     *
84
     * @param string $name name of the event
85
     *
86
     * @return bool wether any listener is registered for event-name
87
     */
88
    public function hasListeners($name)
89
    {
90
        foreach (Reflection::getImplementedClassesAndInterfaces($name) as $cls) {
91
            if (parent::hasListeners($cls)) {
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
}
99