Passed
Push — master ( c158ee...e231dd )
by Roeland
12:36 queued 11s
created

EventDispatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addListener() 0 4 1
A getSymfonyDispatcher() 0 2 1
A removeListener() 0 3 1
A dispatchTyped() 0 2 1
A addServiceListener() 0 10 1
A dispatch() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2019 Christoph Wurst <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OC\EventDispatcher;
29
30
use function get_class;
31
use OC\Broadcast\Events\BroadcastEvent;
32
use OCP\Broadcast\Events\IBroadcastEvent;
33
use OCP\EventDispatcher\ABroadcastedEvent;
34
use OCP\EventDispatcher\Event;
35
use OCP\EventDispatcher\IEventDispatcher;
36
use OCP\IContainer;
37
use OCP\ILogger;
38
use OCP\IServerContainer;
39
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
40
41
class EventDispatcher implements IEventDispatcher {
42
43
	/** @var SymfonyDispatcher */
44
	private $dispatcher;
45
46
	/** @var IContainer */
47
	private $container;
48
49
	/** @var ILogger */
50
	private $logger;
51
52
	public function __construct(SymfonyDispatcher $dispatcher,
53
								IServerContainer $container,
54
								ILogger $logger) {
55
		$this->dispatcher = $dispatcher;
56
		$this->container = $container;
57
		$this->logger = $logger;
58
	}
59
60
	public function addListener(string $eventName,
61
								callable $listener,
62
								int $priority = 0): void {
63
		$this->dispatcher->addListener($eventName, $listener, $priority);
64
	}
65
66
	public function removeListener(string $eventName,
67
								   callable $listener): void {
68
		$this->dispatcher->removeListener($eventName, $listener);
69
	}
70
71
	public function addServiceListener(string $eventName,
72
									   string $className,
73
									   int $priority = 0): void {
74
		$listener = new ServiceEventListener(
75
			$this->container,
76
			$className,
77
			$this->logger
78
		);
79
80
		$this->addListener($eventName, $listener, $priority);
81
	}
82
83
	public function dispatch(string $eventName,
84
							 Event $event): void {
85
		$this->dispatcher->dispatch($event, $eventName);
86
87
		if ($event instanceof ABroadcastedEvent && !$event->isPropagationStopped()) {
88
			// Propagate broadcast
89
			$this->dispatch(
90
				IBroadcastEvent::class,
91
				new BroadcastEvent($event)
92
			);
93
		}
94
	}
95
96
	public function dispatchTyped(Event $event): void {
97
		$this->dispatch(get_class($event), $event);
98
	}
99
100
	/**
101
	 * @return SymfonyDispatcher
102
	 */
103
	public function getSymfonyDispatcher(): SymfonyDispatcher {
104
		return $this->dispatcher;
105
	}
106
107
}
108