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

EventProvider::setEventManager()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
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 18
rs 8.8571
cc 6
eloc 12
nc 4
nop 1
1
<?php
2
namespace ZfcUser\EventManager;
3
use Traversable;
4
use Zend\EventManager\EventManagerAwareInterface;
5
use Zend\EventManager\EventManagerInterface;
6
use Zend\EventManager\EventManager;
7
use Zend\EventManager\SharedEventManager;
8
9
abstract class EventProvider implements EventManagerAwareInterface
10
{
11
    /**
12
     * @var EventManagerInterface
13
     */
14
    protected $events;
15
    /**
16
     * Set the event manager instance used by this context
17
     *
18
     * @param  EventManagerInterface $events
19
     * @return mixed
20
     */
21
    public function setEventManager(EventManagerInterface $events)
22
    {
23
        $identifiers = array(__CLASS__, get_called_class());
24
        if (isset($this->eventIdentifier)) {
25
            if ((is_string($this->eventIdentifier))
26
                || (is_array($this->eventIdentifier))
27
                || ($this->eventIdentifier instanceof Traversable)
28
            ) {
29
                $identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier));
30
            } 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...
31
                $identifiers[] = $this->eventIdentifier;
32
            }
33
            // silently ignore invalid eventIdentifier types
34
        }
35
        $events->setIdentifiers($identifiers);
36
        $this->events = $events;
37
        return $this;
38
    }
39
    /**
40
     * Retrieve the event manager
41
     *
42
     * Lazy-loads an EventManager instance if none registered.
43
     *
44
     * @return EventManagerInterface
45
     */
46
    public function getEventManager()
47
    {
48
        if (!$this->events instanceof EventManagerInterface) {
49
            $this->setEventManager(new EventManager(new SharedEventManager()));
50
        }
51
        return $this->events;
52
    }
53
}
54