Total Complexity | 46 |
Total Lines | 326 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BackendService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BackendService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class BackendService { |
||
39 | |||
40 | /** Visibility constants for VisibilityTrait */ |
||
41 | const VISIBILITY_NONE = 0; |
||
42 | const VISIBILITY_PERSONAL = 1; |
||
43 | const VISIBILITY_ADMIN = 2; |
||
44 | //const VISIBILITY_ALIENS = 4; |
||
45 | |||
46 | const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN |
||
47 | |||
48 | /** Priority constants for PriorityTrait */ |
||
49 | const PRIORITY_DEFAULT = 100; |
||
50 | |||
51 | /** @var IConfig */ |
||
52 | protected $config; |
||
53 | |||
54 | /** @var bool */ |
||
55 | private $userMountingAllowed = true; |
||
56 | |||
57 | /** @var string[] */ |
||
58 | private $userMountingBackends = []; |
||
59 | |||
60 | /** @var Backend[] */ |
||
61 | private $backends = []; |
||
62 | |||
63 | /** @var IBackendProvider[] */ |
||
64 | private $backendProviders = []; |
||
65 | |||
66 | /** @var AuthMechanism[] */ |
||
67 | private $authMechanisms = []; |
||
68 | |||
69 | /** @var IAuthMechanismProvider[] */ |
||
70 | private $authMechanismProviders = []; |
||
71 | |||
72 | /** @var callable[] */ |
||
73 | private $configHandlerLoaders = []; |
||
74 | |||
75 | private $configHandlers = []; |
||
76 | |||
77 | /** |
||
78 | * @param IConfig $config |
||
79 | */ |
||
80 | public function __construct( |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Register a backend provider |
||
101 | * |
||
102 | * @since 9.1.0 |
||
103 | * @param IBackendProvider $provider |
||
104 | */ |
||
105 | public function registerBackendProvider(IBackendProvider $provider) { |
||
107 | } |
||
108 | |||
109 | private function callForRegistrations() { |
||
110 | static $eventSent = false; |
||
111 | if(!$eventSent) { |
||
112 | \OC::$server->getEventDispatcher()->dispatch( |
||
113 | 'OCA\\Files_External::loadAdditionalBackends' |
||
114 | ); |
||
115 | $eventSent = true; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | private function loadBackendProviders() { |
||
120 | $this->callForRegistrations(); |
||
121 | foreach ($this->backendProviders as $provider) { |
||
122 | $this->registerBackends($provider->getBackends()); |
||
|
|||
123 | } |
||
124 | $this->backendProviders = []; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Register an auth mechanism provider |
||
129 | * |
||
130 | * @since 9.1.0 |
||
131 | * @param IAuthMechanismProvider $provider |
||
132 | */ |
||
133 | public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { |
||
134 | $this->authMechanismProviders[] = $provider; |
||
135 | } |
||
136 | |||
137 | private function loadAuthMechanismProviders() { |
||
138 | $this->callForRegistrations(); |
||
139 | foreach ($this->authMechanismProviders as $provider) { |
||
140 | $this->registerAuthMechanisms($provider->getAuthMechanisms()); |
||
141 | } |
||
142 | $this->authMechanismProviders = []; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Register a backend |
||
147 | * |
||
148 | * @deprecated 9.1.0 use registerBackendProvider() |
||
149 | * @param Backend $backend |
||
150 | */ |
||
151 | public function registerBackend(Backend $backend) { |
||
152 | if (!$this->isAllowedUserBackend($backend)) { |
||
153 | $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
||
154 | } |
||
155 | foreach ($backend->getIdentifierAliases() as $alias) { |
||
156 | $this->backends[$alias] = $backend; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @deprecated 9.1.0 use registerBackendProvider() |
||
162 | * @param Backend[] $backends |
||
163 | */ |
||
164 | public function registerBackends(array $backends) { |
||
165 | foreach ($backends as $backend) { |
||
166 | $this->registerBackend($backend); |
||
167 | } |
||
168 | } |
||
169 | /** |
||
170 | * Register an authentication mechanism |
||
171 | * |
||
172 | * @deprecated 9.1.0 use registerAuthMechanismProvider() |
||
173 | * @param AuthMechanism $authMech |
||
174 | */ |
||
175 | public function registerAuthMechanism(AuthMechanism $authMech) { |
||
176 | if (!$this->isAllowedAuthMechanism($authMech)) { |
||
177 | $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
||
178 | } |
||
179 | foreach ($authMech->getIdentifierAliases() as $alias) { |
||
180 | $this->authMechanisms[$alias] = $authMech; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @deprecated 9.1.0 use registerAuthMechanismProvider() |
||
186 | * @param AuthMechanism[] $mechanisms |
||
187 | */ |
||
188 | public function registerAuthMechanisms(array $mechanisms) { |
||
189 | foreach ($mechanisms as $mechanism) { |
||
190 | $this->registerAuthMechanism($mechanism); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Get all backends |
||
196 | * |
||
197 | * @return Backend[] |
||
198 | */ |
||
199 | public function getBackends() { |
||
200 | $this->loadBackendProviders(); |
||
201 | // only return real identifiers, no aliases |
||
202 | $backends = []; |
||
203 | foreach ($this->backends as $backend) { |
||
204 | $backends[$backend->getIdentifier()] = $backend; |
||
205 | } |
||
206 | return $backends; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get all available backends |
||
211 | * |
||
212 | * @return Backend[] |
||
213 | */ |
||
214 | public function getAvailableBackends() { |
||
215 | return array_filter($this->getBackends(), function($backend) { |
||
216 | return !$backend->checkDependencies(); |
||
217 | }); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string $identifier |
||
222 | * @return Backend|null |
||
223 | */ |
||
224 | public function getBackend($identifier) { |
||
225 | $this->loadBackendProviders(); |
||
226 | if (isset($this->backends[$identifier])) { |
||
227 | return $this->backends[$identifier]; |
||
228 | } |
||
229 | return null; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get all authentication mechanisms |
||
234 | * |
||
235 | * @return AuthMechanism[] |
||
236 | */ |
||
237 | public function getAuthMechanisms() { |
||
238 | $this->loadAuthMechanismProviders(); |
||
239 | // only return real identifiers, no aliases |
||
240 | $mechanisms = []; |
||
241 | foreach ($this->authMechanisms as $mechanism) { |
||
242 | $mechanisms[$mechanism->getIdentifier()] = $mechanism; |
||
243 | } |
||
244 | return $mechanisms; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Get all authentication mechanisms for schemes |
||
249 | * |
||
250 | * @param string[] $schemes |
||
251 | * @return AuthMechanism[] |
||
252 | */ |
||
253 | public function getAuthMechanismsByScheme(array $schemes) { |
||
254 | return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) { |
||
255 | return in_array($authMech->getScheme(), $schemes, true); |
||
256 | }); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param string $identifier |
||
261 | * @return AuthMechanism|null |
||
262 | */ |
||
263 | public function getAuthMechanism($identifier) { |
||
264 | $this->loadAuthMechanismProviders(); |
||
265 | if (isset($this->authMechanisms[$identifier])) { |
||
266 | return $this->authMechanisms[$identifier]; |
||
267 | } |
||
268 | return null; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function isUserMountingAllowed() { |
||
275 | return $this->userMountingAllowed; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Check a backend if a user is allowed to mount it |
||
280 | * |
||
281 | * @param Backend $backend |
||
282 | * @return bool |
||
283 | */ |
||
284 | protected function isAllowedUserBackend(Backend $backend) { |
||
285 | if ($this->userMountingAllowed && |
||
286 | array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) |
||
287 | ) { |
||
288 | return true; |
||
289 | } |
||
290 | return false; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Check an authentication mechanism if a user is allowed to use it |
||
295 | * |
||
296 | * @param AuthMechanism $authMechanism |
||
297 | * @return bool |
||
298 | */ |
||
299 | protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { |
||
300 | return true; // not implemented |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * registers a configuration handler |
||
305 | * |
||
306 | * The function of the provided $placeholder is mostly to act a sorting |
||
307 | * criteria, so longer placeholders are replaced first. This avoids |
||
308 | * "$user" overwriting parts of "$userMail" and "$userLang", for example. |
||
309 | * The provided value should not contain the $ prefix, only a-z0-9 are |
||
310 | * allowed. Upper case letters are lower cased, the replacement is case- |
||
311 | * insensitive. |
||
312 | * |
||
313 | * The configHandlerLoader should just instantiate the handler on demand. |
||
314 | * For now all handlers are instantiated when a mount is loaded, independent |
||
315 | * of whether the placeholder is present or not. This may change in future. |
||
316 | * |
||
317 | * @since 16.0.0 |
||
318 | */ |
||
319 | public function registerConfigHandler(string $placeholder, callable $configHandlerLoader) { |
||
320 | $placeholder = trim(strtolower($placeholder)); |
||
321 | if(!(bool)\preg_match('/^[a-z0-9]*$/', $placeholder)) { |
||
322 | throw new \RuntimeException(sprintf( |
||
323 | 'Invalid placeholder %s, only [a-z0-9] are allowed', $placeholder |
||
324 | )); |
||
325 | } |
||
326 | if($placeholder === '') { |
||
327 | throw new \RuntimeException('Invalid empty placeholder'); |
||
328 | } |
||
329 | if(isset($this->configHandlerLoaders[$placeholder]) || isset($this->configHandlers[$placeholder])) { |
||
330 | throw new \RuntimeException(sprintf('A handler is already registered for %s', $placeholder)); |
||
331 | } |
||
332 | $this->configHandlerLoaders[$placeholder] = $configHandlerLoader; |
||
333 | } |
||
334 | |||
335 | protected function loadConfigHandlers():void { |
||
354 | }); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @since 16.0.0 |
||
360 | */ |
||
361 | public function getConfigHandlers() { |
||
362 | $this->loadConfigHandlers(); |
||
363 | return $this->configHandlers; |
||
364 | } |
||
365 | } |
||
366 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.