LocationController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Location;
6
use Illuminate\Http\Request;
7
8 View Code Duplication
class LocationController extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    /**
11
      * Display a listing of the resource.
12
      *
13
      * @return \Illuminate\Http\Response
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
14
      */
15
     public function __construct()
16
     {
17
         $this->middleware('auth');
18
     }
19
20
    public function index()
21
    {
22
        $locations = Location::paginate(5);
23
24
        return view('manteniments/location/index', ['locations' => $locations]);
25
    }
26
27
    /**
28
     * Show the form for creating a new resource.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function create()
33
    {
34
        return view('manteniments/location/create');
35
    }
36
37
    /**
38
     * Store a newly created resource in storage.
39
     *
40
     * @param \Illuminate\Http\Request $request
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function store(Request $request)
45
    {
46
        $this->validateInput($request);
47
        Location::create([
48
         'name'          => $request['name'],
49
         'shortName'     => $request['shortName'],
50
         'description'   => $request['description'],
51
         'date_entrance' => $request['date_entrance'],
52
         'last_update'   => $request['last_update'],
53
           ]);
54
55
        return redirect()->intended('mnt/location');
56
    }
57
58
    /**
59
     * Display the specified resource.
60
     *
61
     * @param int $id
62
     *
63
     * @return \Illuminate\Http\Response
64
     */
65
    public function show($id)
66
    {
67
        //
68
    }
69
70
    /**
71
     * Show the form for editing the specified resource.
72
     *
73
     * @param int $id
74
     *
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function edit($id)
78
    {
79
        $location = Location::find($id);
80
      // Redirect to country list if updating country wasn't existed
81
      if ($location == null || count($location) == 0) {
82
          return redirect()->intended('/mnt/location');
83
      }
84
85
        return view('manteniments/location/edit', ['location' => $location]);
86
    }
87
88
    /**
89
     * Update the specified resource in storage.
90
     *
91
     * @param \Illuminate\Http\Request $request
92
     * @param int                      $id
93
     *
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function update(Request $request, $id)
97
    {
98
        $location = Location::findOrFail($id);
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
        $input = [
100
        'name'          => $request['name'],
101
        'shortName'     => $request['shortName'],
102
        'description'   => $request['description'],
103
        'date_entrance' => $request['date_entrance'],
104
        'last_update'   => $request['last_update'],
105
      ];
106
        $this->validate($request, [
107
      'name' => 'required|max:60',
108
      ]);
109
        Location::where('id', $id)
110
          ->update($input);
111
112
        return redirect()->intended('mnt/location');
113
    }
114
115
    /**
116
     * Remove the specified resource from storage.
117
     *
118
     * @param int $id
119
     *
120
     * @return \Illuminate\Http\Response
121
     */
122
    public function destroy($id)
123
    {
124
        Location::where('id', $id)->delete();
125
126
        return redirect()->intended('mnt/location');
127
    }
128
129
    public function search(Request $request)
130
    {
131
        $constraints = [
132
            'name'      => $request['name'],
133
            'shortName' => $request['shortName'],
134
            ];
135
        $locations = $this->doSearchingQuery($constraints);
136
137
        return view('manteniments/location/index', ['locations' => $locations, 'searchingVals' => $constraints]);
138
    }
139
140
    private function doSearchingQuery($constraints)
141
    {
142
        $query = location::query();
143
        $fields = array_keys($constraints);
144
        $index = 0;
145
        foreach ($constraints as $constraint) {
146
            if ($constraint != null) {
147
                $query = $query->where($fields[$index], 'like', '%'.$constraint.'%');
148
            }
149
            $index++;
150
        }
151
152
        return $query->paginate(5);
153
    }
154
155
    private function validateInput($request)
156
    {
157
        $this->validate($request, [
158
        'name'      => 'required|max:60|unique:provider',
159
        'shortName' => 'required|max:6|unique:provider',
160
    ]);
161
    }
162
}
163