Passed
Push — master ( c44634...5079d8 )
by Henri
08:48 queued 10s
created

App::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
session_start();
4
5
require __DIR__.'/../vendor/autoload.php';
6
require __DIR__.'/Routes/default.php';
7
8
use HnrAzevedo\Http\Factory;
0 ignored issues
show
Bug introduced by
The type HnrAzevedo\Http\Factory 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...
9
use HnrAzevedo\Http\Uri;
0 ignored issues
show
Bug introduced by
The type HnrAzevedo\Http\Uri 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...
10
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface 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...
11
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface 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...
12
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface 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...
13
14
use HnrAzevedo\Router\Router;
0 ignored issues
show
Bug introduced by
The type HnrAzevedo\Router\Router 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...
15
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface 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...
16
17
try{
18
    $serverRequest = (new Factory())->createServerRequest($_SERVER['REQUEST_METHOD'], new Uri($_SERVER['REQUEST_URI']));
19
20
    class App implements MiddlewareInterface{
21
        public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
22
        {
23
            if(empty($request->getAttribute('route')))
24
            {
25
                throw new Exception('Page not found', 404);
26
            }
27
28
            $request->getAttribute('route')['action']();
29
30
            return (new Factory())->createResponse(200);
31
        }
32
    }
33
34
    define('GLOBAL_MIDDLEWARES',[
35
        Router::class,
36
        App::class
37
    ]);
38
39
    function nextExample(RequestHandlerInterface $defaultHandler): RequestHandlerInterface
40
    {
41
        return new class (GLOBAL_MIDDLEWARES, $defaultHandler) implements RequestHandlerInterface {
42
            private RequestHandlerInterface $handler;
43
            private array $pipeline;
44
45
            public function __construct(array $pipeline, RequestHandlerInterface $handler)
46
            {
47
                $this->handler = $handler;
48
                $this->pipeline = $pipeline;
49
            }
50
51
            public function handle(ServerRequestInterface $request): ResponseInterface
52
            {
53
                if (!$middleware = array_shift($this->pipeline)) {
54
                    return $this->handler->handle($request);
55
                }
56
57
                $next = clone $this;
58
                $this->pipeline = [];
59
60
                $response = (new $middleware())->process($request, $next);
61
62
                return $response;
63
            }
64
        };
65
    }
66
67
68
    function runMiddlewares($serverRequest)
69
    {
70
        nextExample(new class implements RequestHandlerInterface{
71
            public function handle(ServerRequestInterface $request): ResponseInterface
72
            {
73
                return (new Factory())->createResponse(200);
74
            }
75
        })->handle($serverRequest);
76
    }
77
78
    runMiddlewares($serverRequest);
79
80
}catch(Exception $er){
81
82
    die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}.");
83
84
}
85