Completed
Pull Request — master (#70)
by
unknown
13:06
created

EventDispatcherBridge::addSubscriber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Tmdb\Laravel\EventDispatcher;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyDispatcher;
7
use Illuminate\Contracts\Events\Dispatcher as LaravelDispatcher;
8
9
/**
10
 * This adapter provides a Laravel integration for applications
11
 * using the Symfony EventDispatcherInterface
12
 * It passes any request on to a Symfony Dispatcher and only
13
 * uses the Laravel Dispatcher when dispatching events
14
 */
15
class EventDispatcherBridge implements SymfonyDispatcher
16
{
17
    /**
18
     * The Laravel Events Dispatcher
19
     * @var \Illuminate\Contracts\Events\Dispatcher or \Illuminate\Events\Dispatcher
20
     */
21
    protected $laravelDispatcher;
22
23
    /**
24
     * The Symfony Event Dispatcher
25
     * @var  \Symfony\Component\EventDispatcher\EventDispatcherInterface
26
     */
27
    protected $symfonyDispatcher;
28
29
    /**
30
     * Forward all methods to the Laravel Events Dispatcher
31
     * @param LaravelDispatcher $laravelDispatcher
32
     * @param SymfonyDispatcher $symfonyDispatcher
33
     */
34
    public function __construct(LaravelDispatcher $laravelDispatcher, SymfonyDispatcher $symfonyDispatcher)
35
    {
36
        $this->laravelDispatcher = $laravelDispatcher;
37
        $this->symfonyDispatcher = $symfonyDispatcher;
38
    }
39
40
    public function addListener(string $eventName, $listener, int $priority = 0)
41
    {
42
        $this->symfonyDispatcher->addListener($eventName, $listener, $priority);
43
    }
44
45
    public function addSubscriber(EventSubscriberInterface $subscriber)
46
    {
47
        $this->symfonyDispatcher->addSubscriber($subscriber);
48
    }
49
50
    public function removeListener(string $eventName, $listener)
51
    {
52
        $this->symfonyDispatcher->removeListener($eventName, $listener);
53
    }
54
55
    public function removeSubscriber(EventSubscriberInterface $subscriber)
56
    {
57
        $this->symfonyDispatcher->removeSubscriber($subscriber);
58
    }
59
60
    public function getListeners(string $eventName = null)
61
    {
62
        return $this->symfonyDispatcher->getListeners($eventName);
63
    }
64
65
    public function dispatch(object $event, string $eventName = null): object
66
    {
67
        $this->laravelDispatcher->dispatch($eventName, $event);
68
        return $this->symfonyDispatcher->dispatch($event, $eventName);
69
    }
70
71
    public function getListenerPriority(string $eventName, $listener)
72
    {
73
        return $this->symfonyDispatcher->getListenerPriority($eventName, $listener);
74
    }
75
76
    public function hasListeners(string $eventName = null)
77
    {
78
        return ($this->symfonyDispatcher->hasListeners($eventName) ||
79
            $this->laravelDispatcher->hasListeners($eventName));
80
    }
81
}
82