Passed
Branch 2.0 (f5ddb0)
by Samuel
04:04
created

Response::setStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMartins\Exceptions\JsonApi;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Support\Collection;
7
8
class Response
9
{
10
    /**
11
     * The HTTP status code.
12
     *
13
     * @var int
14
     */
15
    protected $status;
16
17
    /**
18
     * The errors on response.
19
     *
20
     * @var array
21
     */
22
    protected $errors = [];
23
24
    /**
25
     * Create new JsonApi response passing the errors.
26
     *
27
     * @param mixed $errors
28
     */
29
    public function __construct($errors)
30
    {
31
        if ($errors instanceof ErrorCollection) {
32
            $this->errors = $errors;
0 ignored issues
show
Documentation Bug introduced by
It seems like $errors of type SMartins\Exceptions\JsonApi\ErrorCollection is incompatible with the declared type array of property $errors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
        } elseif (is_array($errors) || $errors instanceof Collection) {
34
            $this->errors = new ErrorCollection($errors);
35
        } elseif ($errors instanceof Error) {
36
            $this->errors = (new ErrorCollection)->push($errors);
37
            $this->errors->setStatusCode($errors->getStatus());
38
        }
39
40
        if (! $this->errors instanceof ErrorCollection) {
0 ignored issues
show
introduced by
$this->errors is never a sub-type of SMartins\Exceptions\JsonApi\ErrorCollection. If $this->errors can have other possible types, add them to src/JsonApi/Response.php:20.
Loading history...
41
            throw new InvalidArgumentException('The errors must be an array, '.Collection::class.','.Error::class.' or '.ErrorCollection::class.'.');
0 ignored issues
show
Bug introduced by
The type SMartins\Exceptions\Json...nvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
42
        }
43
44
        $this->errors->validate();
45
46
        $this->setStatus($this->errors->getStatusCode());
47
    }
48
49
    /**
50
     * Get the HTTP status code.
51
     *
52
     * @return  int
53
     */
54
    public function getStatus()
55
    {
56
        return $this->status;
57
    }
58
59
    /**
60
     * Set the HTTP status code.
61
     *
62
     * @param  int  $status  The HTTP status code.
63
     *
64
     * @return  self
65
     */
66
    public function setStatus(int $status)
67
    {
68
        $this->status = $status;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Get the errors on response.
75
     *
76
     * @return  array
77
     */
78
    public function getErrors()
79
    {
80
        return $this->errors;
81
    }
82
83
    /**
84
     * Convert the object to its JSON representation.
85
     *
86
     * @param  int  $options
87
     * @return string
88
     */
89
    public function json()
90
    {
91
        return new JsonResponse(
92
            ['errors' => $this->getErrors()->toArray()],
93
            $this->getStatus()
94
        );
95
    }
96
}
97