RepositoryController::index()   B
last analyzed

Complexity

Conditions 10
Paths 24

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
nc 24
nop 1
dl 0
loc 37
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Repository;
6
use Illuminate\Http\Request;
7
8
class RepositoryController extends Controller
9
{
10
    /**
11
     * Display a listing of the resource.
12
     *
13
     * @return \Illuminate\Http\Response
14
     */
15
    public function index(Request $request)
16
    {
17
        $query = Repository::query()->with(['type', 'addr']);
18
19
        if ($request->has('searchTerm')) {
20
            $columnsToSearch = ['repo', 'name', 'addr_id', 'rin', 'email', 'phon', 'fax', 'www', 'description', 'type_id', 'is_active'];
21
            $search_term = json_decode($request->searchTerm)->searchTerm;
22
            if (! empty($search_term)) {
23
                $searchQuery = '%'.$search_term.'%';
24
                foreach ($columnsToSearch as $column) {
25
                    $query->orWhere($column, 'LIKE', $searchQuery);
26
                }
27
            }
28
        }
29
30
        if ($request->has('columnFilters')) {
31
            $filters = get_object_vars(json_decode($request->columnFilters));
32
            $relationship_column = ['addr.adr1', 'type.name'];
33
            foreach ($filters as $key => $value) {
34
                if (! in_array($key, $relationship_column)) {
35
                    if (! empty($value)) {
36
                        $query->orWhere($key, 'like', '%'.$value.'%');
37
                    }
38
                }
39
            }
40
        }
41
42
        if ($request->has('sort.0')) {
43
            $sort = json_decode($request->sort[0]);
44
            $query->orderBy($sort->field, $sort->type);
45
        }
46
47
        if ($request->has('perPage')) {
48
            $rows = $query->paginate($request->perPage);
49
        }
50
51
        return $rows;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rows does not seem to be defined for all execution paths leading up to this point.
Loading history...
52
    }
53
54
    /**
55
     * Show the form for creating a new resource.
56
     *
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function create()
60
    {
61
        //
62
    }
63
64
    /**
65
     * Store a newly created resource in storage.
66
     *
67
     * @param  \Illuminate\Http\Request  $request
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function store(Request $request)
71
    {
72
        $request->validate([
73
            'repo' => 'required',
74
            'addr_id' => 'required',
75
            'phon' => 'required',
76
            'email' => 'required',
77
            'name' => 'required',
78
            'type_id' => 'required',
79
            'is_active' => 'required',
80
        ]);
81
82
        return Repository::create([
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Reposi...$request->description)) also could return the type App\Models\Repository which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
83
            'repo' => $request->repo,
84
            'addr_id' => $request->addr_id,
85
            'rin' => $request->rin,
86
            'phon' => $request->phon,
87
            'email' => $request->email,
88
            'fax' => $request->fax,
89
            'www' => $request->www,
90
            'name' => $request->name,
91
            'is_active' => $request->is_active,
92
            'description' => $request->description,
93
        ]);
94
    }
95
96
    /**
97
     * Display the specified resource.
98
     *
99
     * @param  int  $id
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function show($id)
103
    {
104
        return Repository::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Repository::find($id) also could return the type App\Models\Repository which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
105
    }
106
107
    /**
108
     * Show the form for editing the specified resource.
109
     *
110
     * @param  int  $id
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function edit($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

113
    public function edit(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
        //
116
    }
117
118
    /**
119
     * Update the specified resource in storage.
120
     *
121
     * @param  \Illuminate\Http\Request  $request
122
     * @param  int  $id
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function update(Request $request, $id)
126
    {
127
        $request->validate([
128
            'repo' => 'required',
129
            'addr_id' => 'required',
130
            'phon' => 'required',
131
            'email' => 'required',
132
            'name' => 'required',
133
            'type_id' => 'required',
134
            'is_active' => 'required',
135
        ]);
136
137
        $repository = Repository::find($id);
138
        $repository->repo = $request->repo;
139
        $repository->addr_id = $request->addr_id;
140
        $repository->rin = $request->rin;
141
        $repository->phon = $request->phon;
142
        $repository->email = $request->email;
143
        $repository->fax = $request->fax;
144
        $repository->www = $request->www;
145
        $repository->name = $request->name;
146
        $repository->is_active = $request->is_active;
147
        $repository->description = $request->description;
148
        $repository->save();
149
150
        return $repository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repository also could return the type App\Models\Repository which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
151
    }
152
153
    /**
154
     * Remove the specified resource from storage.
155
     *
156
     * @param  int  $id
157
     * @return \Illuminate\Http\Response
158
     */
159
    public function destroy($id)
160
    {
161
        $repository = Repository::find($id);
162
        if ($repository) {
163
            $repository->delete();
164
165
            return 'true';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'true' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
166
        }
167
168
        return 'false';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'false' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
169
    }
170
171
    public function get()
172
    {
173
        $repo_data = Repository::all();
174
175
        return $repo_data;
176
    }
177
}
178