ClassEventDispatcher::buildAndNotify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 15
Ratio 93.75 %

Importance

Changes 5
Bugs 5 Features 0
Metric Value
c 5
b 5
f 0
dl 15
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 5
1
<?php
2
3
namespace PEIP\Dispatcher;
4
5
/*
6
 * To change this template, choose Tools | Templates
7
 * and open the template in the editor.
8
 */
9
10
/*
11
 * Description of ClassEventDispatcher
12
 *
13
 * @author timo
14
 */
15
use PEIP\Event\EventBuilder;
16
use PEIP\Util\Reflection;
17
18
class ClassEventDispatcher extends \PEIP\Dispatcher\ObjectEventDispatcher
19
{
20
    /**
21
     * connects a Handler to an event on an object.
22
     *
23
     * @param string                            $name     the event-name
24
     * @param object                            $object   arbitrary object to connect to
25
     * @param callable|PEIP\INF\Handler\Handler $listener event-handler
26
     *
27
     * @return bool
28
     */
29
    public function connect($name, $object, $listener)
30
    {
31
        $class = is_object($object) ? get_class($object) : (string) $object;
32
        foreach (Reflection::getImplementedClassesAndInterfaces($object) as $cls) {
33
            $reflection = \PEIP\Util\ReflectionPool::getInstance($class);
34
            parent::connect($name, $reflection, $listener);
35
        }
36
37
        return true;
38
    }
39
40
    public function disconnect($name, $object, $listener)
41
    {
42
        $class = is_object($object) ? get_class($object) : (string) $object;
43
        $res = true;
44
        foreach (Reflection::getImplementedClassesAndInterfaces($object) as $cls) {
45
            $reflection = \PEIP\Util\ReflectionPool::getInstance($class);
46
            $r = parent::disconnect($name, $reflection, $listener);
47
            if (!$r) {
48
                $res = false;
49
            }
50
        }
51
52
        return $res;
53
    }
54
55
    /**
56
     * Notifies all listeners of a given event-object.
57
     *
58
     * @param string                $name   name of the event
59
     * @param \PEIP\INF\Event\Event $object an event object
60
     *
61
     * @return bool|null
62
     */
63 View Code Duplication
    public function notify($name, $object)
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...
64
    {
65
        if ($object instanceof \PEIP\INF\Event\Event) {
66
            if (is_object($object->getContent())) {
67
                return self::doNotify(
68
                    $this->getListeners(
69
                        $name,
70
                        \PEIP\Util\ReflectionPool::getInstance(
71
                            $object->getContent()
72
                        )
73
                        ),
74
                        $object
75
                    );
76
            } else {
77
                throw new \InvalidArgumentException('instance of \PEIP\INF\Event\Event must contain subject');
78
            }
79
        } else {
80
            throw new \InvalidArgumentException('object must be instance of \PEIP\INF\Event\Event');
81
        }
82
    }
83
84
       //put your code here
85
86
    /**
87
     * Creates an event-object with given object as content/subject and notifies
88
     * all registers listeners of the event.
89
     *
90
     * @param string $name       name of the event
91
     * @param object $object     the subject of the event
92
     * @param array  $headers    headers of the event-object as key/value pairs
93
     * @param string $eventClass event-class to create instances from
94
     *
95
     * @return
96
     */
97 View Code Duplication
    public function buildAndNotify($name, $object, array $headers = [], $eventClass = false, $type = false)
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...
98
    {
99
        if (!$this->hasListeners($name, ($object))) {
100
            return false;
101
        }
102
103
        return $this->notify(
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->notify($name, \PE...ders, (string) $type)); of type boolean|null adds the type boolean to the return on line 103 which is incompatible with the return type of the parent method PEIP\Dispatcher\ObjectEv...patcher::buildAndNotify of type false|object.
Loading history...
104
                $name,
105
                EventBuilder::getInstance($eventClass)->build(
0 ignored issues
show
Bug introduced by
It seems like $eventClass defined by parameter $eventClass on line 97 can also be of type false; however, PEIP\Event\EventBuilder::getInstance() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
106
                    $object,
107
                    $name,
108
                    $headers,
109
                    (string) $type
0 ignored issues
show
Unused Code introduced by
The call to EventBuilder::build() has too many arguments starting with (string) $type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
                )
111
        );
112
    }
113
114
    /**
115
     * Checks wether an object has a listener for an event.
116
     *
117
     * @param string $name   the event-name
118
     * @param object $object object to check for listeners
119
     *
120
     * @return bool
121
     */
122
    public function hasListeners($name, $object)
123
    {
124
        return parent::hasListeners(
125
            $name,
126
            \PEIP\Util\ReflectionPool::getInstance($object)
127
        );
128
    }
129
}
130