HttpException::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Basis\Exception;
6
7
use Exception;
8
use HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface;
9
use Throwable;
10
11
class HttpException extends Exception
12
{
13
    /**
14
     * @var int
15
     */
16
    private int $statusCode;
17
18
    /**
19
     * @var string
20
     */
21
    private string $reasonPhrase;
22
23
    /**
24
     * @param int $statusCode
25
     * @param string|null $reasonPhrase
26
     * @param Throwable|null $previous
27
     */
28 39
    public function __construct(int $statusCode, ?string $reasonPhrase = null, ?Throwable $previous = null)
29
    {
30 39
        $reasonPhrase ??= '';
31
32 39
        if ($reasonPhrase === '' && isset(ErrorResponseGeneratorInterface::ERROR_PHRASES[$statusCode])) {
33 28
            $reasonPhrase = ErrorResponseGeneratorInterface::ERROR_PHRASES[$statusCode];
34
        }
35
36 39
        $this->statusCode = $statusCode;
37 39
        $this->reasonPhrase = $reasonPhrase;
38 39
        parent::__construct($reasonPhrase, $statusCode, $previous);
39
    }
40
41
    /**
42
     * @return int
43
     */
44 35
    public function getStatusCode(): int
45
    {
46 35
        return $this->statusCode;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 35
    public function getReasonPhrase(): string
53
    {
54 35
        return $this->reasonPhrase;
55
    }
56
57
    /**
58
     * @return string
59
     */
60 26
    public function getTitle(): string
61
    {
62 26
        return $this->statusCode . ($this->reasonPhrase ? ' ' . $this->reasonPhrase : '');
63
    }
64
}
65