|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiFinder\Http\Controllers\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use PiFinder\Device; |
|
7
|
|
|
use PiFinder\Events\ServerWasPoked; |
|
8
|
|
|
use PiFinder\Transformers\DeviceTransformer; |
|
9
|
|
|
use PiFinder\Http\Requests\StoreComputerRequest; |
|
10
|
|
|
|
|
11
|
|
|
class DeviceController extends ApiController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var DeviceTransformer |
|
15
|
|
|
*/ |
|
16
|
|
|
private $transformer; |
|
17
|
|
|
|
|
18
|
9 |
|
public function __construct(DeviceTransformer $transformer) |
|
19
|
|
|
{ |
|
20
|
9 |
|
$this->middleware('auth.basic', ['except' => ['index', 'poke', 'show']]); |
|
21
|
|
|
|
|
22
|
9 |
|
$this->transformer = $transformer; |
|
23
|
9 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Display a listing of the resource. |
|
27
|
|
|
* |
|
28
|
|
|
* @param null $group |
|
29
|
|
|
* |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function index($group = null) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$devices = $group ? Device::where('group', $group)->get() : Device::onHomePage()->get(); |
|
35
|
|
|
|
|
36
|
1 |
|
return $this->respond([ |
|
37
|
1 |
|
'data' => $this->transformer->transformCollection($devices->all()), |
|
38
|
1 |
|
'server_time' => Carbon::now()->toIso8601String(), |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Store a newly created resource in storage. |
|
44
|
|
|
* |
|
45
|
|
|
* @param StoreComputerRequest $request |
|
46
|
|
|
* |
|
47
|
|
|
* @return Response |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function store(StoreComputerRequest $request) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$device = Device::create($request->all()); |
|
52
|
|
|
|
|
53
|
1 |
|
$device->public = $request->get('public', 'auto'); |
|
54
|
|
|
|
|
55
|
1 |
|
return $this->respondCreated($this->transformer->transform($device), $device->id); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Display the specified resource. |
|
60
|
|
|
* |
|
61
|
|
|
* @param int $id |
|
62
|
|
|
* |
|
63
|
|
|
* @return Response |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function show($id) |
|
66
|
|
|
{ |
|
67
|
1 |
|
$device = Device::find($id); |
|
68
|
|
|
|
|
69
|
1 |
|
if (! $device) { |
|
70
|
1 |
|
return $this->respondNotFound('Did not find the device you are looking for!'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return $this->respond([ |
|
74
|
1 |
|
'data' => $this->transformer->transform($device), |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Update the specified resource in storage. |
|
80
|
|
|
* |
|
81
|
|
|
* @param StoreComputerRequest $request |
|
82
|
|
|
* @param int $id |
|
83
|
|
|
* |
|
84
|
|
|
* @return Response |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function update(StoreComputerRequest $request, $id) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$device = Device::find($id); |
|
89
|
|
|
|
|
90
|
1 |
|
if (! $device) { |
|
91
|
1 |
|
return $this->respondNotFound('Did not find the device you are looking for!'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$device = $device->fill($request->all()); |
|
95
|
|
|
|
|
96
|
1 |
|
$device->save(); |
|
97
|
|
|
|
|
98
|
1 |
|
return $this->respond([ |
|
99
|
1 |
|
'data' => $this->transformer->transform($device), |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Remove the specified resource from storage. |
|
105
|
|
|
* |
|
106
|
|
|
* @param int $id |
|
107
|
|
|
* |
|
108
|
|
|
* @return Response |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function destroy($id) |
|
111
|
|
|
{ |
|
112
|
1 |
|
$device = Device::find($id); |
|
113
|
|
|
|
|
114
|
1 |
|
if (! $device) { |
|
115
|
1 |
|
return $this->respondNotFound('Did not find the device you are looking for!'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
$device->delete(); |
|
119
|
|
|
|
|
120
|
1 |
|
return $this->respondNoContent(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Handle device pokes. |
|
125
|
|
|
* |
|
126
|
|
|
* @param StoreComputerRequest $request |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \Exception |
|
129
|
|
|
* |
|
130
|
|
|
* @return Response |
|
131
|
|
|
*/ |
|
132
|
4 |
|
public function poke(StoreComputerRequest $request) |
|
133
|
|
|
{ |
|
134
|
4 |
|
$device = Device::firstOrNew(['mac' => $request->mac]); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
4 |
|
$device->fill($request->all()); |
|
137
|
|
|
|
|
138
|
4 |
|
$device->group = $request->get('group', null); |
|
139
|
4 |
|
$device->public = $request->get('public', 'auto'); |
|
140
|
|
|
|
|
141
|
4 |
|
$device->touch(); |
|
142
|
|
|
|
|
143
|
4 |
|
event(new ServerWasPoked(array_add($device, 'server_time', Carbon::now()->toDateTimeString()))); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
4 |
|
return $this->respondPoked($this->transformer->transform($device), $device->id); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.