Completed
Push — master ( cc5f29...ed9e69 )
by Dominik
02:18
created

HttpException::getMessageByStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ErrorHandler;
6
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Http\Message\ResponseInterface as Response;
9
10
final class HttpException extends \RuntimeException
11
{
12
    /**
13
     * @var Request
14
     */
15
    private $request;
16
17
    /**
18
     * @var Response
19
     */
20
    private $response;
21
22
    const STATUS_400 = 'Bad Request';
23
    const STATUS_401 = 'Unauthorized';
24
    const STATUS_402 = 'Payment Required';
25
    const STATUS_403 = 'Forbidden';
26
    const STATUS_404 = 'Not Found';
27
    const STATUS_405 = 'Method Not Allowed';
28
    const STATUS_406 = 'Not Acceptable';
29
    const STATUS_407 = 'Proxy Authentication Required';
30
    const STATUS_408 = 'Request Time-out';
31
    const STATUS_409 = 'Conflict';
32
    const STATUS_410 = 'Gone';
33
    const STATUS_411 = 'Length Required';
34
    const STATUS_412 = 'Precondition Failed';
35
    const STATUS_413 = 'Request Entity Too Large';
36
    const STATUS_414 = 'Request-URL Too Long';
37
    const STATUS_415 = 'Unsupported Media Type';
38
    const STATUS_416 = 'Requested range not satisfiable';
39
    const STATUS_417 = 'Expectation Failed';
40
    const STATUS_418 = 'I’m a teapot';
41
    const STATUS_420 = 'Policy Not Fulfilled';
42
    const STATUS_421 = 'Misdirected Request';
43
    const STATUS_422 = 'Unprocessable Entity';
44
    const STATUS_423 = 'Locked';
45
    const STATUS_424 = 'Failed Dependency';
46
    const STATUS_425 = 'Unordered Collection';
47
    const STATUS_426 = 'Upgrade Required';
48
    const STATUS_428 = 'Precondition Required';
49
    const STATUS_429 = 'Too Many Requests';
50
    const STATUS_431 = 'Request Header Fields Too Large';
51
    const STATUS_451 = 'Unavailable For Legal Reasons';
52
    const STATUS_444 = 'No Response';
53
    const STATUS_449 = 'The request should be retried after doing the appropriate action';
54
55
    const STATUS_500 = 'Internal Server Error';
56
    const STATUS_501 = 'Not Implemented';
57
    const STATUS_502 = 'Bad Gateway';
58
    const STATUS_503 = 'Service Unavailable';
59
    const STATUS_504 = 'Gateway Time-out';
60
    const STATUS_505 = 'HTTP Version not supported';
61
    const STATUS_506 = 'Variant Also Negotiates';
62
    const STATUS_507 = 'Insufficient Storage';
63
    const STATUS_508 = 'Loop Detected';
64
    const STATUS_509 = 'Bandwidth Limit Exceeded';
65
    const STATUS_510 = 'Not Extended';
66
    const STATUS_511 = 'Network Authentication Required';
67
68
    /**
69
     * @param Request     $request
70
     * @param Response    $response
71
     * @param int         $status
72
     * @param string|null $message
73
     *
74
     * @return HttpException
75
     */
76
    public static function create(Request $request, Response $response, int $status, string $message = null): self
77
    {
78
        $exception = new self($message ?? self::getMessageByStatus($status), $status);
79
80
        $exception->request = $request;
81
        $exception->response = $response;
82
83
        return $exception;
84
    }
85
86
    /**
87
     * @param int $status
88
     *
89
     * @return string
90
     */
91
    private static function getMessageByStatus(int $status): string
92
    {
93
        $statusConstantName = 'STATUS_'.$status;
94
        $reflection = new \ReflectionClass(self::class);
95
        if ($reflection->hasConstant($statusConstantName)) {
96
            return $reflection->getConstant($statusConstantName);
97
        }
98
99
        return 'unknown';
100
    }
101
102
    /**
103
     * @return Request
104
     */
105
    public function getRequest(): Request
106
    {
107
        return $this->request;
108
    }
109
110
    /**
111
     * @return Response
112
     */
113
    public function getResponse(): Response
114
    {
115
        return $this->response;
116
    }
117
}
118