Passed
Push — 2.0 ( 50041b...cfd0e0 )
by Samuel
02:05
created

Response   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A setStatus() 0 5 1
A __construct() 0 5 1
A getStatus() 0 3 1
A json() 0 5 1
1
<?php
2
3
namespace SMartins\Exceptions\JsonApi;
4
5
use Illuminate\Http\JsonResponse;
6
7
class Response
8
{
9
    /**
10
     * The HTTP status code.
11
     *
12
     * @var int
13
     */
14
    protected $status;
15
16
    /**
17
     * The errors on response.
18
     *
19
     * @var \SMartins\Exceptions\JsonApi\ErrorCollection
20
     */
21
    protected $errors;
22
23
    /**
24
     * Create new JsonApi response passing the errors.
25
     *
26
     * @param \SMartins\Exceptions\JsonApi\ErrorCollection $errors
27
     */
28
    public function __construct(ErrorCollection $errors)
29
    {
30
        $this->errors = $errors;
31
32
        $this->setStatus((int) $this->errors->getStatusCode());
33
    }
34
35
    /**
36
     * Get the HTTP status code.
37
     *
38
     * @return  int
39
     */
40
    public function getStatus()
41
    {
42
        return $this->status;
43
    }
44
45
    /**
46
     * Set the HTTP status code.
47
     *
48
     * @param  int  $status  The HTTP status code.
49
     *
50
     * @return  self
51
     */
52
    public function setStatus(int $status)
53
    {
54
        $this->status = $status;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Get the errors on response.
61
     *
62
     * @return  array
63
     */
64
    public function getErrors()
65
    {
66
        return $this->errors;
67
    }
68
69
    /**
70
     * Convert the object to its JSON representation.
71
     *
72
     * @return string
73
     */
74
    public function json()
75
    {
76
        return new JsonResponse(
77
            ['errors' => $this->getErrors()->toArray()],
78
            $this->getStatus()
79
        );
80
    }
81
}
82