Completed
Push — master ( 62946e...cfb4fd )
by
unknown
07:23
created

Result   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 56.82%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 151
ccs 25
cts 44
cp 0.5682
rs 10
wmc 16
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A data() 0 9 2
A code() 0 9 2
A exception() 0 9 2
A setPayload() 0 4 1
A payload() 0 11 3
A toArray() 0 13 2
A __debugInfo() 0 4 1
1
<?php
2
/**
3
 * Copyright 2016, 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, 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
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->data($data);
62
        }
63 60
        if ($code !== null) {
64
            $this->code($code);
65
        }
66 60
    }
67
68
    /**
69
     * Data api method.
70
     *
71
     * @param array $value data to be delivered for the api
72
     * @return array
73
     */
74 56
    public function data($value = null)
75
    {
76 56
        if ($value === null) {
77 56
            return $this->_data;
78
        }
79 56
        $this->_data = $value;
80
81 56
        return $this->_data;
82
    }
83
84
    /**
85
     * Code api method.
86
     *
87
     * @param int $value code of the api request
88
     * @return int
89
     */
90 60
    public function code($value = null)
91
    {
92 60
        if ($value === null) {
93 60
            return $this->_code;
94
        }
95 58
        $this->_code = $value;
96
97 58
        return $this->_code;
98
    }
99
100
    /**
101
     * Exception api.
102
     *
103
     * @param Exception $value set the excepltion value
104
     * @return Exception
105
     */
106 56
    public function exception($value = null)
107
    {
108 56
        if ($value === null) {
109 56
            return $this->_exception;
110
        }
111 8
        $this->_exception = $value;
112
113 8
        return $this->_exception;
114
    }
115
116
    /**
117
     * Payload setter.
118
     *
119
     * @param string $key the key to be setted in the payload
120
     * @param mixed $value value to be setted as payload
121
     * @return void
122
     */
123 30
    public function setPayload($key, $value)
124
    {
125 30
        $this->_payload[$key] = $value;
126 30
    }
127
128
    /**
129
     * Payload api method
130
     *
131
     * @param string $key the key to get the payload
132
     * @return mixed
133
     */
134 54
    public function payload($key = null)
135
    {
136 54
        if ($key === null) {
137 54
            return $this->_payload;
138
        }
139
        if (isset($this->_payload[$key])) {
140
            return $this->_payload[$key];
141
        }
142
143
        return null;
144
    }
145
146
    public function toArray()
147
    {
148
        $info = [
149
            'code' => $this->_code,
150
            'data' => $this->_data,
151
            'payload' => $this->_payload,
152
        ];
153
        if ($this->_exception !== null) {
154
            $info['exception'] = $this->_exception->getMessage();
155
            $info['exceptionStack'] = $this->_exception->getTraceAsString();
156
        }
157
        return $info;
158
    }
159
160
    /**
161
     * Returns an array that can be used to describe the internal state of this
162
     * object.
163
     *
164
     * @return array
165
     */
166
    public function __debugInfo()
167
    {
168
        return $this->toArray();
169
    }
170
171
}
172