Passed
Push — master ( 4c6bc6...f797a9 )
by Roeland
17:19 queued 03:27
created

SymfonyAdapter::detectEventAndName()   B

Complexity

Conditions 11
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 9
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 20
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2019 Christoph Wurst <[email protected]>
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OC\EventDispatcher;
31
32
use Symfony\Component\EventDispatcher\GenericEvent;
33
use function is_callable;
34
use OCP\EventDispatcher\Event;
35
use OCP\ILogger;
36
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
37
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
38
use function is_object;
39
use function is_string;
40
41
/**
42
 * @deprecated 20.0.0 use \OCP\EventDispatcher\IEventDispatcher
43
 */
44
class SymfonyAdapter implements EventDispatcherInterface {
45
46
	/** @var EventDispatcher */
47
	private $eventDispatcher;
48
	/** @var ILogger */
49
	private $logger;
50
51
	/**
52
	 * @deprecated 20.0.0
53
	 */
54
	public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) {
55
		$this->eventDispatcher = $eventDispatcher;
56
		$this->logger = $logger;
57
	}
58
59
	private static function detectEventAndName($a, $b) {
60
		if (is_object($a) && (is_string($b) || $b === null)) {
61
			// a is the event, the other one is the optional name
62
			return [$a, $b];
63
		}
64
		if (is_object($b) && (is_string($a) || $a === null)) {
65
			// b is the event, the other one is the optional name
66
			return [$b, $a];
67
		}
68
		if (is_string($a) && $b === null) {
69
			// a is a payload-less event
70
			return [null, $a];
71
		}
72
		if (is_string($b) && $a === null) {
73
			// b is a payload-less event
74
			return [null, $b];
75
		}
76
77
		// Anything else we can't detect
78
		return [$a, $b];
79
	}
80
81
	/**
82
	 * Dispatches an event to all registered listeners.
83
	 *
84
	 * @param string $eventName The name of the event to dispatch. The name of
85
	 *                              the event is the name of the method that is
86
	 *                              invoked on listeners.
87
	 * @param Event|null $event The event to pass to the event handlers/listeners
88
	 *                              If not supplied, an empty Event instance is created
89
	 *
90
	 * @return object the emitted event
91
	 * @deprecated 20.0.0
92
	 */
93
	public function dispatch($eventName, $event = null): object {
94
		[$event, $eventName] = self::detectEventAndName($event, $eventName);
95
96
		// type hinting is not possible, due to usage of GenericEvent
97
		if ($event instanceof Event && $eventName === null) {
98
			$this->eventDispatcher->dispatchTyped($event);
99
			return $event;
100
		}
101
		if ($event instanceof Event) {
102
			$this->eventDispatcher->dispatch($eventName, $event);
0 ignored issues
show
Deprecated Code introduced by
The function OC\EventDispatcher\EventDispatcher::dispatch() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

102
			/** @scrutinizer ignore-deprecated */ $this->eventDispatcher->dispatch($eventName, $event);
Loading history...
103
			return $event;
104
		}
105
106
		if ($event instanceof GenericEvent && get_class($event) === GenericEvent::class) {
107
			$newEvent = new GenericEventWrapper($this->logger, $eventName, $event);
108
		} else {
109
			$newEvent = $event;
110
111
			// Legacy event
112
			$this->logger->info(
113
				'Deprecated event type for {name}: {class}',
114
				['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null']
115
			);
116
		}
117
118
		// Event with no payload (object) need special handling
119
		if ($newEvent === null) {
120
			$this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName);
121
			return new Event();
122
		}
123
124
		// Flip the argument order for Symfony to prevent a trigger_error
125
		return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($newEvent, $eventName);
126
	}
127
128
	/**
129
	 * Adds an event listener that listens on the specified events.
130
	 *
131
	 * @param string $eventName The event to listen on
132
	 * @param callable $listener The listener
133
	 * @param int $priority The higher this value, the earlier an event
134
	 *                            listener will be triggered in the chain (defaults to 0)
135
	 * @deprecated 20.0.0
136
	 */
137
	public function addListener($eventName, $listener, $priority = 0) {
138
		if (is_callable($listener)) {
139
			$this->eventDispatcher->addListener($eventName, $listener, $priority);
140
		} else {
141
			// Legacy listener
142
			$this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority);
143
		}
144
	}
145
146
	/**
147
	 * Adds an event subscriber.
148
	 *
149
	 * The subscriber is asked for all the events it is
150
	 * interested in and added as a listener for these events.
151
	 * @deprecated 20.0.0
152
	 */
153
	public function addSubscriber(EventSubscriberInterface $subscriber) {
154
		$this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber);
155
	}
156
157
	/**
158
	 * Removes an event listener from the specified events.
159
	 *
160
	 * @param string $eventName The event to remove a listener from
161
	 * @param callable $listener The listener to remove
162
	 * @deprecated 20.0.0
163
	 */
164
	public function removeListener($eventName, $listener) {
165
		$this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
166
	}
167
168
	/**
169
	 * @deprecated 20.0.0
170
	 */
171
	public function removeSubscriber(EventSubscriberInterface $subscriber) {
172
		$this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber);
173
	}
174
175
	/**
176
	 * Gets the listeners of a specific event or all listeners sorted by descending priority.
177
	 *
178
	 * @param string|null $eventName The name of the event
179
	 *
180
	 * @return array The event listeners for the specified event, or all event listeners by event name
181
	 * @deprecated 20.0.0
182
	 */
183
	public function getListeners($eventName = null) {
184
		return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName);
185
	}
186
187
	/**
188
	 * Gets the listener priority for a specific event.
189
	 *
190
	 * Returns null if the event or the listener does not exist.
191
	 *
192
	 * @param string $eventName The name of the event
193
	 * @param callable $listener The listener
194
	 *
195
	 * @return int|null The event listener priority
196
	 * @deprecated 20.0.0
197
	 */
198
	public function getListenerPriority($eventName, $listener) {
199
		return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener);
200
	}
201
202
	/**
203
	 * Checks whether an event has any registered listeners.
204
	 *
205
	 * @param string|null $eventName The name of the event
206
	 *
207
	 * @return bool true if the specified event has any listeners, false otherwise
208
	 * @deprecated 20.0.0
209
	 */
210
	public function hasListeners($eventName = null) {
211
		return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
212
	}
213
}
214