HttpException::withResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Equip\Exception;
4
5
use Psr\Http\Message\ResponseInterface;
6
use RuntimeException;
7
8
class HttpException extends RuntimeException
9
{
10
    /**
11
     * @param string $path
12
     *
13
     * @return static
14
     */
15 3
    public static function notFound($path)
16
    {
17 3
        return new static(sprintf(
18 3
            'Cannot find any resource at `%s`',
19
            $path
20 3
        ), 404);
21
    }
22
23
    /**
24
     * @param string $path
25
     * @param string $method
26
     * @param array $allowed
27
     *
28
     * @return static
29
     */
30 3
    public static function methodNotAllowed($path, $method, array $allowed)
31
    {
32 3
        $error = new static(sprintf(
33 3
            'Cannot access resource `%s` using method `%s`',
34 3
            $path,
35
            $method
36 3
        ), 405);
37
38 3
        $error->allowed = $allowed;
39
40 3
        return $error;
41
    }
42
43
    /**
44
     * @param string $message
45
     *
46
     * @return static
47
     */
48 2
    public static function badRequest($message)
49
    {
50 2
        return new static(sprintf(
51 2
            'Cannot parse the request: %s',
52
            $message
53 2
        ), 400);
54
    }
55
56
    /**
57
     * @var array
58
     */
59
    private $allowed = [];
60
61
    /**
62
     * @param ResponseInterface $response
63
     *
64
     * @return ResponseInterface
65
     */
66 3
    public function withResponse(ResponseInterface $response)
67
    {
68 3
        if (!empty($this->allowed)) {
69 2
            $response = $response->withHeader('Allow', implode(',', $this->allowed));
70 2
        }
71
72 3
        return $response;
73
    }
74
}
75