Passed
Push — master ( f594d2...bb0a0f )
by Paweł
03:39
created

ProblemDetails::fromThrowable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 9
rs 10
ccs 7
cts 7
cp 1
crap 3
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Http\Dto;
10
11
use Gorynych\Http\Exception\HttpException;
12
use Symfony\Component\HttpFoundation\Response;
13
14
/**
15
 * Errors representation according to the API Problem spec (RFC 7807)
16
 *
17
 * @see https://tools.ietf.org/html/rfc7807
18
 */
19
final class ProblemDetails
20
{
21
    public string $type;
22
    public string $title;
23
    public int $status;
24
    public string $detail;
25
26 2
    public static function fromThrowable(\Throwable $throwable): self
27
    {
28 2
        $self = new self();
29 2
        $self->type = 'https://tools.ietf.org/html/rfc2616#section-10';
30 2
        $self->title = 'An error occurred';
31 2
        $self->status = $throwable instanceof HttpException ? $throwable->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR;
32 2
        $self->detail = $throwable instanceof HttpException ? $throwable->getMessage() : 'Internal server error.';
33
34 2
        return $self;
35
    }
36
}
37