Completed
Push — master ( 6e8adf...c24137 )
by
unknown
27s
created

LocationController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
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
        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...
29
    }
30
31
    /**
32
     * Show the form for creating a new resource.
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function create()
37
    {
38
        $sites = Site::orderBy('name', 'ASC')->get();
39
        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...
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param  EditLocation $request
46
     * @return \Illuminate\Http\RedirectResponse
47
     */
48 View Code Duplication
    public function store(EditLocation $request)
49
    {
50
        //Get the site id of the old or newly created site
51
        if (!empty($request->input('new_site_name')))
52
        {
53
            //Create a new site
54
            $site = Site::create([ 'name' => $request->input('new_site_name') ]);
55
            $site_id = $site->id;
56
        } else {
57
                    $site_id = $request->input('site');
58
        }
59
    
60
        $location = Location::create([ 'name' => $request->input('name'), 'site_id' => $site_id ]);
61
    
62
        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

62
        return redirect()->route('location.show', /** @scrutinizer ignore-type */ $location->id)
Loading history...
63
            ->with('success', 'Location created successfully');
64
    }
65
66
    /**
67
     * Display the specified resource.
68
     *
69
     * @param  int  $id
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function show($id)
73
    {
74
        $location = Location::findOrFail($id);
75
        $devices = $location->devices()->orderBy('name', 'ASC')->paginate(15);
76
    
77
        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...
78
    }
79
80
    /**
81
     * Show the form for editing the specified resource.
82
     *
83
     * @param  int  $id
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function edit($id)
87
    {
88
        $location = Location::findOrFail($id);
89
        $sites = Site::orderByRaw("id = ? DESC", $location->site_id)->orderBy('name', 'ASC')->get();
90
    
91
        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...
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  EditLocation  $request
98
     * @param  int  $id
99
     * @return \Illuminate\Http\RedirectResponse
100
     */
101 View Code Duplication
    public function update(EditLocation $request, $id)
102
    {
103
        //Get the site id of the old or newly created site
104
        if (!empty($request->input('new_site_name')))
105
        {
106
            //Create a new site
107
            $site = Site::create([ 'name' => $request->input('new_site_name') ]);
108
            $site_id = $site->id;
109
        } else {
110
                    $site_id = $request->input('site');
111
        }
112
        
113
        //Update the location with the supplied name and the site
114
        Location::findOrFail($id)->update([ 'name' => $request->input('name'), 'site_id' => $site_id ]);
115
        
116
        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

116
        return redirect()->route('location.show', /** @scrutinizer ignore-type */ $id)
Loading history...
117
            ->with('success', 'Location updated successfully');
118
    }
119
120
    /**
121
     * Remove the specified resource from storage.
122
     *
123
     * @param  int  $id
124
     * @return \Illuminate\Http\RedirectResponse
125
     */
126
    public function destroy($id)
127
    {
128
        Location::findOrFail($id)->delete();
129
        return redirect()->route('location.index')
130
            ->with('success', 'Location deleted successfully');
131
    }
132
}
133