Passed
Push — develop ( 7b3a44...b44bab )
by Pablo
02:19
created

ApiResponse::setBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * The response object for API Controllers
5
 *
6
 * @package     Nails
7
 * @subpackage  module-api
8
 * @category    Factory
9
 * @author      Nails Dev Team
10
 * @link
11
 */
12
13
namespace Nails\Api\Factory;
14
15
use Nails\Common\Exception\ValidationException;
16
use Nails\Common\Service\HttpCodes;
17
18
/**
19
 * Class ApiResponse
20
 *
21
 * @package Nails\Api\Factory
22
 */
23
class ApiResponse
24
{
25
    /**
26
     * The API Response code
27
     *
28
     * @var int
29
     */
30
    protected $iCode = HttpCodes::STATUS_OK;
31
32
    /**
33
     * If defined, this will be the exact body of the response
34
     *
35
     * @var string|null
36
     */
37
    protected $sBody = null;
38
39
    /**
40
     * The payload for the response
41
     *
42
     * @var mixed
43
     */
44
    protected $mData;
45
46
    /**
47
     * Additional data to return
48
     *
49
     * @var array
50
     */
51
    protected $aMeta = [];
52
53
    // --------------------------------------------------------------------------
54
55
    /**
56
     * Set the response code
57
     *
58
     * @param int $iCode The API response code to use
59
     *
60
     * @return $this
61
     */
62
    public function setCode(int $iCode)
63
    {
64
        if ($iCode < 100 || $iCode > 299) {
65
            throw new ValidationException('Response code must be in the range 100-299');
66
        }
67
68
        $this->iCode = $iCode;
69
        return $this;
70
    }
71
72
    // --------------------------------------------------------------------------
73
74
    /**
75
     * Get the response code
76
     *
77
     * @return mixed
78
     */
79
    public function getCode(): int
80
    {
81
        return $this->iCode;
82
    }
83
84
    // --------------------------------------------------------------------------
85
86
    /**
87
     * Sets the body value
88
     *
89
     * @param string $sBody
90
     */
91
    public function setBody(string $sBody): self
92
    {
93
        if (!empty($this->mData)) {
94
            throw new ValidationException(
95
                'Cannot set response body when a data has been set'
96
            );
97
        }
98
99
        $this->sBody = $sBody;
100
        return $this;
101
    }
102
103
    // --------------------------------------------------------------------------
104
105
    /**
106
     * Returns the body
107
     *
108
     * @return string|null
109
     */
110
    public function getBody(): ?string
111
    {
112
        return $this->sBody;
113
    }
114
115
    // --------------------------------------------------------------------------
116
117
    /**
118
     * Set the response payload
119
     *
120
     * @param $mData
121
     *
122
     * @return $this
123
     */
124
    public function setData($mData)
125
    {
126
        if ($this->sBody !== null) {
127
            throw new ValidationException(
128
                'Cannot set response data when a body has been set'
129
            );
130
        }
131
132
        $this->mData = $mData;
133
        return $this;
134
    }
135
136
    // --------------------------------------------------------------------------
137
138
    /**
139
     * Get the response payload
140
     *
141
     * @return mixed
142
     */
143
    public function getData()
144
    {
145
        return $this->mData;
146
    }
147
148
    // --------------------------------------------------------------------------
149
150
    /**
151
     * Set the response meta
152
     *
153
     * @param array $aMeta
154
     *
155
     * @return $this
156
     */
157
    public function setMeta(array $aMeta)
158
    {
159
        $this->aMeta = $aMeta;
160
        return $this;
161
    }
162
163
    // --------------------------------------------------------------------------
164
165
    /**
166
     * Get the response meta
167
     *
168
     * @return array
169
     */
170
    public function getMeta()
171
    {
172
        return $this->aMeta;
173
    }
174
}
175