Completed
Push — master ( a6cd6e...bb5a47 )
by
unknown
02:15
created

Main::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Anetwork\Respond;
4
5
use Illuminate\Http\Response;
6
7
class Main {
8
9
	/**
10
	 * Http status code
11
	 * @var integer
12
	 */
13
	protected $statusCode = 200;
14
15
	/**
16
	 * Status text
17
	 * @var string
18
	 */
19
	protected $statusText = 'success';
20
21
	/**
22
	 * Error code, message and text-key
23
	 * @var array
24
	 */
25
	protected $error;
26
27
	/**
28
	 * Error code
29
	 * @var integer
30
	 */
31
	protected $errorCode;
32
33
	/**
34
	 * Haeders
35
	 * @var array
36
	 */
37
	protected $headers = [];
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $lang;
43
44
	/**
45
	 * @var arry
46
	 */
47
	protected $config;
48
49
	/**
50
	 * @author Shahrokh Niakan <[email protected]>
51
	 * @since Sep 24, 2016
52
	 */
53
	public function __construct() {
54
55
		$this->lang = \App::getLocale();
56
57
		$this->config = include __DIR__ . '/../errors/lang/' . $this->lang . '.php';
58
59
	}
60
61
	/**
62
	 * Getter for $statusCode
63
	 * @author Shima Payro <[email protected]>
64
	 * @since May 2, 2016 9:46:27 AM
65
	 * @uses
66
	 * @see
67
	 */
68
	public function getStatusCode() {
69
70
		return $this->statusCode;
71
72
	}
73
74
	/**
75
	 * Setter for $statusCode
76
	 * @param integer $statusCode
77
	 * @return App\Htpp\Responds\Respond
78
	 * @author Shima Payro <[email protected]>
79
	 * @since May 2, 2016 9:47:04 AM
80
	 * @uses
81
	 * @see
82
	 */
83
	public function setStatusCode( $statusCode ) {
84
85
		$this->statusCode = $statusCode;
86
87
		return $this;
88
89
	}
90
91
	/**
92
	 * Getter for $statusText
93
	 * @author Shima Payro <[email protected]>
94
	 * @since May 2, 2016 9:47:36 AM
95
	 * @uses
96
	 * @see
97
	 */
98
	public function getStatusText() {
99
100
		return $this->statusText;
101
102
	}
103
104
	/**
105
	 * Setter for $statusText
106
	 * @param String $statusText
107
	 * @return App\HtppApp\Htpp\Responds\Respond
108
	 * @author Shima Payro <[email protected]>
109
	 * @since May 2, 2016 9:48:23 AM
110
	 * @uses
111
	 * @see
112
	 */
113
	public function setStatusText( $statusText ) {
114
115
		$this->statusText = $statusText;
116
117
		return $this;
118
119
	}
120
121
	/**
122
	 * Response
123
	 * @param json $data
124
	 * @return jsom
125
	 * @author Shima Payro <[email protected]>
126
	 * @since May 2, 2016 9:48:45 AM
127
	 * @uses
128
	 * @see
129
	 */
130
	public function respond( $data ) {
131
132
		$result = array_filter( $this->getHeaders() );
133
134
		if ( empty( $result ) )
135
			return response()->json( $data, $this->getStatusCode() );
136
137
		return response()->json( $data, $this->getStatusCode() )
138
						->withHeaders( $this->getHeaders() );
139
140
	}
141
142
	/**
143
	 * Response which conteins just a message
144
	 * @param string $message
145
	 * @author Shima Payro <[email protected]>
146
	 * @since May 2, 2016 9:49:21 AM
147
	 * @return json
148
	 * @uses
149
	 * @see
150
	 */
151
	public function respondWithMessage( $message ) {
152
153
		$res[ 'status' ] = $this->getStatusText();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
154
155
		//if it's about failure
156
		if ( $this->getErrorCode() ) {
157
158
			$res[ 'error' ] = $this->getErrorCode();
159
160
			if ( empty( $message ) )
161
				$res[ 'message' ] = $this->getErrorMessage();
162
163
			$res[ 'message' ] = $message;
164
165
		} else {
166
167
			$res[ 'message' ] = $message;
168
169
		}
170
171
		return $this->respond( $res );
0 ignored issues
show
Documentation introduced by
$res is of type array<string,string|integer,{"message":"string"}>, but the function expects a object<Anetwork\Respond\json>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
173
	}
174
175
	/**
176
	 * Set error code in our result
177
	 * @author Mehdi Hosseini <[email protected]>
178
	 * @since August 24, 2016
179
	 * @param $errorCode integer
180
	 * @return instance
181
	 */
182
	public function setErrorCode( $errorCode ) {
183
184
		$this->error = $this->config[ $errorCode ];
185
186
		$this->errorCode = $errorCode;
187
188
		return $this;
189
190
	}
191
192
	/**
193
	 * Return Error code
194
	 * @author Mehdi Hosseini <[email protected]>
195
	 * @since August 24, 2016
196
	 * @return integer
197
	 */
198
	public function getErrorCode() {
199
200
		return $this->errorCode;
201
202
	}
203
204
	/**
205
	 * Get error message
206
	 * @author Mehdi Hosseini <[email protected]>
207
	 * @since August 24, 2016
208
	 * @return string
209
	 */
210
	public function getErrorMessage() {
211
212
		return $this->error['message'];
213
214
	}
215
216
	/**
217
	 * Get headers
218
	 * @author Shima Payro <[email protected]>
219
	 * @since Sep 13, 2016
220
	 * @return array
221
	 */
222
	public function getHeaders() {
223
224
		return $this->headers;
225
226
	}
227
228
	/**
229
	 * Set headers
230
	 * @author Shima Payro <[email protected]>
231
	 * @since Sep 13, 2016
232
	 * @return array
233
	 */
234
	public function setHeaders( $headers = [] ) {
235
236
		$this->headers = $headers;
237
238
		return $this;
239
240
	}
241
242
	/**
243
	 * Response which contains status and data
244
	 * @param json $data
245
	 * @author Shima Payro <[email protected]>
246
	 * @since May 2, 2016 9:50:19 AM
247
	 * @return json
248
	 * @uses
249
	 * @see
250
	 */
251
	 public function respondWithResult( $data = NULL ) {
252
253
		$res[ 'status' ] = $this->getStatusText();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
254
255
		//if it's about laravel validation error
256
		if ( $this->getErrorCode() && $this->getStatusCode() == 420 ) {
257
258
			$res[ 'error' ] = $this->getErrorCode();
259
			$res[ 'message' ] = $data;
260
261
		} else {
262
263
			$res[ 'result' ] = $data;
264
265
		}
266
267
		return $this->respond( $res );
0 ignored issues
show
Documentation introduced by
$res is of type array<string,?>, but the function expects a object<Anetwork\Respond\json>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
268
269
	}
270
271
}
272