Passed
Push — master ( 2d838a...6c2413 )
by Jeremy
27:11 queued 26:43
created

LaravelLoggerController::searchActivityLog()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 11
eloc 11
c 4
b 0
f 0
nc 32
nop 2
dl 0
loc 24
rs 7.3166

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 jeremykenedy\LaravelLogger\App\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Au...cess\AuthorizesRequests 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...
7
use Illuminate\Foundation\Bus\DispatchesJobs;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Bus\DispatchesJobs 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...
8
use Illuminate\Foundation\Validation\ValidatesRequests;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Validation\ValidatesRequests 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...
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\Controller as BaseController;
11
use jeremykenedy\LaravelLogger\App\Http\Traits\IpAddressDetails;
12
use jeremykenedy\LaravelLogger\App\Http\Traits\UserAgentDetails;
13
use jeremykenedy\LaravelLogger\App\Models\Activity;
14
15
class LaravelLoggerController extends BaseController
16
{
17
    use AuthorizesRequests, DispatchesJobs, IpAddressDetails, UserAgentDetails, ValidatesRequests;
18
19
    private $_rolesEnabled;
20
    private $_rolesMiddlware;
21
22
    /**
23
     * Create a new controller instance.
24
     *
25
     * @return void
26
     */
27
    public function __construct()
28
    {
29
        $this->middleware('auth');
30
31
        $this->_rolesEnabled = config('LaravelLogger.rolesEnabled');
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

31
        $this->_rolesEnabled = /** @scrutinizer ignore-call */ config('LaravelLogger.rolesEnabled');
Loading history...
32
        $this->_rolesMiddlware = config('LaravelLogger.rolesMiddlware');
33
34
        if ($this->_rolesEnabled) {
35
            $this->middleware($this->_rolesMiddlware);
36
        }
37
    }
38
39
    /**
40
     * Add additional details to a collections.
41
     *
42
     * @param collection $collectionItems
0 ignored issues
show
Bug introduced by
The type jeremykenedy\LaravelLogg...\Controllers\collection 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...
43
     *
44
     * @return collection
45
     */
46
    private function mapAdditionalDetails($collectionItems)
47
    {
48
        $collectionItems->map(function ($collectionItem) {
49
            $eventTime = Carbon::parse($collectionItem->updated_at);
50
            $collectionItem['timePassed'] = $eventTime->diffForHumans();
51
            $collectionItem['userAgentDetails'] = UserAgentDetails::details($collectionItem->useragent);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 104 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
52
            $collectionItem['langDetails'] = UserAgentDetails::localeLang($collectionItem->locale);
53
            $collectionItem['userDetails'] = config('LaravelLogger.defaultUserModel')::find($collectionItem->userId);
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

53
            $collectionItem['userDetails'] = /** @scrutinizer ignore-call */ config('LaravelLogger.defaultUserModel')::find($collectionItem->userId);
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 117 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
54
55
            return $collectionItem;
56
        });
57
58
        return $collectionItems;
59
    }
60
61
    /**
62
     * Show the activities log dashboard.
63
     *
64
     * @return \Illuminate\Http\Response
65
     */
66
    public function showAccessLog(Request $request)
67
    {
68
        if (config('LaravelLogger.loggerPaginationEnabled')) {
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

68
        if (/** @scrutinizer ignore-call */ config('LaravelLogger.loggerPaginationEnabled')) {
Loading history...
69
            $activities = Activity::orderBy('created_at', 'desc');
70
            if (config('LaravelLogger.enableSearch')) {
71
                $activities = $this->searchActivityLog($activities, $request);
72
            }
73
            $activities = $activities->paginate(config('LaravelLogger.loggerPaginationPerPage'));
74
            $totalActivities = $activities->total();
75
        } else {
76
            $activities = Activity::orderBy('created_at', 'desc');
77
78
            if (config('LaravelLogger.enableSearch')) {
79
                $activities = $this->searchActivityLog($activities, $request);
80
            }
81
            $activities = $activities->get();
82
            $totalActivities = $activities->count();
83
        }
84
85
        self::mapAdditionalDetails($activities);
86
87
        $users = config('LaravelLogger.defaultUserModel')::all();
88
89
        $data = [
90
            'activities'        => $activities,
91
            'totalActivities'   => $totalActivities,
92
            'users'             => $users,
93
        ];
94
95
        return View('LaravelLogger::logger.activity-log', $data);
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

95
        return /** @scrutinizer ignore-call */ View('LaravelLogger::logger.activity-log', $data);
Loading history...
96
    }
97
98
    /**
99
     * Show an individual activity log entry.
100
     *
101
     * @param Request $request
102
     * @param int     $id
103
     *
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function showAccessLogEntry(Request $request, $id)
107
    {
108
        $activity = Activity::findOrFail($id);
109
110
        $userDetails = config('LaravelLogger.defaultUserModel')::find($activity->userId);
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

110
        $userDetails = /** @scrutinizer ignore-call */ config('LaravelLogger.defaultUserModel')::find($activity->userId);
Loading history...
111
        $userAgentDetails = UserAgentDetails::details($activity->useragent);
112
        $ipAddressDetails = IpAddressDetails::checkIP($activity->ipAddress);
113
        $langDetails = UserAgentDetails::localeLang($activity->locale);
114
        $eventTime = Carbon::parse($activity->created_at);
115
        $timePassed = $eventTime->diffForHumans();
116
117
        if (config('LaravelLogger.loggerPaginationEnabled')) {
118
            $userActivities = Activity::where('userId', $activity->userId)
119
            ->orderBy('created_at', 'desc')
120
            ->paginate(config('LaravelLogger.loggerPaginationPerPage'));
121
            $totalUserActivities = $userActivities->total();
122
        } else {
123
            $userActivities = Activity::where('userId', $activity->userId)
124
            ->orderBy('created_at', 'desc')
125
            ->get();
126
            $totalUserActivities = $userActivities->count();
127
        }
128
129
        self::mapAdditionalDetails($userActivities);
130
131
        $data = [
132
            'activity'              => $activity,
133
            'userDetails'           => $userDetails,
134
            'ipAddressDetails'      => $ipAddressDetails,
135
            'timePassed'            => $timePassed,
136
            'userAgentDetails'      => $userAgentDetails,
137
            'langDetails'           => $langDetails,
138
            'userActivities'        => $userActivities,
139
            'totalUserActivities'   => $totalUserActivities,
140
            'isClearedEntry'        => false,
141
        ];
142
143
        return View('LaravelLogger::logger.activity-log-item', $data);
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

143
        return /** @scrutinizer ignore-call */ View('LaravelLogger::logger.activity-log-item', $data);
Loading history...
144
    }
145
146
    /**
147
     * Remove the specified resource from storage.
148
     *
149
     * @param Request $request
150
     *
151
     * @return \Illuminate\Http\Response
152
     */
153
    public function clearActivityLog(Request $request)
154
    {
155
        $activities = Activity::all();
156
        foreach ($activities as $activity) {
157
            $activity->delete();
158
        }
159
160
        return redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logClearedSuccessfuly'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

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

160
        return /** @scrutinizer ignore-call */ redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logClearedSuccessfuly'));
Loading history...
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

160
        return redirect('activity')->with('success', /** @scrutinizer ignore-call */ trans('LaravelLogger::laravel-logger.messages.logClearedSuccessfuly'));
Loading history...
161
    }
162
163
    /**
164
     * Show the cleared activity log - softdeleted records.
165
     *
166
     * @return \Illuminate\Http\Response
167
     */
168
    public function showClearedActivityLog()
169
    {
170
        if (config('LaravelLogger.loggerPaginationEnabled')) {
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

170
        if (/** @scrutinizer ignore-call */ config('LaravelLogger.loggerPaginationEnabled')) {
Loading history...
171
            $activities = Activity::onlyTrashed()
172
            ->orderBy('created_at', 'desc')
173
            ->paginate(config('LaravelLogger.loggerPaginationPerPage'));
174
            $totalActivities = $activities->total();
175
        } else {
176
            $activities = Activity::onlyTrashed()
177
            ->orderBy('created_at', 'desc')
178
            ->get();
179
            $totalActivities = $activities->count();
180
        }
181
182
        self::mapAdditionalDetails($activities);
183
184
        $data = [
185
            'activities'        => $activities,
186
            'totalActivities'   => $totalActivities,
187
        ];
188
189
        return View('LaravelLogger::logger.activity-log-cleared', $data);
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

189
        return /** @scrutinizer ignore-call */ View('LaravelLogger::logger.activity-log-cleared', $data);
Loading history...
190
    }
191
192
    /**
193
     * Show an individual cleared (soft deleted) activity log entry.
194
     *
195
     * @param Request $request
196
     * @param int     $id
197
     *
198
     * @return \Illuminate\Http\Response
199
     */
200
    public function showClearedAccessLogEntry(Request $request, $id)
201
    {
202
        $activity = self::getClearedActvity($id);
203
204
        $userDetails = config('LaravelLogger.defaultUserModel')::find($activity->userId);
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

204
        $userDetails = /** @scrutinizer ignore-call */ config('LaravelLogger.defaultUserModel')::find($activity->userId);
Loading history...
Bug introduced by
The property userId does not seem to exist on Illuminate\Http\Response.
Loading history...
205
        $userAgentDetails = UserAgentDetails::details($activity->useragent);
0 ignored issues
show
Bug introduced by
The property useragent does not seem to exist on Illuminate\Http\Response.
Loading history...
206
        $ipAddressDetails = IpAddressDetails::checkIP($activity->ipAddress);
0 ignored issues
show
Bug introduced by
The property ipAddress does not seem to exist on Illuminate\Http\Response.
Loading history...
207
        $langDetails = UserAgentDetails::localeLang($activity->locale);
0 ignored issues
show
Bug introduced by
The property locale does not seem to exist on Illuminate\Http\Response.
Loading history...
208
        $eventTime = Carbon::parse($activity->created_at);
0 ignored issues
show
Bug introduced by
The property created_at does not seem to exist on Illuminate\Http\Response.
Loading history...
209
        $timePassed = $eventTime->diffForHumans();
210
211
        $data = [
212
            'activity'              => $activity,
213
            'userDetails'           => $userDetails,
214
            'ipAddressDetails'      => $ipAddressDetails,
215
            'timePassed'            => $timePassed,
216
            'userAgentDetails'      => $userAgentDetails,
217
            'langDetails'           => $langDetails,
218
            'isClearedEntry'        => true,
219
        ];
220
221
        return View('LaravelLogger::logger.activity-log-item', $data);
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

221
        return /** @scrutinizer ignore-call */ View('LaravelLogger::logger.activity-log-item', $data);
Loading history...
222
    }
223
224
    /**
225
     * Get Cleared (Soft Deleted) Activity - Helper Method.
226
     *
227
     * @param int $id
228
     *
229
     * @return \Illuminate\Http\Response
230
     */
231
    private static function getClearedActvity($id)
232
    {
233
        $activity = Activity::onlyTrashed()->where('id', $id)->get();
234
        if (count($activity) != 1) {
235
            return abort(404);
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

235
            return /** @scrutinizer ignore-call */ abort(404);
Loading history...
236
        }
237
238
        return $activity[0];
239
    }
240
241
    /**
242
     * Destroy the specified resource from storage.
243
     *
244
     * @param Request $request
245
     *
246
     * @return \Illuminate\Http\Response
247
     */
248
    public function destroyActivityLog(Request $request)
249
    {
250
        $activities = Activity::onlyTrashed()->get();
251
        foreach ($activities as $activity) {
252
            $activity->forceDelete();
253
        }
254
255
        return redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logDestroyedSuccessfuly'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

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

255
        return /** @scrutinizer ignore-call */ redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logDestroyedSuccessfuly'));
Loading history...
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

255
        return redirect('activity')->with('success', /** @scrutinizer ignore-call */ trans('LaravelLogger::laravel-logger.messages.logDestroyedSuccessfuly'));
Loading history...
256
    }
257
258
    /**
259
     * Restore the specified resource from soft deleted storage.
260
     *
261
     * @param Request $request
262
     *
263
     * @return \Illuminate\Http\Response
264
     */
265
    public function restoreClearedActivityLog(Request $request)
266
    {
267
        $activities = Activity::onlyTrashed()->get();
268
        foreach ($activities as $activity) {
269
            $activity->restore();
270
        }
271
272
        return redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logRestoredSuccessfuly'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
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

272
        return redirect('activity')->with('success', /** @scrutinizer ignore-call */ trans('LaravelLogger::laravel-logger.messages.logRestoredSuccessfuly'));
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

272
        return /** @scrutinizer ignore-call */ redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logRestoredSuccessfuly'));
Loading history...
273
    }
274
275
    /**
276
     * Search the activity log according to specific criteria
277
     *
278
     * @param query
279
     * @param request
280
     *
281
     * @return filtered query
0 ignored issues
show
Bug introduced by
The type jeremykenedy\LaravelLogg...tp\Controllers\filtered 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...
282
     */
283
    public function searchActivityLog($query, $requeset)
284
    {
285
        if (in_array('description', explode(',', config('LaravelLogger.searchFields'))) && $requeset->get('description')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
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

285
        if (in_array('description', explode(',', /** @scrutinizer ignore-call */ config('LaravelLogger.searchFields'))) && $requeset->get('description')) {
Loading history...
286
            $query->where('description','like', '%'.$requeset->get('description').'%');
287
        }
288
289
290
        if (in_array('user', explode(',', config('LaravelLogger.searchFields'))) && $requeset->get('user')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 109 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
291
            $query->where('userId', '=', $requeset->get('user'));
292
        }
293
294
        if (in_array('method', explode(',', config('LaravelLogger.searchFields'))) && $requeset->get('method')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
295
            $query->where('methodType', '=', $requeset->get('method'));
296
        }
297
298
        if (in_array('route', explode(',', config('LaravelLogger.searchFields')))&& $requeset->get('route')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 110 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
299
            $query->where('route', 'like', '%'.$requeset->get('route').'%');
300
        }
301
302
        if (in_array('ip', explode(',', config('LaravelLogger.searchFields'))) && $requeset->get('ip_address')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
303
            $query->where('ipAddress', 'like', '%'.$requeset->get('ip_address').'%');
304
        }
305
306
        return $query;
307
    }
308
}
309