Passed
Push — master ( 7bc374...cf39f3 )
by Arthur
05:05
created

MachineController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Modules\Machine\Http\Controllers;
4
5
use Foundation\Abstracts\Controller\Controller;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Modules\Machine\Contracts\MachineServiceContract;
9
use Modules\Machine\Entities\Machine;
10
use Modules\Machine\Resources\MachineResource;
11
use Modules\Machine\Services\MachineService;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14
class MachineController extends Controller
15
{
16
    /**
17
     * @var MachineService
18
     */
19
    protected $service;
20
21
    /**
22
     * MachineController constructor.
23
     *
24
     * @param $service
25
     */
26 1
    public function __construct(MachineServiceContract $service)
27
    {
28 1
        $this->service = $service;
0 ignored issues
show
Documentation Bug introduced by
$service is of type Modules\Machine\Contracts\MachineServiceContract, but the property $service was declared to be of type Modules\Machine\Services\MachineService. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
29 1
    }
30
31
    /**
32
     * Display a listing of the resource.
33
     */
34
    public function index()
35
    {
36
        $machines = get_authenticated_user()->machines;
0 ignored issues
show
Bug introduced by
Accessing machines on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
37
38
        return MachineResource::collection($machines);
39
    }
40
41
    /**
42
     * Show the form for creating a new resource.
43
     *
44
     * @return JsonResponse
45
     */
46
    public function store(Request $request)
47
    {
48
        $this->authorize('create');
49
50
        return \response()->json($this->service->create($request->toArray()));
51
    }
52
53
    /**
54
     * Store a newly created resource in storage.
55
     *
56
     * @param Request $request
57
     *
58
     * @return JsonResponse
59
     */
60
    public function update(Request $request, $id)
61
    {
62
        $machine = $this->service->find($id);
63
        $this->authorize('update', $machine);
64
        $machine = $this->service->update($id, $request->toArray());
65
66
        return \response()->json($machine);
67
    }
68
69
    /**
70
     * Show the specified resource.
71
     *
72
     * @return MachineResource
73
     */
74 1
    public function show($id)
75
    {
76 1
        $machine = Machine::find($id);
77
78 1
        if ($machine === null) {
79
            throw new NotFoundHttpException();
80
        }
81
82 1
        $this->authorize('access', $machine);
83
84 1
        return new MachineResource($machine);
85
    }
86
87
    /**
88
     * Remove the specified resource from storage.
89
     *
90
     * @return JsonResponse
91
     */
92
    public function destroy($id)
93
    {
94
        $machine = Machine::find($id);
95
96
        if ($machine === null) {
97
            throw new NotFoundHttpException();
98
        }
99
100
        $this->authorize('delete', $machine);
101
102
        return \response()->json(['deleted']);
103
    }
104
}
105