Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

ErrorResponseBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A error() 0 7 1
A data() 0 6 1
A getOutput() 0 4 1
A validateStatusCode() 0 6 3
1
<?php
2
3
namespace Flugg\Responder\Http\Responses;
4
5
use Flugg\Responder\Contracts\ErrorFactory;
6
use Flugg\Responder\Contracts\ResponseFactory;
7
use InvalidArgumentException;
8
9
/**
10
 * A builder class for building error responses.
11
 *
12
 * @package flugger/laravel-responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
class ErrorResponseBuilder extends ResponseBuilder
17
{
18
    /**
19
     * A factory for building error data output.
20
     *
21
     * @var \Flugg\Responder\Contracts\ErrorFactory
22
     */
23
    private $errorFactory;
24
25
    /**
26
     * A code representing the error.
27
     *
28
     * @var string|null
29
     */
30
    protected $errorCode = null;
31
32
    /**
33
     * A message descibing the error.
34
     *
35
     * @var string|null
36
     */
37
    protected $message = null;
38
39
    /**
40
     * Additional data included with the error.
41
     *
42
     * @var array|null
43
     */
44
    protected $data = null;
45
46
    /**
47
     * A HTTP status code for the response.
48
     *
49
     * @var int
50
     */
51
    protected $status = 500;
52
53
    /**
54
     * Construct the builder class.
55
     *
56
     * @param \Flugg\Responder\Contracts\ResponseFactory $responseFactory
57
     * @param \Flugg\Responder\Contracts\ErrorFactory    $errorFactory
58
     */
59
    public function __construct(ResponseFactory $responseFactory, ErrorFactory $errorFactory)
60
    {
61
        $this->errorFactory = $errorFactory;
62
63
        parent::__construct($responseFactory);
64
    }
65
66
    /**
67
     * Set the error code and message.
68
     *
69
     * @param  string|null $errorCode
70
     * @param  string|null $message
71
     * @return self
72
     */
73
    public function error(string $errorCode = null, string $message = null): ErrorResponseBuilder
74
    {
75
        $this->errorCode = $errorCode;
76
        $this->message = $message;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Add additional data to the error.
83
     *
84
     * @param  array $data
85
     * @return self
86
     */
87
    public function data(array $data): ErrorResponseBuilder
88
    {
89
        $this->data = array_merge((array) $this->data, $data);
90
91
        return $this;
92
    }
93
94
    /**
95
     * Get the serialized response output.
96
     *
97
     * @return array
98
     */
99
    protected function getOutput(): array
100
    {
101
        return $this->errorFactory->make($this->errorCode, $this->message, $this->data);
102
    }
103
104
    /**
105
     * Validate the HTTP status code for the response.
106
     *
107
     * @param  int $status
108
     * @return void
109
     * @throws \InvalidArgumentException
110
     */
111
    protected function validateStatusCode(int $status): void
112
    {
113
        if ($status < 400 || $status >= 600) {
114
            throw new InvalidArgumentException("{$status} is not a valid error HTTP status code.");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $status instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
115
        }
116
    }
117
}