LocationController::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 19
loc 19
ccs 0
cts 9
cp 0
crap 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\DataTables\LocationDataTable;
6
use App\Location;
7
use App\Site;
8
use App\Http\Requests\EditLocation;
9
10
class LocationController extends Controller
11
{
12
    /**
13
     * Create a new controller instance.
14
     */
15
    public function __construct()
16
    {
17
        $this->middleware('auth');
18
    }
19
    
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @param  LocationDataTable   $dataTable
24
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
25
     */
26
    public function index(LocationDataTable $dataTable)
27
    {
28
        $this->authorize('index', Location::class);
29
        
30
        return $dataTable->render('location.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dataTable->render('location.index') also could return the type callable which is incompatible with the documented return type Illuminate\Http\JsonResponse|Illuminate\View\View.
Loading history...
31
    }
32
33
    /**
34
     * Show the form for creating a new resource.
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function create()
39
    {
40
        $this->authorize('create', Location::class);
41
        
42
        $sites = Site::orderBy('name', 'ASC')->get();
43
        return view('location.create', [ 'sites' => $sites ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('location.cr...ray('sites' => $sites)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param  EditLocation $request
50
     * @return \Illuminate\Http\RedirectResponse
51
     */
52 View Code Duplication
    public function store(EditLocation $request)
53
    {
54
        $this->authorize('store', Location::class);
55
        
56
        //Get the site id of the old or newly created site
57
        if (!empty($request->input('new_site_name')))
58
        {
59
            //Create a new site
60
            $site = Site::create([ 'name' => $request->input('new_site_name') ]);
61
            $site_id = $site->id;
62
        } else {
63
                    $site_id = $request->input('site');
64
        }
65
    
66
        $location = Location::create([ 'name' => $request->input('name'), 'site_id' => $site_id ]);
67
    
68
        return redirect()->route('location.show', $location->id)
0 ignored issues
show
Bug introduced by
$location->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        return redirect()->route('location.show', /** @scrutinizer ignore-type */ $location->id)
Loading history...
69
            ->with('success', 'Location created successfully');
70
    }
71
72
    /**
73
     * Display the specified resource.
74
     *
75
     * @param  int  $id
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function show($id)
79
    {
80
        $this->authorize('show', Location::class);
81
        
82
        $location = Location::findOrFail($id);
83
        $devices = $location->devices()->orderBy('name', 'ASC')->paginate(15);
84
    
85
        return view('location.show', [ 'location' => $location, 'devices' => $devices ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('location.sh...'devices' => $devices)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
86
    }
87
88
    /**
89
     * Show the form for editing the specified resource.
90
     *
91
     * @param  int  $id
92
     * @return \Illuminate\Http\Response
93
     */
94
    public function edit($id)
95
    {
96
        $this->authorize('edit', Location::class);
97
        
98
        $location = Location::findOrFail($id);
99
        $sites = Site::orderByRaw("id = ? DESC", $location->site_id)->orderBy('name', 'ASC')->get();
100
    
101
        return view('location.edit', [ 'location' => $location, 'sites' => $sites ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('location.ed...on, 'sites' => $sites)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
    }
103
104
    /**
105
     * Update the specified resource in storage.
106
     *
107
     * @param  EditLocation  $request
108
     * @param  int  $id
109
     * @return \Illuminate\Http\RedirectResponse
110
     */
111 View Code Duplication
    public function update(EditLocation $request, $id)
112
    {
113
        $this->authorize('update', Location::class);
114
        
115
        //Get the site id of the old or newly created site
116
        if (!empty($request->input('new_site_name')))
117
        {
118
            //Create a new site
119
            $site = Site::create([ 'name' => $request->input('new_site_name') ]);
120
            $site_id = $site->id;
121
        } else {
122
                    $site_id = $request->input('site');
123
        }
124
        
125
        //Update the location with the supplied name and the site
126
        Location::findOrFail($id)->update([ 'name' => $request->input('name'), 'site_id' => $site_id ]);
127
        
128
        return redirect()->route('location.show', $id)
0 ignored issues
show
Bug introduced by
$id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        return redirect()->route('location.show', /** @scrutinizer ignore-type */ $id)
Loading history...
129
            ->with('success', 'Location updated successfully');
130
    }
131
132
    /**
133
     * Remove the specified resource from storage.
134
     *
135
     * @param  int  $id
136
     * @return \Illuminate\Http\RedirectResponse
137
     */
138
    public function destroy($id)
139
    {
140
        $this->authorize('destroy', Location::class);
141
        
142
        Location::findOrFail($id)->delete();
143
        return redirect()->route('location.index')
144
            ->with('success', 'Location deleted successfully');
145
    }
146
}
147