Passed
Push — master ( 022aa2...8d4f2f )
by Henri
04:23 queued 47s
created

nextExample()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
nc 1
nop 1
dl 0
loc 24
rs 9.9
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MiddlewareExample.php$0 ➔ handle() 0 12 2
A MiddlewareExample.php$0 ➔ __construct() 0 4 1
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;
9
use HnrAzevedo\Http\Uri;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
use HnrAzevedo\Router\Router;
15
use Psr\Http\Server\MiddlewareInterface;
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