Test Setup Failed
Push — master ( 1ac915...9b6ae8 )
by Mehmet
09:50
created

Application::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
    /**
24
     * @var Config
25
     */
26
    private $config;
27
    /**
28
     * @var Router
29
     */
30
    private $router;
31
32
    /**
33
     * @var array
34
     */
35
    private $response;
36
37
    /**
38
     * @var ServerRequestInterface
39
     */
40
    private $request;
41
42
    public function __construct(
43
        ContainerInterface $container,
44
        Router $router,
45
        Config $config
46
    ) {
47
        $this->config = $config;
48
        $this->router = $router;
49
        $this->container  = $container;
50
    }
51
52
    public static function createWithContainer(ContainerInterface $container) : self
53
    {
54
        return new self(
55
            $container,
56
            $container->get(Router::class),
57
            $container->get(Config::class)
58
        );
59
    }
60
61
    public function handle(ServerRequestInterface $request) : ResponseInterface
62
    {
63
        $this->request = $request;
64
        $this->run();
65
        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...
66
    }
67
68
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
69
    : ResponseInterface
70
    {
71
        $this->request = $request;
72
        $this->run();
73
        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...
74
    }
75
76
    private function run() : void
77
    {
78
        $route = $this->getRoute();
79
        $statusCode = $route->getStatusCode();
80
        switch ($statusCode) {
81
            case 405:
82
                $this->notFound(405, 'Method Not Allowed');
83
                break;
84
            case 200:
85
                $this->runRoute(
86
                    $route->getController(),
87
                    $route->getReturnType(),
88
                    $route->getUriParameters()
89
                );
90
                break;
91
            case 404:
92
            default:
93
                $this->notFound($statusCode, 'Not Found');
94
                break;
95
        }
96
    }
97
98 View Code Duplication
    private function notFound($status, $message) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $errorHandlerClass = $this->container->get('http-error-handler');
101
        $errorHandler = new $errorHandlerClass($status, $message);
102
        $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...
103
            $errorHandlerClass,
104
            $errorHandler(),
105
            $this->config,
106
            $this->container->get(ViewInterface::class)
107
        );
108
    }
109
110
    private function getRoute() : Route
111
    {
112
        $this->router = $this->router
113
            ->withDefaultReturnType($this->config->app->get('default_return_type', Router::HTML))
114
            ->withSubFolder($this->config->app->get('app_sub_folder', ''));
115
        $cacheFile = $this->config->app->get('route_cache_file', null);
116
        if ($cacheFile !== null) {
117
            $this->router = $this->router
118
                ->withCacheFile($cacheFile);
119
        }
120
        $this->getRouter($this->config->routes);
121
        return $this->router->getRoute();
122
    }
123
124
    private function getRouter($routes) : void
125
    {
126
        foreach ($routes as $route) {
127
            $this->router->add($route[0], $route[1], $route[2], $route[3], $route[4] ?? '');
128
        }
129
    }
130
131 View Code Duplication
    private function runRoute($controllerClass, int $returnType, array $args) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $controller = new ApplicationController($this->container, $controllerClass, $returnType, $args);
134
        $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...
135
            $controllerClass,
136
            $controller->getControllerResponse(),
137
            $this->config,
138
            $this->container->get(ViewInterface::class)
139
        );
140
    }
141
}
142