Status::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 11
c 1
b 1
f 0
dl 0
loc 20
rs 9.9
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Error;
6
7
use BEAR\Resource\Exception\BadRequestException;
8
use Koriym\HttpConstants\StatusCode;
9
use RuntimeException;
10
use Throwable;
11
12
final class Status
13
{
14
    /** @var int */
15
    public $code;
16
17
    /** @var string */
18
    public $text;
19
20
    public function __construct(Throwable $e)
21
    {
22
        /** @var array<int, string> $text */
23
        $text = (new StatusCode())->statusText;
24
        if ($e instanceof BadRequestException) {
25
            $this->code = $e->getCode();
26
            $this->text = $text[$this->code] ?? '';
27
28
            return;
29
        }
30
31
        if ($e instanceof RuntimeException) {
32
            $this->code = 503;
33
            $this->text = $text[$this->code];
34
35
            return;
36
        }
37
38
        $this->code = 500;
39
        $this->text = $text[$this->code];
40
    }
41
}
42