EventListenerCollection::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace PhpDDD\Domain\Event\Listener;
4
5
use ArrayIterator;
6
use Countable;
7
use IteratorAggregate;
8
use PhpDDD\Domain\Exception\RuntimeException;
9
10
class EventListenerCollection implements IteratorAggregate, Countable
11
{
12
    /**
13
     * @var string
14
     */
15
    private $eventName;
16
17
    /**
18
     * @var EventListenerInterface[]
19
     */
20
    private $eventListeners = array();
21
22
    /**
23
     * @param string $eventName
24
     */
25
    public function __construct($eventName)
26
    {
27
        $this->eventName = $eventName;
28
    }
29
30
    /**
31
     * @return EventListenerInterface[]
0 ignored issues
show
Documentation introduced by
Should the return type not be ArrayIterator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33
    public function getIterator()
34
    {
35
        return new ArrayIterator($this->eventListeners);
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getEventName()
42
    {
43
        return $this->eventName;
44
    }
45
46
    /**
47
     * @param EventListenerInterface $eventListener
48
     */
49
    public function add(EventListenerInterface $eventListener)
50
    {
51
        if ($eventListener->getSupportedEventClassName() !== $this->eventName) {
52
            throw new RuntimeException(
53
                sprintf(
54
                    'EventListener %s does not support event %s.',
55
                    get_class($eventListener),
56
                    $this->eventName
57
                )
58
            );
59
        }
60
61
        $this->eventListeners[] = $eventListener;
62
    }
63
64
    /**
65
     * @return int The custom count as an integer.
66
     */
67
    public function count()
68
    {
69
        return count($this->eventListeners);
70
    }
71
}
72