1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiFinder\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Response; |
6
|
|
|
use PiFinder\Http\Controllers\Controller; |
7
|
|
|
|
8
|
|
|
class ApiController extends Controller |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Default Status Code. |
12
|
|
|
* |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $statusCode = 200; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return int |
19
|
|
|
*/ |
20
|
10 |
|
public function getStatusCode() |
21
|
|
|
{ |
22
|
10 |
|
return $this->statusCode; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param int $statusCode |
27
|
|
|
* |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
9 |
|
public function setStatusCode($statusCode) |
31
|
|
|
{ |
32
|
9 |
|
$this->statusCode = $statusCode; |
33
|
|
|
|
34
|
9 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $data |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
1 |
View Code Duplication |
public function respondCreated($data, $id) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$headers = [ |
45
|
1 |
|
'Location' => route('devices.show', $id), |
46
|
|
|
]; |
47
|
|
|
|
48
|
1 |
|
return $this->setStatusCode(201)->respond(compact('data'), $headers); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $data |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
5 |
View Code Duplication |
public function respondPoked($data, $id) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$headers = [ |
59
|
5 |
|
'Location' => route('devices.show', $id), |
60
|
|
|
]; |
61
|
|
|
|
62
|
5 |
|
return $this->setStatusCode(200)->respond(compact('data'), $headers); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
1 |
|
public function respondNoContent() |
69
|
|
|
{ |
70
|
1 |
|
return $this->setStatusCode(204)->respond([]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $message |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
3 |
|
public function respondNotFound($message = 'Did not find the resource you are looking for!') |
79
|
|
|
{ |
80
|
3 |
|
return $this->setStatusCode(404)->respondWithError($message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $data |
85
|
|
|
* @param array $headers |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
10 |
|
public function respond($data, $headers = []) |
90
|
|
|
{ |
91
|
10 |
|
return Response::json($data, $this->getStatusCode(), $headers); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $message |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
3 |
|
public function respondWithError($message) |
100
|
|
|
{ |
101
|
3 |
|
return $this->respond([ |
102
|
|
|
'errors' => [ |
103
|
3 |
|
'title' => $message, |
104
|
3 |
|
'status' => $this->getStatusCode(), |
105
|
|
|
], |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.