ObjectEventDispatcher::buildAndNotify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 15
Ratio 88.24 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
c 4
b 4
f 0
dl 15
loc 17
rs 9.4285
cc 2
eloc 11
nc 2
nop 5
1
<?php
2
3
namespace PEIP\Dispatcher;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/*
14
 * ObjectEventDispatcher
15
 * Event dispatcher for an abritrary amount of different objects and events.
16
 * Contrary to it�s parent class \PEIP\Dispatcher\ObjectMapDispatcher) this class can
17
 * create event-objects (\PEIP\INF\Event\Event) with the notification subject as content/subject
18
 * and notify it�s listners on the event-objects. Also this class only accepts instaces of
19
 * \PEIP\INF\Event\Event as it�s notification subjects.
20
 *
21
 * @author Timo Michna <timomichna/yahoo.de>
22
 * @package PEIP
23
 * @subpackage dispatcher
24
 * @extends \PEIP\Dispatcher\ObjectMapDispatcher
25
 * @implements \PEIP\INF\Dispatcher\ObjectMapDispatcher
26
 */
27
28
use PEIP\Event\EventBuilder;
29
30
class ObjectEventDispatcher extends \PEIP\Dispatcher\ObjectMapDispatcher
31
{
32
    /**
33
     * Notifies all listeners of a given event-object.
34
     *
35
     * @param string                $name   name of the event
36
     * @param \PEIP\INF\Event\Event $object an event object
37
     *
38
     * @return bool|null
39
     */
40
    public function notify($name, $object)
41
    {
42
        if ($object instanceof \PEIP\INF\Event\Event) {
43
            if (is_object($object->getContent())) {
44
                if ($this->hasListeners($name, $object->getContent())) {
45
                    return self::doNotify($this->getListeners($name, $object->getContent()), $object);
46
                }
47
            } else {
48
                throw new \InvalidArgumentException('instance of \PEIP\INF\Event\Event must contain subject');
49
            }
50
        } else {
51
            throw new \InvalidArgumentException('object must be instance of \PEIP\INF\Event\Event');
52
        }
53
    }
54
55
    /**
56
     * Creates an event-object with given object as content/subject and notifies
57
     * all registers listeners of the event.
58
     *
59
     * @param string $name       name of the event
60
     * @param object $object     the subject of the event
61
     * @param array  $headers    headers of the event-object as key/value pairs
62
     * @param string $eventClass event-class to create instances from
63
     *
64
     * @return
65
     *
66
     * @see EventBuilder
67
     */
68 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...
69
    {
70
        if (!$this->hasListeners($name, $object)) {
71
            return false;
72
        }
73
        $event = EventBuilder::getInstance($eventClass)->build(
0 ignored issues
show
Bug introduced by
It seems like $eventClass defined by parameter $eventClass on line 68 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...
74
            $object,
75
            $name,
76
            $headers
77
        );
78
        $this->notify(
79
                $name,
80
                $event
81
        );
82
83
        return $event;
84
    }
85
}
86