Passed
Push — master ( ac48a9...31bd7d )
by Roeland
11:49
created

EventDispatcher::addServiceListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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