Completed
Push — master ( ed9e69...3bf488 )
by Dominik
03:14
created

HttpException::hasDefaultMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * @var bool
70
     */
71
    private $hasDefaultMessage;
72
73
    /**
74
     * @param Request     $request
75
     * @param Response    $response
76
     * @param int         $status
77
     * @param string|null $message
78
     *
79
     * @return HttpException
80
     */
81
    public static function create(Request $request, Response $response, int $status, string $message = null): self
82
    {
83
        $exception = new self($message ?? self::getMessageByStatus($status), $status);
84
85
        $exception->request = $request;
86
        $exception->response = $response;
87
        $exception->hasDefaultMessage = null === $message;
88
89
        return $exception;
90
    }
91
92
    /**
93
     * @param int $status
94
     *
95
     * @return string
96
     */
97
    private static function getMessageByStatus(int $status): string
98
    {
99
        $statusConstantName = 'STATUS_'.$status;
100
        $reflection = new \ReflectionClass(self::class);
101
        if ($reflection->hasConstant($statusConstantName)) {
102
            return $reflection->getConstant($statusConstantName);
103
        }
104
105
        return 'unknown';
106
    }
107
108
    /**
109
     * @return Request
110
     */
111
    public function getRequest(): Request
112
    {
113
        return $this->request;
114
    }
115
116
    /**
117
     * @return Response
118
     */
119
    public function getResponse(): Response
120
    {
121
        return $this->response;
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function hasDefaultMessage(): bool
128
    {
129
        return $this->hasDefaultMessage;
130
    }
131
}
132