DeviceController::poke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
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 10
    public function __construct(DeviceTransformer $transformer)
19
    {
20 10
        $this->middleware('auth.basic', ['except' => ['index', 'poke', 'show']]);
21
22 10
        $this->transformer = $transformer;
23 10
    }
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 5
    public function poke(StoreComputerRequest $request)
133
    {
134 5
        $device = Device::firstOrNew(['mac' => $request->mac]);
0 ignored issues
show
Documentation introduced by
The property mac does not exist on object<PiFinder\Http\Req...s\StoreComputerRequest>. Since you implemented __get, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
135
136 5
        $device->fill($request->all());
137
138 5
        $device->group = $request->get('group', null);
139 5
        $device->public = $request->get('public', 'auto');
140
141 5
        $device->touch();
142
143 5
        event(new ServerWasPoked(array_add($device, 'server_time', Carbon::now()->toDateTimeString())));
0 ignored issues
show
Documentation introduced by
array_add($device, 'serv...()->toDateTimeString()) is of type array, but the function expects a object<PiFinder\Device>.

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...
144
145 5
        return $this->respondPoked($this->transformer->transform($device), $device->id);
146
    }
147
}
148