Brand_ModelController::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
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 6
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Brand_Model;
6
use Illuminate\Http\Request;
7
8 View Code Duplication
class Brand_ModelController 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
        $brand_models = Brand_model::paginate(5);
23
24
        return view('manteniments/brand_model/index', ['brand_models' => $brand_models]);
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/brand_model/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
        Brand_model::create([
48
         'name'        => $request['name'],
49
         'description' => $request['description'],
50
           ]);
51
52
        return redirect()->intended('mnt/brand_model');
53
    }
54
55
    /**
56
     * Display the specified resource.
57
     *
58
     * @param int $id
59
     *
60
     * @return \Illuminate\Http\Response
61
     */
62
    public function show($id)
63
    {
64
        //
65
    }
66
67
    /**
68
     * Show the form for editing the specified resource.
69
     *
70
     * @param int $id
71
     *
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function edit($id)
75
    {
76
        $brand_model = Brand_model::find($id);
77
      // Redirect to country list if updating country wasn't existed
78
      if ($brand_model == null || count($brand_model) == 0) {
79
          return redirect()->intended('/mnt/brand_model');
80
      }
81
82
        return view('manteniments/brand_model/edit', ['brand_model' => $brand_model]);
83
    }
84
85
    /**
86
     * Update the specified resource in storage.
87
     *
88
     * @param \Illuminate\Http\Request $request
89
     * @param int                      $id
90
     *
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function update(Request $request, $id)
94
    {
95
        $brand_model = Brand_model::findOrFail($id);
0 ignored issues
show
Unused Code introduced by
$brand_model 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...
96
        $input = [
97
        'name'        => $request['name'],
98
        'description' => $request['description'],
99
      ];
100
        $this->validate($request, [
101
      'name' => 'required|max:60',
102
      ]);
103
        Brand_model::where('id', $id)
104
          ->update($input);
105
106
        return redirect()->intended('mnt/brand_model');
107
    }
108
109
    /**
110
     * Remove the specified resource from storage.
111
     *
112
     * @param int $id
113
     *
114
     * @return \Illuminate\Http\Response
115
     */
116
    public function destroy($id)
117
    {
118
        Brand_model::where('id', $id)->delete();
119
120
        return redirect()->intended('mnt/brand_model');
121
    }
122
123
    public function search(Request $request)
124
    {
125
        $constraints = [
126
            'name' => $request['name'],
127
            ];
128
        $brand_models = $this->doSearchingQuery($constraints);
129
130
        return view('manteniments/brand_model/index', ['brand_models' => $brand_models, 'searchingVals' => $constraints]);
131
    }
132
133
    private function doSearchingQuery($constraints)
134
    {
135
        $query = brand_model::query();
136
        $fields = array_keys($constraints);
137
        $index = 0;
138
        foreach ($constraints as $constraint) {
139
            if ($constraint != null) {
140
                $query = $query->where($fields[$index], 'like', '%'.$constraint.'%');
141
            }
142
            $index++;
143
        }
144
145
        return $query->paginate(5);
146
    }
147
148
    private function validateInput($request)
149
    {
150
        $this->validate($request, [
151
        'name' => 'required|max:60|unique:provider',
152
    ]);
153
    }
154
}
155