| Conditions | 28 |
| Paths | 106 |
| Total Lines | 162 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| 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 exact match in middleware routes |
||
| 260 | */ |
||
| 261 | foreach ($this->registry[$api] as $i => $path) { |
||
| 262 | if ($path instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
| 263 | $middlewareRoute = $path->getRoute(); |
||
| 264 | if ($middlewareRoute === $endpoint) { |
||
| 265 | $scope = $path->getScope(); |
||
| 266 | if ($scope == 'public') { |
||
| 267 | $scope = 'anonymous'; |
||
| 268 | } |
||
| 269 | $endpointSettings['model']['scope'] = $scope; |
||
| 270 | $endpointSettings['model']['middleware'] = $path; |
||
| 271 | return (object) $endpointSettings; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Check for dynamic uri eg: {asset_id} |
||
| 278 | * Dynamic uri's must be wrapped in {} for a true return |
||
| 279 | */ |
||
| 280 | foreach ($this->registry[$api] as $i => $path) { |
||
| 281 | $endpointRegister = $path; |
||
| 282 | $methodArgs = []; |
||
| 283 | |||
| 284 | $routeInstance = null; |
||
| 285 | if ($path instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
| 286 | $routeInstance = $path; |
||
| 287 | $endpointRegister = $path = $routeInstance->getRoute(); |
||
| 288 | $scope = $routeInstance->getScope(); |
||
|
|
|||
| 289 | |||
| 290 | if (isset($_SERVER['REQUEST_METHOD'])) { |
||
| 291 | $serverMethod = $_SERVER['REQUEST_METHOD']; |
||
| 292 | if ($routeInstance->getVerb() !== $serverMethod) { |
||
| 293 | continue; |
||
| 294 | } |
||
| 295 | } else { |
||
| 296 | // If no request method is set then continue to force a bad request |
||
| 297 | continue; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * If comparing the two sizes are not equal |
||
| 303 | * then no use continuing through the loop |
||
| 304 | */ |
||
| 305 | if (!$this->uriCheckSize($endpointRegister, $endpoint)) { |
||
| 306 | continue; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * This replacment will create a pattern to use as a match all expression |
||
| 311 | * @var [string] |
||
| 312 | */ |
||
| 313 | $endpointRegister = preg_replace('@/{(.*?)}@', '/(.*?)', $endpointRegister); |
||
| 314 | |||
| 315 | if (preg_match_all('@^' . $endpointRegister . '$@i', $endpoint, $dynamicParts)) { |
||
| 316 | $endpointFilter = $this->filterParts($endpoint, $dynamicParts); |
||
| 317 | $model = $this->getClassModel($path); |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Find the dynamic parts and set them as argument key(s) |
||
| 321 | * then combine them with the endpoint request and set the request parts |
||
| 322 | * as argument value(s) |
||
| 323 | */ |
||
| 324 | if (preg_match_all("/(?<={).*?(?=})/", $path, $registerParts)) { |
||
| 325 | if (isset($registerParts[0][0])) { |
||
| 326 | $registerParts = $registerParts[0]; |
||
| 327 | |||
| 328 | if (sizeof($endpointFilter) == sizeof($registerParts)) { |
||
| 329 | $methodArgs = array_combine($registerParts, $endpointFilter); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | $scope = 'private'; |
||
| 335 | if ($routeInstance instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
| 336 | $scope = $routeInstance->getScope(); |
||
| 337 | if ($scope == 'public') { |
||
| 338 | $scope = 'anonymous'; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | if (method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope')) { |
||
| 343 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api]())->scope(); |
||
| 344 | $position = array_search($path, $this->registry[$api]); |
||
| 345 | |||
| 346 | if (is_array($classScope) && isset($classScope[$position])) { |
||
| 347 | $scope = $classScope[$position]; |
||
| 348 | } else { |
||
| 349 | if (!is_array($classScope)) { |
||
| 350 | $scope = $classScope; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | $endpointSettings['model'] = array( |
||
| 356 | 'scope' => $scope, |
||
| 357 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
| 358 | 'class' => $model['class'], |
||
| 359 | 'method' => $model['method'], |
||
| 360 | 'arguments' => $methodArgs, |
||
| 361 | 'middleware' => $routeInstance, |
||
| 362 | ); |
||
| 363 | |||
| 364 | return (object) $endpointSettings; |
||
| 365 | } else { |
||
| 366 | continue; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | return; |
||
| 373 | } |
||
| 434 |