Completed
Push — master ( b1ec47...2bda8d )
by
unknown
04:46
created

Main::getHeaders()   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 0
dl 0
loc 5
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
	 * Getter for $statusCode
41
	 * @author Shima Payro <[email protected]>
42
	 * @since May 2, 2016 9:46:27 AM
43
	 * @uses
44
	 * @see
45
	 */
46
	public function getStatusCode() {
47
48
		return $this->statusCode;
49
50
	}
51
52
	/**
53
	 * Setter for $statusCode
54
	 * @param integer $statusCode
55
	 * @return App\Htpp\Responds\Respond
56
	 * @author Shima Payro <[email protected]>
57
	 * @since May 2, 2016 9:47:04 AM
58
	 * @uses
59
	 * @see
60
	 */
61
	public function setStatusCode( $statusCode ) {
62
63
		$this->statusCode = $statusCode;
64
65
		return $this;
66
67
	}
68
69
	/**
70
	 * Getter for $statusText
71
	 * @author Shima Payro <[email protected]>
72
	 * @since May 2, 2016 9:47:36 AM
73
	 * @uses
74
	 * @see
75
	 */
76
	public function getStatusText() {
77
78
		return $this->statusText;
79
80
	}
81
82
	/**
83
	 * Setter for $statusText
84
	 * @param String $statusText
85
	 * @return App\HtppApp\Htpp\Responds\Respond
86
	 * @author Shima Payro <[email protected]>
87
	 * @since May 2, 2016 9:48:23 AM
88
	 * @uses
89
	 * @see
90
	 */
91
	public function setStatusText( $statusText ) {
92
93
		$this->statusText = $statusText;
94
95
		return $this;
96
97
	}
98
99
	/**
100
	 * Response
101
	 * @param json $data
102
	 * @return jsom
103
	 * @author Shima Payro <[email protected]>
104
	 * @since May 2, 2016 9:48:45 AM
105
	 * @uses
106
	 * @see
107
	 */
108
	public function respond( $data ) {
109
110
		$result = array_filter( $this->getHeaders() );
111
112
		if ( empty( $result ) );
113
			return response()->json( $data, $this->getStatusCode() );
114
			
115
		return response()->json( $data, $this->getStatusCode() )
0 ignored issues
show
Unused Code introduced by
return response()->json(...s($this->getHeaders()); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
116
						->withHeaders( $this->getHeaders() );
117
118
	}
119
120
	/**
121
	 * Response which conteins just a message
122
	 * @param string $message
123
	 * @author Shima Payro <[email protected]>
124
	 * @since May 2, 2016 9:49:21 AM
125
	 * @return json
126
	 * @uses
127
	 * @see
128
	 */
129 View Code Duplication
	public function respondWithMessage( $message = NULL ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
131
		$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...
132
133
		//if it's about failure
134
		if ( $this->getErrorCode() ) {
135
136
			$res[ 'error' ] = $this->getErrorCode();
137
			$res[ 'message' ] = $this->getErrorMessage();
138
			
139
		} else {
140
141
			$res[ 'message' ] = $message;
142
143
		}
144
145
		return $this->respond( $res );
0 ignored issues
show
Documentation introduced by
$res is of type array<string,string|inte...essage":"string|null"}>, 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...
146
147
	}
148
149
	/**
150
	 * Set error code in our result
151
	 * @author Mehdi Hosseini <[email protected]>
152
	 * @since August 24, 2016
153
	 * @param $errorCode integer
154
	 * @return instance
155
	 */
156
	public function setErrorCode( $errorCode ) {
157
158
		$this->error = config( 'errors.' . $errorCode );
159
160
		$this->errorCode = $errorCode;
161
162
		return $this;
163
164
	}
165
166
	/**
167
	 * Return Error code
168
	 * @author Mehdi Hosseini <[email protected]>
169
	 * @since August 24, 2016
170
	 * @return integer
171
	 */
172
	public function getErrorCode() {
173
174
		return $this->errorCode;
175
176
	}
177
178
	/**
179
	 * Get error message
180
	 * @author Mehdi Hosseini <[email protected]>
181
	 * @since August 24, 2016
182
	 * @return string
183
	 */
184
	public function getErrorMessage() {
185
186
		return $this->error['message'];
187
188
	}
189
190
	/**
191
	 * Get headers
192
	 * @author Shima Payro <[email protected]>
193
	 * @since Sep 13, 2016
194
	 * @return array
195
	 */
196
	public function getHeaders() {
197
198
		return $this->headers;
199
200
	}
201
202
	/**
203
	 * Get headers
204
	 * @author Shima Payro <[email protected]>
205
	 * @since Sep 13, 2016
206
	 * @return array
207
	 */
208
	public function setHeaders( $statusText = [] ) {
0 ignored issues
show
Unused Code introduced by
The parameter $statusText is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
210
		$this->headers = $headers;
0 ignored issues
show
Bug introduced by
The variable $headers does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
211
212
		return $this;
213
214
	} 
215
216
	/**
217
	 * Response which contains status and data
218
	 * @param json $data
219
	 * @author Shima Payro <[email protected]>
220
	 * @since May 2, 2016 9:50:19 AM
221
	 * @return json
222
	 * @uses
223
	 * @see
224
	 */
225 View Code Duplication
	 public function respondWithResult( $data = NULL ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
227
		$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...
228
229
		//if it's about laravel validation error
230
		if ( $this->getErrorCode() && $this->getStatusCode() == 420 ) {
231
232
			$res[ 'error' ] = $this->getErrorCode();
233
			$res[ 'message' ] = $data;
234
235
		} else {
236
237
			$res[ 'result' ] = $data;
238
239
		}
240
241
		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...
242
243
	}
244
245
}
246