Completed
Pull Request — master (#1)
by Woody
27:07 queued 01:07
created

HttpException::badRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Equip\Exception;
4
5
use RuntimeException;
6
use Psr\Http\Message\ResponseInterface;
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
            $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 1
    public static function badRequest($message)
49
    {
50 1
        return new static(sprintf(
51 1
            'Cannot parse the request: %s',
52
            $message
53 1
        ), 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 ($this->allowed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->allowed of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69 2
            $response = $response->withHeader('Allow', implode(',', $this->allowed));
70
        }
71
72 3
        return $response;
73
    }
74
}
75