LaravelBlockerDeletedController::search()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace jeremykenedy\LaravelBlocker\App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use jeremykenedy\LaravelBlocker\App\Http\Requests\SearchBlockerRequest;
8
use jeremykenedy\LaravelBlocker\App\Models\BlockedItem;
9
10
class LaravelBlockerDeletedController extends LaravelBlockerController
11
{
12
    /**
13
     * Get Soft Deleted BlockedItem.
14
     *
15
     * @param int $id
16
     *
17
     * @return \Illuminate\Http\Response
18
     */
19
    public static function getDeletedBlockedItem($id)
20
    {
21
        $item = BlockedItem::onlyTrashed()->where('id', $id)->get();
22
        if (count($item) != 1) {
23
            return abort(redirect('blocker-deleted')
0 ignored issues
show
Bug introduced by
The function abort 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

23
            return /** @scrutinizer ignore-call */ abort(redirect('blocker-deleted')
Loading history...
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

23
            return abort(/** @scrutinizer ignore-call */ redirect('blocker-deleted')
Loading history...
24
                            ->with('error', trans('laravelblocker::laravelblocker.errors.errorBlockerNotFound')));
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

24
                            ->with('error', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.errors.errorBlockerNotFound')));
Loading history...
25
        }
26
27
        return $item[0];
28
    }
29
30
    /**
31
     * Show the laravel ip blocker deleted dashboard.
32
     *
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function index()
36
    {
37
        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

37
        if (/** @scrutinizer ignore-call */ config('laravelblocker.blockerPaginationEnabled')) {
Loading history...
38
            $blocked = BlockedItem::onlyTrashed()->paginate(config('laravelblocker.blockerPaginationPerPage'));
39
        } else {
40
            $blocked = BlockedItem::onlyTrashed()->get();
41
        }
42
43
        return view('laravelblocker::laravelblocker.deleted.index', compact('blocked'));
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

43
        return /** @scrutinizer ignore-call */ view('laravelblocker::laravelblocker.deleted.index', compact('blocked'));
Loading history...
44
    }
45
46
    /**
47
     * Display the specified resource.
48
     *
49
     * @param int $id
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function show($id)
54
    {
55
        $item = self::getDeletedBlockedItem($id);
56
        $typeDeleted = 'deleted';
57
58
        return view('laravelblocker::laravelblocker.show', compact('item', 'typeDeleted'));
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

58
        return /** @scrutinizer ignore-call */ view('laravelblocker::laravelblocker.show', compact('item', 'typeDeleted'));
Loading history...
59
    }
60
61
    /**
62
     * Update the specified resource in storage.
63
     *
64
     * @param \Illuminate\Http\Request $request
65
     * @param int                      $id
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69
    public function restoreBlockedItem(Request $request, $id)
70
    {
71
        $item = self::getDeletedBlockedItem($id);
72
        $item->restore();
73
74
        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

74
        return /** @scrutinizer ignore-call */ redirect('blocker')
Loading history...
75
                    ->with('success', trans('laravelblocker::laravelblocker.messages.successRestoredItem'));
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

75
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.successRestoredItem'));
Loading history...
76
    }
77
78
    /**
79
     * Restore all the specified resource from soft deleted storage.
80
     *
81
     * @param Request $request
82
     *
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function restoreAllBlockedItems(Request $request)
86
    {
87
        $items = BlockedItem::onlyTrashed()->get();
88
        foreach ($items as $item) {
89
            $item->restore();
90
        }
91
92
        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

92
        return /** @scrutinizer ignore-call */ redirect('blocker')
Loading history...
93
                    ->with('success', trans('laravelblocker::laravelblocker.messages.successRestoredAllItems'));
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

93
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.successRestoredAllItems'));
Loading history...
94
    }
95
96
    /**
97
     * Remove the specified resource from storage.
98
     *
99
     * @param int $id
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function destroy($id)
104
    {
105
        $item = self::getDeletedBlockedItem($id);
106
        $item->forceDelete();
107
108
        return redirect('blocker-deleted')
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

108
        return /** @scrutinizer ignore-call */ redirect('blocker-deleted')
Loading history...
109
                    ->with('success', trans('laravelblocker::laravelblocker.messages.successDestroyedItem'));
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

109
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.successDestroyedItem'));
Loading history...
110
    }
111
112
    /**
113
     * Destroy all the specified resource from storage.
114
     *
115
     * @param Request $request
116
     *
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function destroyAllItems(Request $request)
120
    {
121
        $items = BlockedItem::onlyTrashed()->get();
122
123
        foreach ($items as $item) {
124
            $item->forceDelete();
125
        }
126
127
        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

127
        return /** @scrutinizer ignore-call */ redirect('blocker')
Loading history...
128
                    ->with('success', trans('laravelblocker::laravelblocker.messages.successDestroyedAllItems'));
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

128
                    ->with('success', /** @scrutinizer ignore-call */ trans('laravelblocker::laravelblocker.messages.successDestroyedAllItems'));
Loading history...
129
    }
130
131
    /**
132
     * Method to search the deleted blocked items.
133
     *
134
     * @param SearchBlockerRequest $request
135
     *
136
     * @return \Illuminate\Http\Response
137
     */
138
    public function search(SearchBlockerRequest $request)
139
    {
140
        $searchTerm = $request->validated()['blocked_search_box'];
141
        $results = BlockedItem::onlyTrashed()->where('id', 'like', $searchTerm.'%')->onlyTrashed()
142
                        ->orWhere('typeId', 'like', $searchTerm.'%')->onlyTrashed()
143
                        ->orWhere('value', 'like', $searchTerm.'%')->onlyTrashed()
144
                        ->orWhere('note', 'like', $searchTerm.'%')->onlyTrashed()
145
                        ->orWhere('userId', 'like', $searchTerm.'%')->onlyTrashed()
146
                        ->get();
147
148
        $results->map(function ($item) {
149
            $item['type'] = $item->blockedType->slug;
150
151
            return $item;
152
        });
153
154
        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

154
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
155
            json_encode($results),
156
        ], Response::HTTP_OK);
157
    }
158
}
159