| Conditions | 10 |
| Paths | 10 |
| Total Lines | 70 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function recordVisit(): bool |
||
| 61 | { |
||
| 62 | $config = config('redbox-tracker'); |
||
| 63 | $user = request()->user(); |
||
| 64 | $routeName = request()->route()->getName(); |
||
| 65 | $methodName = request()->getMethod(); |
||
| 66 | |||
| 67 | // session()->forget('visitor'); return true; |
||
| 68 | /** |
||
| 69 | * Check if we should skip the current round. |
||
| 70 | */ |
||
| 71 | if (in_array($routeName, $config['skip_routes'])) { |
||
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * If the request method is not in the allowed methods |
||
| 77 | * array return false. |
||
| 78 | */ |
||
| 79 | if (!in_array($methodName, $config['allowed_methods'])) { |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * If we are not allowed to tracked authenticated users return false. |
||
| 85 | */ |
||
| 86 | if ($config['track_authenticated_users'] == false && $user) { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * If we are not allowed to tracked authenticated users return false. |
||
| 92 | */ |
||
| 93 | if ($config['track_unauthenticated_users'] == false && !$user) { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | if (session()->has('visitor') === true) { |
||
| 98 | $visitor = session()->get('visitor'); |
||
| 99 | $visitor = Visitor::find($visitor['id']); |
||
| 100 | } else { |
||
| 101 | $visitor = new Visitor(); |
||
| 102 | } |
||
| 103 | |||
| 104 | $visitor->fill($this->collect()); |
||
| 105 | |||
| 106 | if ($visitor->exists === false) { |
||
| 107 | if ($config['events']['dispatch']) { |
||
| 108 | event(new NewVisitorNotification($visitor)); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | event(new NewVisitorNotification($visitor)); |
||
| 112 | $visitor->save(); |
||
| 113 | |||
| 114 | $visitorRequest = new VisitorRequest(); |
||
| 115 | $visitorRequest->visitor_id = $visitor['id']; |
||
| 116 | $visitorRequest->domain = request()->getHttpHost(); |
||
| 117 | $visitorRequest->method = $methodName; |
||
| 118 | $visitorRequest->route = $routeName; |
||
| 119 | $visitorRequest->referer = request()->headers->get('Referer'); |
||
| 120 | $visitorRequest->is_secure = request()->isSecure(); |
||
| 121 | $visitorRequest->is_ajax = \request()->ajax(); |
||
| 122 | $visitorRequest->path = request()->path() ?? ''; |
||
| 123 | |||
| 124 | $visitor->requests()->save($visitorRequest); |
||
| 125 | |||
| 126 | request()->merge(['visitor' => $visitor]); |
||
| 127 | session()->put('visitor', $visitor->toArray()); |
||
| 128 | |||
| 129 | return true; |
||
| 130 | } |
||
| 155 |