ForumCategoryController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\ForumCategory;
6
use Illuminate\Http\Request;
7
8
class ForumCategoryController 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 = ForumCategory::query();
18
19
        if ($request->has('searchTerm')) {
20
            $columnsToSearch = ['name', 'email', 'phone'];
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
33
            foreach ($filters as $key => $value) {
34
                if (! empty($value)) {
35
                    $query->orWhere($key, 'like', '%'.$value.'%');
36
                }
37
            }
38
        }
39
40
        if ($request->has('sort.0')) {
41
            $sort = json_decode($request->sort[0]);
42
            $query->orderBy($sort->field, $sort->type);
43
        }
44
45
        if ($request->has('perPage')) {
46
            $rows = $query->paginate($request->perPage);
47
        } else {
48
            $rows = $query->get();
49
        }
50
51
        return $rows;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rows returns the type Illuminate\Database\Eloq...on\LengthAwarePaginator which is incompatible with the documented return type Illuminate\Http\Response.
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
            'name' => 'required',
74
        ]);
75
76
        return ForumCategory::create([
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ForumC...me' => $request->name)) also could return the type App\Models\ForumCategory which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
77
            'name' => $request->name,
78
        ]);
79
    }
80
81
    /**
82
     * Display the specified resource.
83
     *
84
     * @param  \App\Models\ForumCategory  $forumCategory
85
     * @return \Illuminate\Http\Response
86
     */
87
    public function show($id)
88
    {
89
        return ForumCategory::where('slug', $id)->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ForumC...e('slug', $id)->first() also could return the type App\Models\ForumCategory which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
90
    }
91
92
    /**
93
     * Show the form for editing the specified resource.
94
     *
95
     * @param  int  $id
96
     * @return \Illuminate\Http\Response
97
     */
98
    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

98
    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...
99
    {
100
        //
101
    }
102
103
    /**
104
     * Update the specified resource in storage.
105
     *
106
     * @param  \Illuminate\Http\Request  $request
107
     * @param  integrer  $id
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\integrer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
108
     * @return \Illuminate\Http\Response
109
     */
110
    public function update(Request $request, $id)
111
    {
112
        $request->validate([
113
            'name' => 'required',
114
        ]);
115
        $category = ForumCategory::find($id);
116
        if ($category) {
117
            $category->name = $request->name;
118
            $category->save();
119
        }
120
    }
121
122
    /**
123
     * Remove the specified resource from storage.
124
     *
125
     * @param  int  $id
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function destroy($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

128
    public function destroy(/** @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...
129
    {
130
        //
131
    }
132
}
133