Passed
Pull Request — master (#14)
by Alex
03:06
created

EventDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher;
6
7
use Arp\EventDispatcher\Listener\AddableListenerProviderInterface;
8
use Arp\EventDispatcher\Listener\AddListenerAwareInterface;
9
use Arp\EventDispatcher\Listener\Exception\EventListenerException;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\EventDispatcher
14
 */
15
final class EventDispatcher extends AbstractEventDispatcher implements AddListenerAwareInterface
16
{
17
    /**
18
     * @var AddableListenerProviderInterface
19
     */
20
    protected $listenerProvider;
21
22
    /**
23
     * @param AddableListenerProviderInterface $listenerProvider
24
     */
25
    public function __construct(AddableListenerProviderInterface $listenerProvider)
26
    {
27
        $this->listenerProvider = $listenerProvider;
28
    }
29
30
    /**
31
     * Add a new event listener to the collection.
32
     *
33
     * @param object|string $event    The event that should be attached to.
34
     * @param callable      $listener The event listener to attach.
35
     * @param int           $priority The event priority.
36
     *
37
     * @throws EventListenerException  If the event listener cannot be added.
38
     */
39
    public function addListenerForEvent($event, callable $listener, int $priority = 1): void
40
    {
41
        $this->listenerProvider->addListenerForEvent($event, $listener, $priority);
42
    }
43
44
    /**
45
     * Add a collection of event listeners for a single event.
46
     *
47
     * @param object|string       $event     The event name or instance to attach to.
48
     * @param iterable|callable[] $listeners Collection of listeners to attach.
49
     * @param int                 $priority  Event priority to use for all $listeners. This will default to 1.
50
     *
51
     * @throws EventListenerException  If the event listeners cannot be added.
52
     */
53
    public function addListenersForEvent($event, iterable $listeners, int $priority = 1): void
54
    {
55
        $this->listenerProvider->addListenersForEvent($event, $listeners, $priority);
56
    }
57
}
58