Test Setup Failed
Push — master ( 002a67...75549f )
by Mehmet
04:28
created

Application::notFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami;
5
6
use Psr\Container\ContainerInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Selami\Router\Route;
11
use Selami\Router\Router;
12
use Selami\View\ViewInterface;
13
use Zend\Diactoros\Response;
14
use Zend\Config\Config;
15
16
class Application implements RequestHandlerInterface
17
{
18
19
    private $id;
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
    /**
25
     * @var Config
26
     */
27
    private $config;
28
    /**
29
     * @var Router
30
     */
31
    private $router;
32
33
    /**
34
     * @var array
35
     */
36
    private $response;
37
38
    /**
39
     * @var ServerRequestInterface
40
     */
41
    private $request;
42
43
    public function __construct(
44
        string $id,
45
        ContainerInterface $container,
46
        Router $router,
47
        Config $config
48
    ) {
49
        $this->id = $id;
50
        $this->config = $config;
51
        $this->router = $router;
52
        $this->container  = $container;
53
    }
54
55
    public static function createWithContainer(ContainerInterface $container, ?string $id = 'selami-app') : self
56
    {
57
        return new self(
58
            $id,
59
            $container,
60
            $container->get(Router::class),
61
            $container->get(Config::class)
62
        );
63
    }
64
65
    public function handle(ServerRequestInterface $request) : ResponseInterface
66
    {
67
        $this->request = $request;
68
        $this->run();
69
        return $this->response->returnResponse(new Response());
0 ignored issues
show
Bug introduced by
The method returnResponse cannot be called on $this->response (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
    }
71
72
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
73
    : ResponseInterface
74
    {
75
        $this->request = $request;
76
        $this->run();
77
        return $this->response->returnResponse($response);
0 ignored issues
show
Bug introduced by
The method returnResponse cannot be called on $this->response (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
78
    }
79
80
    private function run() : void
81
    {
82
        $route = $this->getRoute();
83
        $statusCode = $route->getStatusCode();
84
        switch ($statusCode) {
85
            case 405:
86
                $this->notFound(405, 'Method Not Allowed');
87
                break;
88
            case 200:
89
                $this->runRoute(
90
                    $route->getController(),
91
                    $route->getReturnType(),
92
                    $route->getUriParameters()
93
                );
94
                break;
95
            case 404:
96
            default:
97
                $this->notFound($statusCode, 'Not Found');
98
                break;
99
        }
100
    }
101
102
    private function notFound($status, $message) : void
103
    {
104
        $errorHandlerClass = $this->container->get('http-error-handler');
105
        $errorHandler = new $errorHandlerClass($status, $message);
106
        $this->response = new ApplicationResponse(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Selami\ApplicationR...\ViewInterface::class)) of type object<Selami\ApplicationResponse> is incompatible with the declared type array of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
107
            $errorHandlerClass,
108
            $errorHandler(),
109
            $this->config,
110
            $this->container->get(ViewInterface::class)
111
        );
112
    }
113
114
    private function getRoute() : Route
115
    {
116
        $this->router = $this->router
117
            ->withDefaultReturnType($this->config->app->get('default_return_type', Router::HTML))
118
            ->withSubFolder($this->config->app->get('app_sub_folder', ''));
119
        $cacheFile = $this->config->app->get('router_cache_file-' . $this->id, null);
120
        if ((bool) $cacheFile) {
121
            $this->router = $this->router
122
                ->withCacheFile($cacheFile);
123
        }
124
        $this->addRoutes($this->config->routes);
125
        return $this->router->getRoute();
126
    }
127
128
    private function addRoutes($routes) : void
129
    {
130
        foreach ($routes as $route) {
131
            $this->router->add($route[0], $route[1], $route[2], $route[3], $route[4] ?? '');
132
        }
133
    }
134
135
    private function runRoute($controllerClass, int $returnType, array $args) : void
136
    {
137
        $controller = new ApplicationController($this->container, $controllerClass, $returnType, $args);
138
        $this->response = new ApplicationResponse(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Selami\ApplicationR...\ViewInterface::class)) of type object<Selami\ApplicationResponse> is incompatible with the declared type array of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
139
            $controllerClass,
140
            $controller->getControllerResponse(),
141
            $this->config,
142
            $this->container->get(ViewInterface::class)
143
        );
144
    }
145
}
146