Completed
Push — 2.x ( a081e5 )
by Daniel
15:13
created

ProvidesEventsInputFilter::getEventManager()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 8.2222
cc 7
eloc 12
nc 5
nop 0
1
<?php
2
namespace ZfcUser\InputFilter;
3
use Zend\InputFilter\InputFilter;
4
use Zend\EventManager\EventManagerInterface;
5
use Zend\EventManager\EventManager;
6
class ProvidesEventsInputFilter extends InputFilter
7
{
8
    /**
9
     * @var EventManagerInterface
10
     */
11
    protected $events;
12
    /**
13
     * Set the event manager instance used by this context
14
     *
15
     * @param  EventManagerInterface $events
16
     * @return mixed
17
     */
18
    public function setEventManager(EventManagerInterface $events)
19
    {
20
        $this->events = $events;
21
        return $this;
22
    }
23
    /**
24
     * Retrieve the event manager
25
     *
26
     * Lazy-loads an EventManager instance if none registered.
27
     *
28
     * @return EventManagerInterface
29
     */
30
    public function getEventManager()
31
    {
32
        if (!$this->events instanceof EventManagerInterface) {
33
            $identifiers = array(__CLASS__, get_called_class());
34
            if (isset($this->eventIdentifier)) {
35
                if ((is_string($this->eventIdentifier))
36
                    || (is_array($this->eventIdentifier))
37
                    || ($this->eventIdentifier instanceof \Traversable)
38
                ) {
39
                    $identifiers = array_unique($identifiers + (array) $this->eventIdentifier);
40
                } elseif (is_object($this->eventIdentifier)) {
0 ignored issues
show
Bug introduced by
The property eventIdentifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
                    $identifiers[] = $this->eventIdentifier;
42
                }
43
                // silently ignore invalid eventIdentifier types
44
            }
45
            $this->setEventManager(new EventManager(null, $identifiers));
46
        }
47
        return $this->events;
48
    }
49
}
50