Conditions | 22 |
Paths | 104 |
Total Lines | 134 |
Code Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
211 | public function isEndpoint($api, $endpoint) |
||
212 | { |
||
213 | /** |
||
214 | * Return if it's a system endpoint |
||
215 | */ |
||
216 | if (null !== ($endpointSettings = $this->isSystemEndpoint($api, $endpoint))) { |
||
217 | return $endpointSettings; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Check if the endpoint is a RouterInterface |
||
222 | */ |
||
223 | $endpoint = htmlspecialchars($endpoint, ENT_QUOTES, 'UTF-8'); |
||
224 | $index = array_search($api, $this->BASE_ENDPOINTS); |
||
225 | |||
226 | if ($index !== false) { |
||
227 | if (isset($this->registry[$api])) { |
||
228 | $endpointSettings = array( |
||
229 | 'path' => $endpoint, |
||
230 | 'model' => array( |
||
231 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
232 | 'class' => $this->BASE_ENDPOINTS[$index], |
||
233 | 'method' => basename($endpoint), |
||
234 | 'scope' => 'private', |
||
235 | ), |
||
236 | ); |
||
237 | |||
238 | /** |
||
239 | * Nothing dynamic, found an exact match |
||
240 | * @var array |
||
241 | */ |
||
242 | if (array_search($endpoint, $this->registry[$api]) !== false) { |
||
243 | if (method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope')) { |
||
244 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api]())->scope(); |
||
245 | $position = array_search($endpoint, $this->registry[$api]); |
||
246 | |||
247 | if (is_array($classScope) && isset($classScope[$position])) { |
||
248 | $endpointSettings['model']['scope'] = $classScope[$position]; |
||
249 | } else { |
||
250 | if (!is_array($classScope)) { |
||
251 | $endpointSettings['model']['scope'] = $classScope; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | return (object) $endpointSettings; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Check for dynamic uri eg: {asset_id} |
||
260 | * Dynamic uri's must be wrapped in {} for a true return |
||
261 | */ |
||
262 | foreach ($this->registry[$api] as $i => $path) { |
||
263 | $endpointRegister = $path; |
||
264 | $methodArgs = []; |
||
265 | |||
266 | $routeInstance = null; |
||
267 | if ($path instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
268 | $routeInstance = $path; |
||
269 | $endpointRegister = $path = $routeInstance->getRoute(); |
||
270 | $scope = $routeInstance->getScope(); |
||
|
|||
271 | } |
||
272 | |||
273 | /** |
||
274 | * If comparing the two sizes are not equal |
||
275 | * then no use continuing through the loop |
||
276 | */ |
||
277 | if (!$this->uriCheckSize($endpointRegister, $endpoint)) { |
||
278 | continue; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * This replacment will create a pattern to use as a match all expression |
||
283 | * @var [string] |
||
284 | */ |
||
285 | $endpointRegister = preg_replace('@/{(.*?)}@', '/(.*?)', $endpointRegister); |
||
286 | |||
287 | if (preg_match_all('@^' . $endpointRegister . '$@i', $endpoint, $dynamicParts)) { |
||
288 | $endpointFilter = $this->filterParts($endpoint, $dynamicParts); |
||
289 | $model = $this->getClassModel($path); |
||
290 | |||
291 | /** |
||
292 | * Find the dynamic parts and set them as argument key(s) |
||
293 | * then combine them with the endpoint request and set the request parts |
||
294 | * as argument value(s) |
||
295 | */ |
||
296 | if (preg_match_all("/(?<={).*?(?=})/", $path, $registerParts)) { |
||
297 | if (isset($registerParts[0][0])) { |
||
298 | $registerParts = $registerParts[0]; |
||
299 | |||
300 | if (sizeof($endpointFilter) == sizeof($registerParts)) { |
||
301 | $methodArgs = array_combine($registerParts, $endpointFilter); |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | |||
306 | $scope = 'private'; |
||
307 | if ($routeInstance instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
308 | $scope = $routeInstance->getScope(); |
||
309 | if ($scope == 'public') { |
||
310 | $scope = 'anonymous'; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | if (method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope')) { |
||
315 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api]())->scope(); |
||
316 | $position = array_search($path, $this->registry[$api]); |
||
317 | |||
318 | if (is_array($classScope) && isset($classScope[$position])) { |
||
319 | $scope = $classScope[$position]; |
||
320 | } else { |
||
321 | if (!is_array($classScope)) { |
||
322 | $scope = $classScope; |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | |||
327 | $endpointSettings['model'] = array( |
||
328 | 'scope' => $scope, |
||
329 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
330 | 'class' => $model['class'], |
||
331 | 'method' => $model['method'], |
||
332 | 'arguments' => $methodArgs, |
||
333 | 'middleware' => $routeInstance, |
||
334 | ); |
||
335 | |||
336 | return (object) $endpointSettings; |
||
337 | } else { |
||
338 | continue; |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | |||
344 | return; |
||
345 | } |
||
406 |