LaravelBlockerController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 163
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 7 1
A show() 0 5 1
A update() 0 9 1
A search() 0 19 1
A destroy() 0 7 1
A index() 0 11 2
A create() 0 6 1
A store() 0 6 1
A __construct() 0 12 3
1
<?php
2
3
namespace jeremykenedy\LaravelBlocker\App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller 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...
6
use Illuminate\Http\Response;
7
use jeremykenedy\LaravelBlocker\App\Http\Requests\SearchBlockerRequest;
8
use jeremykenedy\LaravelBlocker\App\Http\Requests\StoreBlockerRequest;
9
use jeremykenedy\LaravelBlocker\App\Http\Requests\UpdateBlockerRequest;
10
use jeremykenedy\LaravelBlocker\App\Models\BlockedItem;
11
use jeremykenedy\LaravelBlocker\App\Models\BlockedType;
12
13
class LaravelBlockerController extends Controller
14
{
15
    private $_authEnabled;
16
    private $_rolesEnabled;
17
    private $_rolesMiddlware;
18
19
    /**
20
     * Create a new controller instance.
21
     *
22
     * @return void
23
     */
24
    public function __construct()
25
    {
26
        $this->_authEnabled = config('laravelblocker.authEnabled');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

26
        $this->_authEnabled = /** @scrutinizer ignore-call */ config('laravelblocker.authEnabled');
Loading history...
27
        $this->_rolesEnabled = config('laravelblocker.rolesEnabled');
28
        $this->_rolesMiddlware = config('laravelblocker.rolesMiddlware');
29
30
        if ($this->_authEnabled) {
31
            $this->middleware('auth');
32
        }
33
34
        if ($this->_rolesEnabled) {
35
            $this->middleware($this->_rolesMiddlware);
36
        }
37
    }
38
39
    /**
40
     * Show the laravel ip email blocker dashboard.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function index()
45
    {
46
        if (config('laravelblocker.blockerPaginationEnabled')) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

46
        if (/** @scrutinizer ignore-call */ config('laravelblocker.blockerPaginationEnabled')) {
Loading history...
47
            $blocked = BlockedItem::paginate(config('laravelblocker.blockerPaginationPerPage'));
48
        } else {
49
            $blocked = BlockedItem::all();
50
        }
51
52
        $deletedBlockedItems = BlockedItem::onlyTrashed();
53
54
        return view('laravelblocker::laravelblocker.index', compact('blocked', 'deletedBlockedItems'));
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

54
        return /** @scrutinizer ignore-call */ view('laravelblocker::laravelblocker.index', compact('blocked', 'deletedBlockedItems'));
Loading history...
55
    }
56
57
    /**
58
     * Show the form for creating a new resource.
59
     *
60
     * @return \Illuminate\Http\Response
61
     */
62
    public function create()
63
    {
64
        $blockedTypes = BlockedType::all();
65
        $users = config('laravelblocker.defaultUserModel')::all();
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

65
        $users = /** @scrutinizer ignore-call */ config('laravelblocker.defaultUserModel')::all();
Loading history...
66
67
        return view('laravelblocker::laravelblocker.create', compact('blockedTypes', 'users'));
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

67
        return /** @scrutinizer ignore-call */ view('laravelblocker::laravelblocker.create', compact('blockedTypes', 'users'));
Loading history...
68
    }
69
70
    /**
71
     * Store a newly created resource in storage.
72
     *
73
     * @param StoreBlockerRequest $request
74
     *
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function store(StoreBlockerRequest $request)
78
    {
79
        BlockedItem::create($request->blockedFillData());
80
81
        return redirect('blocker')
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

81
        return /** @scrutinizer ignore-call */ redirect('blocker')
Loading history...
82
                    ->with('success', trans('laravelblocker::laravelblocker.messages.blocked-creation-success'));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

82
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.blocked-creation-success'));
Loading history...
83
    }
84
85
    /**
86
     * Display the specified resource.
87
     *
88
     * @param int $id
89
     *
90
     * @return \Illuminate\Http\Response
91
     */
92
    public function show($id)
93
    {
94
        $item = BlockedItem::findOrFail($id);
95
96
        return view('laravelblocker::laravelblocker.show', compact('item'));
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

96
        return /** @scrutinizer ignore-call */ view('laravelblocker::laravelblocker.show', compact('item'));
Loading history...
97
    }
98
99
    /**
100
     * Display the specified resource.
101
     *
102
     * @param int $id
103
     *
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function edit($id)
107
    {
108
        $blockedTypes = BlockedType::all();
109
        $users = config('laravelblocker.defaultUserModel')::all();
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

109
        $users = /** @scrutinizer ignore-call */ config('laravelblocker.defaultUserModel')::all();
Loading history...
110
        $item = BlockedItem::findOrFail($id);
111
112
        return view('laravelblocker::laravelblocker.edit', compact('blockedTypes', 'users', 'item'));
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

112
        return /** @scrutinizer ignore-call */ view('laravelblocker::laravelblocker.edit', compact('blockedTypes', 'users', 'item'));
Loading history...
113
    }
114
115
    /**
116
     * Update the specified resource in storage.
117
     *
118
     * @param UpdateBlockerRequest $request
119
     * @param int                  $id
120
     *
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function update(UpdateBlockerRequest $request, $id)
124
    {
125
        $item = BlockedItem::findOrFail($id);
126
        $item->fill($request->blockedFillData());
127
        $item->save();
128
129
        return redirect()
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

129
        return /** @scrutinizer ignore-call */ redirect()
Loading history...
130
                    ->back()
131
                    ->with('success', trans('laravelblocker::laravelblocker.messages.update-success'));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

131
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.update-success'));
Loading history...
132
    }
133
134
    /**
135
     * Remove the specified resource from storage.
136
     *
137
     * @param int $id
138
     *
139
     * @return \Illuminate\Http\Response
140
     */
141
    public function destroy($id)
142
    {
143
        $blockedItem = BlockedItem::findOrFail($id);
144
        $blockedItem->delete();
145
146
        return redirect('blocker')
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

146
        return /** @scrutinizer ignore-call */ redirect('blocker')
Loading history...
147
                    ->with('success', trans('laravelblocker::laravelblocker.messages.delete-success'));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

147
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.delete-success'));
Loading history...
148
    }
149
150
    /**
151
     * Method to search the blocked items.
152
     *
153
     * @param SearchBlockerRequest $request
154
     *
155
     * @return \Illuminate\Http\Response
156
     */
157
    public function search(SearchBlockerRequest $request)
158
    {
159
        $searchTerm = $request->validated()['blocked_search_box'];
160
        $results = BlockedItem::where('id', 'like', $searchTerm.'%')
161
                            ->orWhere('typeId', 'like', $searchTerm.'%')
162
                            ->orWhere('value', 'like', $searchTerm.'%')
163
                            ->orWhere('note', 'like', $searchTerm.'%')
164
                            ->orWhere('userId', 'like', $searchTerm.'%')
165
                            ->get();
166
167
        $results->map(function ($item) {
168
            $item['type'] = $item->blockedType->slug;
169
170
            return $item;
171
        });
172
173
        return response()->json([
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

173
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
174
            json_encode($results),
175
        ], Response::HTTP_OK);
176
    }
177
}
178