HttpException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 67
ccs 20
cts 20
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A notFound() 0 7 1
A methodNotAllowed() 0 12 1
A badRequest() 0 7 1
A withResponse() 0 8 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