Failed Conditions
Push — master ( a26426...65249a )
by Arnold
14:53 queued 04:44
created

Slim::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jasny\Controller\Middleware;
4
5
use Jasny\Controller\Controller;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Slim\Exception\HttpBadRequestException;
11
use Slim\Exception\HttpException;
12
use Slim\Exception\HttpForbiddenException;
13
use Slim\Exception\HttpGoneException;
14
use Slim\Exception\HttpInternalServerErrorException;
15
use Slim\Exception\HttpMethodNotAllowedException;
16
use Slim\Exception\HttpNotFoundException;
17
use Slim\Exception\HttpNotImplementedException;
18
use Slim\Exception\HttpUnauthorizedException;
19
use Slim\Routing\Route;
20
21
/**
22
 * Middleware to use controller in Slim framework
23
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class Slim implements MiddlewareInterface
25
{
26
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
     * @param bool $useSlimErrors  Throw Slim exceptions for error responses.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
28
     */
29
    public function __construct(public bool $useSlimErrors = false)
30
    { }
0 ignored issues
show
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
31
32
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function process()
Loading history...
33
    {
34
        $route = $request->getAttribute('__route__');
35
36
        if ($route instanceof Route) {
37
            $request = $this->changeCallable($request, $route);
38
            $request = $this->setPathParameters($request, $route);
39
        }
40
41
        $response = $handler->handle($request);
42
43
        if ($this->useSlimErrors) {
44
            $this->throwOnError($request, $response);
45
        }
46
47
        return $response;
48
    }
49
50
    protected function changeCallable(ServerRequestInterface $request, Route $route): ServerRequestInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function changeCallable()
Loading history...
51
    {
52
        $callable = $route->getCallable();
53
54
        if (
0 ignored issues
show
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
55
            is_array($callable) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
56
            is_a($callable[0], Controller::class, true) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
57
            isset($callable[1]) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
58
            $callable[1] !== '__invoke'
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
59
        ) {
60
            $request = $request
61
                ->withAttribute("route:action", $callable[1])
62
                ->withAttribute('__route__', $route->setCallable([$callable[0], '__invoke']));
63
        }
64
65
        return $request;
66
    }
67
68
    protected function setPathParameters(ServerRequestInterface $request, Route $route): ServerRequestInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setPathParameters()
Loading history...
69
    {
70
        foreach ($route->getArguments() as $key => $value) {
71
            $request = $request->withAttribute('route:{' . $key . '}', $value);
72
        }
73
74
        return $request;
75
    }
76
77
    protected function throwOnError(ServerRequestInterface $request, ResponseInterface $response): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function throwOnError()
Loading history...
78
    {
79
        $status = $response->getStatusCode();
80
81
        switch ($status) {
82
            case 400: throw new HttpBadRequestException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
83
            case 401: throw new HttpUnauthorizedException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
84
            case 403: throw new HttpForbiddenException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
85
            case 404: throw new HttpNotFoundException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
86
            case 405: throw new HttpMethodNotAllowedException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
87
            case 410:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
88
                throw class_exists(HttpGoneException::class)
89
                    ? new HttpGoneException($request, $this->getBody($response))
90
                    : new HttpException($request, $this->getBody($response), $status);
91
            case 500: throw new HttpInternalServerErrorException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
92
            case 501: throw new HttpNotImplementedException($request, $this->getBody($response));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
93
        }
94
95
        if ($status >= 400) {
96
            throw new HttpException($request, $this->getBody($response), $status);
97
        }
98
    }
99
100
    protected function getBody(ResponseInterface $response): string|null
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getBody()
Loading history...
101
    {
102
        $body = (string)$response->getBody();
103
        return $body !== '' ? $body : null;
104
    }
105
}