showClearedAccessLogEntry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 2
dl 0
loc 22
rs 9.7333
c 0
b 0
f 0
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;
18
    use DispatchesJobs;
19
    use IpAddressDetails;
20
    use UserAgentDetails;
21
    use ValidatesRequests;
22
    private $_rolesEnabled;
23
    private $_rolesMiddlware;
24
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        $this->middleware('auth');
33
34
        $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

34
        $this->_rolesEnabled = /** @scrutinizer ignore-call */ config('LaravelLogger.rolesEnabled');
Loading history...
35
        $this->_rolesMiddlware = config('LaravelLogger.rolesMiddlware');
36
37
        if ($this->_rolesEnabled) {
38
            $this->middleware($this->_rolesMiddlware);
39
        }
40
    }
41
42
    /**
43
     * Add additional details to a collections.
44
     *
45
     * @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...
46
     *
47
     * @return collection
48
     */
49
    private function mapAdditionalDetails($collectionItems)
50
    {
51
        $collectionItems->map(function ($collectionItem) {
52
            $eventTime = Carbon::parse($collectionItem->updated_at);
53
            $collectionItem['timePassed'] = $eventTime->diffForHumans();
54
            $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...
55
            $collectionItem['langDetails'] = UserAgentDetails::localeLang($collectionItem->locale);
56
            $collectionItem['userDetails'] = config('LaravelLogger.defaultUserModel')::find($collectionItem->userId);
0 ignored issues
show
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...
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

56
            $collectionItem['userDetails'] = /** @scrutinizer ignore-call */ config('LaravelLogger.defaultUserModel')::find($collectionItem->userId);
Loading history...
57
58
            return $collectionItem;
59
        });
60
61
        return $collectionItems;
62
    }
63
64
    /**
65
     * Show the activities log dashboard.
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69
    public function showAccessLog(Request $request)
70
    {
71
        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

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

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

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

146
        return /** @scrutinizer ignore-call */ View('LaravelLogger::logger.activity-log-item', $data);
Loading history...
147
    }
148
149
    /**
150
     * Remove the specified resource from storage.
151
     *
152
     * @param Request $request
153
     *
154
     * @return \Illuminate\Http\Response
155
     */
156
    public function clearActivityLog(Request $request)
157
    {
158
        $activities = Activity::all();
159
        foreach ($activities as $activity) {
160
            $activity->delete();
161
        }
162
163
        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

163
        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

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

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

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

207
        $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...
208
        $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...
209
        $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...
210
        $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...
211
        $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...
212
        $timePassed = $eventTime->diffForHumans();
213
214
        $data = [
215
            'activity'              => $activity,
216
            'userDetails'           => $userDetails,
217
            'ipAddressDetails'      => $ipAddressDetails,
218
            'timePassed'            => $timePassed,
219
            'userAgentDetails'      => $userAgentDetails,
220
            'langDetails'           => $langDetails,
221
            'isClearedEntry'        => true,
222
        ];
223
224
        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

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

238
            return /** @scrutinizer ignore-call */ abort(404);
Loading history...
239
        }
240
241
        return $activity[0];
242
    }
243
244
    /**
245
     * Destroy the specified resource from storage.
246
     *
247
     * @param Request $request
248
     *
249
     * @return \Illuminate\Http\Response
250
     */
251
    public function destroyActivityLog(Request $request)
252
    {
253
        $activities = Activity::onlyTrashed()->get();
254
        foreach ($activities as $activity) {
255
            $activity->forceDelete();
256
        }
257
258
        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 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

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

258
        return /** @scrutinizer ignore-call */ redirect('activity')->with('success', trans('LaravelLogger::laravel-logger.messages.logDestroyedSuccessfuly'));
Loading history...
259
    }
260
261
    /**
262
     * Restore the specified resource from soft deleted storage.
263
     *
264
     * @param Request $request
265
     *
266
     * @return \Illuminate\Http\Response
267
     */
268
    public function restoreClearedActivityLog(Request $request)
269
    {
270
        $activities = Activity::onlyTrashed()->get();
271
        foreach ($activities as $activity) {
272
            $activity->restore();
273
        }
274
275
        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 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

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

275
        return redirect('activity')->with('success', /** @scrutinizer ignore-call */ trans('LaravelLogger::laravel-logger.messages.logRestoredSuccessfuly'));
Loading history...
276
    }
277
278
    /**
279
     * Search the activity log according to specific criteria.
280
     *
281
     * @param query
282
     * @param request
283
     *
284
     * @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...
285
     */
286
    public function searchActivityLog($query, $requeset)
287
    {
288
        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

288
        if (in_array('description', explode(',', /** @scrutinizer ignore-call */ config('LaravelLogger.searchFields'))) && $requeset->get('description')) {
Loading history...
289
            $query->where('description', 'like', '%'.$requeset->get('description').'%');
290
        }
291
292
        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...
293
            $query->where('userId', '=', $requeset->get('user'));
294
        }
295
296
        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...
297
            $query->where('methodType', '=', $requeset->get('method'));
298
        }
299
300
        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 111 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...
301
            $query->where('route', 'like', '%'.$requeset->get('route').'%');
302
        }
303
304
        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...
305
            $query->where('ipAddress', 'like', '%'.$requeset->get('ip_address').'%');
306
        }
307
308
        return $query;
309
    }
310
}
311