Passed
Push — master ( e5acb2...2b13b1 )
by Evgeny
07:45
created

Result::getPayload()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 3
cts 6
cp 0.5
crap 4.125
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action;
13
14
use Exception;
15
16
/**
17
 * Class Result
18
 *
19
 * @package CakeDC\Api\Service\Action
20
 */
21
class Result
22
{
23
24
    /**
25
     * Response code
26
     *
27
     * @var int
28
     */
29
    protected $_code = 200;
30
31
    /**
32
     * Response data
33
     *
34
     * @var array|mixed
35
     */
36
    protected $_data = null;
37
38
    /**
39
     * Response payload
40
     *
41
     * @var array
42
     */
43
    protected $_payload = [];
44
45
    /**
46
     * Exception structure
47
     *
48
     * @var Exception
49
     */
50
    protected $_exception = null;
51
52
    /**
53
     * Result constructor.
54
     *
55
     * @param array $data data to be delivered for the api
56
     * @param int $code code of the api request
57
     */
58 60
    public function __construct($data = null, $code = null)
59
    {
60 60
        if ($data !== null) {
61
            $this->setData($data);
62
        }
63 60
        if ($code !== null) {
64
            $this->setCode($code);
65
        }
66 60
    }
67
68
    /**
69
     * Gets a result data.
70
     *
71
     * @return array|mixed
72
     */
73 56
    public function getData()
74
    {
75 56
        return $this->_data;
76
    }
77
78
    /**
79
     * Sets a result data.
80
     *
81
     * @param array|mixed $value data to be delivered for the api
82
     * @return $this
83
     */
84 56
    public function setData($value)
85
    {
86 56
        $this->_data = $value;
87
88 56
        return $this;
89
    }
90
91
    /**
92
     * Get and set result data.
93
     *
94
     * @param array|mixed $value data to be delivered for the api
95
     * @deprecated 3.6.0 Use setData()/getData() instead.
96
     * @return array
97
     */
98
    public function data($value = null)
99
    {
100
        deprecationWarning(
101
            'Result::data() is deprecated. ' .
102
            'Use Result::setData()/getData() instead.'
103
        );
104
105
        if ($value !== null) {
106
            return $this->setData($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->setData($value); (CakeDC\Api\Service\Action\Result) is incompatible with the return type documented by CakeDC\Api\Service\Action\Result::data of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
        }
108
109
        return $this->getData();
110
    }
111
112
    /**
113
     * Gets a result code.
114
     *
115
     * @return int
116
     */
117 60
    public function getCode()
118
    {
119 60
        return $this->_code;
120
    }
121
122
    /**
123
     * Sets a result code.
124
     *
125
     * @param int $value code to be delivered for the api
126
     * @return $this
127
     */
128 59
    public function setCode($value)
129
    {
130 59
        $this->_code = $value;
131
132 59
        return $this;
133
    }
134
135
    /**
136
     * Code api method.
137
     *
138
     * @param int $value code of the api request
139
     * @deprecated 3.6.0 Use setCode()/getCode() instead.
140
     * @return int
141
     */
142
    public function code($value = null)
143
    {
144
        deprecationWarning(
145
            'Result::code() is deprecated. ' .
146
            'Use Result::setCode()/getCode() instead.'
147
        );
148
149
        if ($value !== null) {
150
            return $this->setCode($value);
151
        }
152
153
        return $this->getCode();
154
    }
155
156
    /**
157
     * Gets a result exception.
158
     *
159
     * @return Exception
160
     */
161 56
    public function getException()
162
    {
163 56
        return $this->_exception;
164
    }
165
166
    /**
167
     * Sets a result exception.
168
     *
169
     * @param Exception $value exception to be delivered for the api
170
     * @return $this
171
     */
172 8
    public function setException($value)
173
    {
174 8
        $this->_exception = $value;
175
176 8
        return $this;
177
    }
178
179
    /**
180
     * Exception api method.
181
     *
182
     * @param Exception $value exception of the api request
183
     * @deprecated 3.6.0 Use setException()/getException() instead.
184
     * @return Exception
185
     */
186
    public function exception($value = null)
187
    {
188
        deprecationWarning(
189
            'Result::exception() is deprecated. ' .
190
            'Use Result::setException()/getException() instead.'
191
        );
192
193
        if ($value !== null) {
194
            return $this->setException($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->setException($value); (CakeDC\Api\Service\Action\Result) is incompatible with the return type documented by CakeDC\Api\Service\Action\Result::exception of type Exception.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
195
        }
196
197
        return $this->getException();
198
    }
199
200
    /**
201
     * Appends value to Payload.
202
     *
203
     * @param string $key the key to be used in the payload
204
     * @param mixed $value value to be used as payload
205
     * @return void
206
     */
207 30
    public function appendPayload($key, $value)
208
    {
209 30
        $this->_payload[$key] = $value;
210 30
    }
211
212
    /**
213
     * Gets a result payload.
214
     *
215
     * @return array|mixed Payload
216
     */
217 54
    public function getPayload($key = null)
218
    {
219 54
        if ($key === null) {
220 54
            return $this->_payload;
221
        }
222
223
        if (isset($this->_payload[$key])) {
224
            return $this->_payload[$key];
225
        }
226
227
        return null;
228
    }
229
230
    /**
231
     * Sets a result payload.
232
     *
233
     * @param mixed $value payload to be delivered for the api
234
     * @return $this
235
     */
236
    public function setPayload($value)
237
    {
238
        $this->_payload = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type * is incompatible with the declared type array of property $_payload.

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...
239
240
        return $this;
241
    }
242
243
    /**
244
     * Payload api method.
245
     *
246
     * @param string $value payload of the api request
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
247
     * @deprecated 3.6.0 Use getPayload() instead.
248
     * @return mixed
249
     */
250
    public function payload($key = null)
251
    {
252
        deprecationWarning(
253
            'Result::payload() is deprecated. ' .
254
            'Use Result::getPayload() instead.'
255
        );
256
257
        return $this->getPayload($key);
258
    }
259
260
    /**
261
     * To array transformation.
262
     *
263
     * @return array
264
     */
265
    public function toArray()
266
    {
267
        $info = [
268
            'code' => $this->_code,
269
            'data' => $this->_data,
270
            'payload' => $this->_payload,
271
        ];
272
        if ($this->_exception !== null) {
273
            $info['exception'] = $this->_exception->getMessage();
274
            $info['exceptionStack'] = $this->_exception->getTraceAsString();
275
        }
276
277
        return $info;
278
    }
279
280
    /**
281
     * Returns an array that can be used to describe the internal state of this
282
     * object.
283
     *
284
     * @return array
285
     */
286
    public function __debugInfo()
287
    {
288
        return $this->toArray();
289
    }
290
}
291