EventsAwareTrait::setEvents()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 8
nop 1
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoEventLib;
12
13
use Traversable;
14
use Zend\EventManager\EventManagerInterface;
15
16
/**
17
 * Class EventsAwareTrait
18
 */
19
trait EventsAwareTrait
20
{
21
    /**
22
     * @var EventManagerInterface
23
     */
24
    protected $events;
25
26
    /**
27
     * Set the event manager instance used by this context.
28
     *
29
     * For convenience, this method will also set the class name / LSB name as
30
     * identifiers, in addition to any string or array of strings set to the
31
     * $this->eventIdentifier property.
32
     *
33
     * @param EventManagerInterface $events
34
     * @return mixed
35
     */
36
    public function setEvents(EventManagerInterface $events)
37
    {
38
        $identifiers = [__CLASS__, get_class($this)];
39
        if (isset($this->eventIdentifier)) {
40
            if ((is_string($this->eventIdentifier))
41
                || (is_array($this->eventIdentifier))
42
                || ($this->eventIdentifier instanceof Traversable)
43
            ) {
44
                $identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier));
45
            } 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...
46
                $identifiers[] = $this->eventIdentifier;
47
            }
48
            // silently ignore invalid eventIdentifier types
49
        }
50
        $events->setIdentifiers($identifiers);
51
        $this->events = $events;
52
        if (method_exists($this, 'attachDefaultListeners')) {
53
            $this->attachDefaultListeners();
0 ignored issues
show
Bug introduced by
It seems like attachDefaultListeners() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
        }
55
        return $this;
56
    }
57
58
    /**
59
     * Retrieve the event manager
60
     *
61
     * Lazy-loads an EventManager instance if none registered.
62
     *
63
     * @return EventManagerInterface
64
     */
65
    public function getEvents()
66
    {
67
        if (!$this->events instanceof EventManagerInterface) {
68
            $this->setEvents(new EventManager);
69
        }
70
        return $this->events;
71
    }
72
}
73