BaseHandler::resolveRequest()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 23
rs 9.8666
1
<?php
2
3
namespace Lepton\Core\Handler;
4
5
use Lepton\Http\Response\{HttpResponse, SuccessResponse, NotFoundResponse, InternalErrorResponse};
6
use Lepton\Routing\Match\{BaseMatch, MatchRoute, Match404};
7
use Lepton\Routing\UrlResolver;
8
use Lepton\Core\Application;
9
use Lepton\Exceptions;
10
11
class BaseHandler extends AbstractHandler
12
{
13
    /**
14
     * Get the matcher that matches the current request
15
     *
16
     * @return BaseMatch
17
     *
18
     */
19
20
    protected function resolveRequest(): BaseMatch
21
    {
22
        $resolver = new UrlResolver($this->request->url);
23
24
        foreach (Application::$routes as $pattern => $callback) {
25
26
            // Add base_url to $pattern, if needed
27
28
            $link =  "%s/%s";
29
            $pattern = sprintf($link, Application::getAppConfig()->base_url, $pattern);
30
            // If it matches, return the match
31
            $parameters = $resolver->match($pattern);
32
            if (is_array($parameters)) {
33
                // Check if callback is a controller method
34
                $controller = $callback[0];
35
                $method = $callback[1];
36
                if (! method_exists($controller, $method)) {
37
                    throw new Exceptions\ControllerNotFoundException("Invalid Controller and/or method in routes.php");
38
                }
39
                return new MatchRoute(controller: $controller, method: $method, parameters: $parameters);
40
            }
41
        }
42
        return new Match404();
43
    }
44
45
46
    /**
47
     * Handles the request forwarding it to the $handler
48
     * @param Match404|MatchRoute $matcher
49
     * @return NotFoundResponde|SuccessResponse
0 ignored issues
show
Bug introduced by
The type Lepton\Core\Handler\NotFoundResponde was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     */
51
    protected function handle(BaseMatch $matcher): HttpResponse
52
    {
53
        if($matcher instanceof Match404) {
54
            return new NotFoundResponse();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Lepton\Http\Response\NotFoundResponse() returns the type Lepton\Http\Response\NotFoundResponse which is incompatible with the documented return type Lepton\Core\Handler\NotF...esponse\SuccessResponse.
Loading history...
55
        }
56
57
        if($matcher instanceof MatchRoute) {
58
59
            foreach($this->middlewares as $middleware => $args){
60
              $middlewareInstance = new $middleware();
61
              $middlewareInstance->addMatcher($matcher);
62
              $middlewareInstance->setRequest($this->request);
63
              $middlewareResult = $middlewareInstance(...$args);
64
              if($middlewareResult instanceof HttpResponse){
65
                return $middlewareResult;
66
              }
67
            }
68
69
            Application::$controller = $matcher->controller;
70
            $controller = new $matcher->controller();
71
            $method =  $matcher->method;
72
            return $controller->$method(...$matcher->parameters);
73
        }
74
        throw new \Exception("Wrong matcher!");
75
        return new InternalErrorResponse();
0 ignored issues
show
Unused Code introduced by
return new Lepton\Http\R...InternalErrorResponse() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
76
    }
77
78
}
79