App   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 1
eloc 3
c 1
b 0
f 1
dl 0
loc 5
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 4 1
1
<?php
2
3
use HnrAzevedo\Http\Factory;
4
use HnrAzevedo\Http\Uri;
5
use Psr\Http\Server\RequestHandlerInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
10
use HnrAzevedo\Viewer\Viewer;
11
12
try{
13
    $serverRequest = (new Factory())->createServerRequest(
14
        $_SERVER['REQUEST_METHOD'], 
15
        new Uri($_SERVER['REQUEST_URI'])
16
    );
17
18
    $serverRequest = $serverRequest->withAttribute('viewer',[
19
        'path' => 'Views',
20
        'file' => 'default',
21
        'data' => $data
22
    ]);
23
    
24
    class App implements MiddlewareInterface{
25
        public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
26
        {
27
            echo $request->getBody()->getContents();
28
            return (new Factory())->createResponse(200);
29
        }
30
    }
31
32
    define('GLOBAL_MIDDLEWARES',[
33
        Viewer::class,
34
        App::class
35
    ]);
36
37
    function nextExample(RequestHandlerInterface $defaultHandler): RequestHandlerInterface
38
    {
39
        return new class (GLOBAL_MIDDLEWARES, $defaultHandler) implements RequestHandlerInterface {
40
            private RequestHandlerInterface $handler;
41
            private array $pipeline;
42
43
            public function __construct(array $pipeline, RequestHandlerInterface $handler)
44
            {
45
                $this->handler = $handler;
46
                $this->pipeline = $pipeline;
47
            }
48
49
            public function handle(ServerRequestInterface $request): ResponseInterface
50
            {
51
                if (!$middleware = array_shift($this->pipeline)) {
52
                    return $this->handler->handle($request);
53
                }
54
55
                $next = clone $this;
56
                $this->pipeline = [];
57
58
                $response = (new $middleware())->process($request, $next);
59
60
                return $response;
61
            }
62
        };
63
    }
64
65
66
    function runMiddlewares($serverRequest)
67
    {
68
        nextExample(new class implements RequestHandlerInterface{
69
            public function handle(ServerRequestInterface $request): ResponseInterface
70
            {
71
                return (new Factory())->createResponse(200);
72
            }
73
        })->handle($serverRequest);
74
    }
75
76
    runMiddlewares($serverRequest);
77
78
}catch(Exception $er){
79
80
    die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}.");
81
82
}
83