Error   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 245
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setSource() 0 7 3
A setId() 0 4 1
A getStatus() 0 4 1
A setStatus() 0 70 2
A setTitle() 0 8 2
A setDetail() 0 8 2
A setCode() 0 4 1
A setAboutLink() 0 8 2
A setMeta() 0 4 1
A jsonSerialize() 0 15 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/20/15
5
 * Time: 7:22 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Server\Errors;
11
12
/**
13
 * Class ErrorBag.
14
 */
15
class Error implements \JsonSerializable
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $id;
21
    /**
22
     * @var array
23
     */
24
    protected $links = [];
25
    /**
26
     * @var int
27
     */
28
    protected $status;
29
    /**
30
     * @var string
31
     */
32
    protected $code;
33
    /**
34
     * @var string
35
     */
36
    protected $title;
37
    /**
38
     * @var string
39
     */
40
    protected $detail;
41
    /**
42
     * @var array
43
     */
44
    protected $source = [];
45
    /**
46
     * @var mixed
47
     */
48
    protected $meta;
49
50
    /**
51
     * @param string $title
52
     * @param string $message
53
     * @param string $code
54
     */
55
    public function __construct($title, $message, $code = '')
56
    {
57
        $this->setTitle($title);
58
        $this->setDetail($message);
59
        $this->setCode($code);
60
    }
61
62
    /**
63
     * @param string $key
64
     * @param string $value
65
     *
66
     * @throws \InvalidArgumentException
67
     */
68
    public function setSource($key, $value)
69
    {
70
        if ($key !== 'pointer' && $key !== 'parameter') {
71
            throw new \InvalidArgumentException('Key must be "pointer" or "parameter".');
72
        }
73
        $this->source = [(string) $key => (string) $value];
74
    }
75
76
    /**
77
     * A unique identifier for this particular occurrence of the problem.
78
     *
79
     * @param $id
80
     */
81
    public function setId($id)
82
    {
83
        $this->id = $id;
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function getStatus()
90
    {
91
        return $this->status;
92
    }
93
94
    /**
95
     * The HTTP status code applicable to this problem, expressed as a string value.
96
     *
97
     * @param $status
98
     *
99
     * @throws \InvalidArgumentException
100
     */
101
    public function setStatus($status)
102
    {
103
        $validStatus = [
104
            100,
105
            101,
106
            102,
107
            200,
108
            201,
109
            202,
110
            203,
111
            204,
112
            205,
113
            206,
114
            207,
115
            208,
116
            300,
117
            301,
118
            302,
119
            303,
120
            304,
121
            305,
122
            306,
123
            307,
124
            400,
125
            401,
126
            402,
127
            403,
128
            404,
129
            405,
130
            406,
131
            407,
132
            408,
133
            409,
134
            410,
135
            411,
136
            412,
137
            413,
138
            414,
139
            415,
140
            416,
141
            417,
142
            418,
143
            422,
144
            423,
145
            424,
146
            425,
147
            426,
148
            428,
149
            429,
150
            431,
151
            500,
152
            501,
153
            502,
154
            503,
155
            504,
156
            505,
157
            506,
158
            507,
159
            508,
160
            511,
161
        ];
162
163
        if (false === in_array($status, $validStatus, false)) {
164
            throw new \InvalidArgumentException(sprintf(
165
                'Provided status does not match a valid HTTP Status Code.',
166
                $status
167
            ));
168
        }
169
        $this->status = (int) $status;
170
    }
171
172
    /**
173
     * A short, human-readable summary of the problem that SHOULD NOT change from occurrence to
174
     * occurrence of the problem, except for purposes of localization.
175
     *
176
     * @param string $title
177
     *
178
     * @throws \InvalidArgumentException
179
     */
180
    protected function setTitle($title)
181
    {
182
        if (0 === strlen(trim($title))) {
183
            throw new \InvalidArgumentException('Provided title cannot be empty');
184
        }
185
186
        $this->title = (string) $title;
187
    }
188
189
    /**
190
     * A human-readable explanation specific to this occurrence of the problem.
191
     *
192
     * @param $detail
193
     *
194
     * @throws \InvalidArgumentException
195
     */
196
    protected function setDetail($detail)
197
    {
198
        if (0 === strlen(trim($detail))) {
199
            throw new \InvalidArgumentException('Provided error message cannot be empty');
200
        }
201
202
        $this->detail = (string) $detail;
203
    }
204
205
    /**
206
     * An application-specific error code, expressed as a string value.
207
     *
208
     * @param string $code
209
     */
210
    public function setCode($code)
211
    {
212
        $this->code = (string) $code;
213
    }
214
215
    /**
216
     * A link that leads to further details about this particular occurrence of the problem.
217
     *
218
     * @param string $link
219
     *
220
     * @throws \InvalidArgumentException
221
     */
222
    public function setAboutLink($link)
223
    {
224
        if (false === filter_var($link, FILTER_VALIDATE_URL)) {
225
            throw new \InvalidArgumentException(sprintf('Provided link %s is not a valid resource', $link));
226
        }
227
228
        $this->links['about']['href'] = (string) $link;
229
    }
230
231
    /**
232
     * A meta object containing non-standard meta-information about the error.
233
     *
234
     * @param mixed $meta
235
     */
236
    public function setMeta($meta)
237
    {
238
        $this->meta = $meta;
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function jsonSerialize()
245
    {
246
        return array_filter(
247
            [
248
                'id' => $this->id,
249
                'links' => $this->links,
250
                'status' => $this->status,
251
                'code' => $this->code,
252
                'title' => $this->title,
253
                'detail' => $this->detail,
254
                'source' => $this->source,
255
                'meta' => $this->meta,
256
            ]
257
        );
258
    }
259
}
260