Passed
Push — devel-3.0 ( 218a48...a00b1f )
by Rubén
03:54
created

JsonResponse::setMessages()   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 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Http;
26
27
/**
28
 * Class Json para definir la estructura de una respuesta en formato JSON
29
 *
30
 * @package SP\Http
31
 */
32
final class JsonResponse implements \JsonSerializable
33
{
34
    const JSON_SUCCESS = 0;
35
    const JSON_SUCCESS_STICKY = 100;
36
    const JSON_ERROR = 1;
37
    const JSON_ERROR_STICKY = 101;
38
    const JSON_WARNING = 2;
39
    const JSON_WARNING_STICKY = 102;
40
    const JSON_LOGOUT = 10;
41
42
    /**
43
     * @var int
44
     */
45
    protected $status = 1;
46
    /**
47
     * @var string
48
     */
49
    protected $description = '';
50
    /**
51
     * @var string
52
     */
53
    protected $action = '';
54
    /**
55
     * @var array
56
     */
57
    protected $data = [];
58
    /**
59
     * @var array
60
     */
61
    protected $messages = [];
62
    /**
63
     * @var string
64
     */
65
    protected $container = '';
66
    /**
67
     * @var string
68
     */
69
    protected $csrf = '';
70
71
    /**
72
     * JsonResponse constructor.
73
     *
74
     * @param string $description
75
     */
76
    public function __construct(string $description = null)
77
    {
78
        $this->description = $description;
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public function getStatus()
85
    {
86
        return $this->status;
87
    }
88
89
    /**
90
     * @param int $status
91
     *
92
     * @return JsonResponse
93
     */
94
    public function setStatus($status)
95
    {
96
        $this->status = (int)$status;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getDescription()
105
    {
106
        return $this->description;
107
    }
108
109
    /**
110
     * @param string $description
111
     *
112
     * @return JsonResponse
113
     */
114
    public function setDescription($description)
115
    {
116
        $this->description = __($description);
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getAction()
125
    {
126
        return $this->action;
127
    }
128
129
    /**
130
     * @param string $action
131
     *
132
     * @return JsonResponse
133
     */
134
    public function setAction($action)
135
    {
136
        $this->action = $action;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getData()
145
    {
146
        return $this->data;
147
    }
148
149
    /**
150
     * @param array|\stdClass $data
151
     *
152
     * @return JsonResponse
153
     */
154
    public function setData($data)
155
    {
156
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data can also be of type stdClass. However, the property $data is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function getMessages()
165
    {
166
        return $this->messages;
167
    }
168
169
    /**
170
     * @param array $messages
171
     *
172
     * @return JsonResponse
173
     */
174
    public function setMessages(array $messages)
175
    {
176
        $this->messages = $messages;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getContainer()
185
    {
186
        return $this->container;
187
    }
188
189
    /**
190
     * @param string $container
191
     *
192
     * @return JsonResponse
193
     */
194
    public function setContainer($container)
195
    {
196
        $this->container = $container;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getCsrf()
205
    {
206
        return $this->csrf;
207
    }
208
209
    /**
210
     * @param string $csrf
211
     *
212
     * @return JsonResponse
213
     */
214
    public function setCsrf($csrf)
215
    {
216
        $this->csrf = $csrf;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @param $message
223
     *
224
     * @return JsonResponse
225
     */
226
    public function addMessage($message)
227
    {
228
        $this->messages[] = __($message);
229
        return $this;
230
    }
231
232
    /**
233
     * @param $param
234
     *
235
     * @return $this
236
     */
237
    public function addParam($param)
238
    {
239
        if (is_numeric($param)) {
240
            $param = (int)$param;
241
        }
242
243
        $this->data[] = $param;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Specify data which should be serialized to JSON
250
     *
251
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
252
     * @return mixed data which can be serialized by <b>json_encode</b>,
253
     * which is a value of any type other than a resource.
254
     * @since 5.4.0
255
     */
256
    public function jsonSerialize()
257
    {
258
        return $this->getJsonArray();
259
    }
260
261
    /**
262
     * Devolver un array con las propiedades del objeto
263
     *
264
     * @return array
265
     */
266
    public function getJsonArray()
267
    {
268
        $out = [];
269
270
        foreach ($this as $key => $value) {
271
            $out[$key] = $value;
272
        }
273
274
        return $out;
275
    }
276
277
    /**
278
     * Establecer los valores por defecto
279
     *
280
     * @return JsonResponse
281
     */
282
    public function clear()
283
    {
284
        $this->status = 0;
285
        $this->action = '';
286
        $this->data = [];
287
        $this->messages = [];
288
        $this->container = '';
289
        $this->csrf = '';
290
291
        return $this;
292
    }
293
}