Passed
Push — master ( 8093a0...d5968b )
by Johnny
01:33
created

Tracker::recordVisit()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 70
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 70
rs 7.6666
cc 10
nc 10
nop 0

How to fix   Long Method    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
/**
4
 * Tracker.php
5
 *
6
 * This Facade is doing the actual tracking for the package. The tracker is
7
 * instantiated by the TrackingMiddleware. Based on the rules defined in the
8
 * tracker.php configuration file it will track (or not track) visitors on your
9
 * website.
10
 *
11
 * This project is heavily inspired by Laravel Visitor Tracker and this file is
12
 * where most of the source is almost identical to that project.
13
 *
14
 * [Laravel Visitor Tracker](https://github.com/voerro/laravel-visitor-tracker)
15
 *
16
 * PHP version 7.2
17
 *
18
 * @category Facades
19
 * @package  RedboxTracker
20
 * @author   Johnny Mast <[email protected]>
21
 * @license  https://opensource.org/licenses/MIT MIT
22
 * @link     https://github.com/johnnymast/redbox-tracker
23
 * @since    GIT:1.0
24
 */
25
26
namespace Redbox\Tracker;
27
28
use DeviceDetector\DeviceDetector;
29
use Redbox\Tracker\Events\NewVisitorNotification;
30
31
/**
32
 * Tracker class
33
 *
34
 * Facade for tracking website visitors.
35
 *
36
 * @category Facades
37
 * @package  RedboxTracker
38
 * @author   Johnny Mast <[email protected]>
39
 * @license  https://opensource.org/licenses/MIT MIT
40
 * @link     https://github.com/johnnymast/redbox-tracker
41
 * @since    GIT:1.0
42
 */
43
class Tracker
44
{
45
46
    /**
47
     * Tracker constructor.
48
     */
49
    public function __construct()
50
    {
51
        $this->dd = new DeviceDetector(request()->header('User-Agent'));
0 ignored issues
show
Bug Best Practice introduced by
The property dd does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The function request 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

51
        $this->dd = new DeviceDetector(/** @scrutinizer ignore-call */ request()->header('User-Agent'));
Loading history...
52
        $this->dd->parse();
53
    }
54
55
    /**
56
     * Record the visit can create a new record if needed.
57
     *
58
     * @return bool
59
     */
60
    public function recordVisit(): bool
61
    {
62
        $config = config('redbox-tracker');
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

62
        $config = /** @scrutinizer ignore-call */ config('redbox-tracker');
Loading history...
63
        $user = request()->user();
0 ignored issues
show
Bug introduced by
The function request 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

63
        $user = /** @scrutinizer ignore-call */ request()->user();
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The function session 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

97
        if (/** @scrutinizer ignore-call */ session()->has('visitor') === true) {
Loading history...
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));
0 ignored issues
show
Bug introduced by
The function event 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
                /** @scrutinizer ignore-call */ 
109
                event(new NewVisitorNotification($visitor));
Loading history...
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
    }
131
132
    /**
133
     * Collect Visitor information so we can store i with the visitor.
134
     *
135
     * @return array
136
     */
137
    public function collect(): array
138
    {
139
        $request = request();
0 ignored issues
show
Bug introduced by
The function request 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

139
        $request = /** @scrutinizer ignore-call */ request();
Loading history...
140
141
        return [
142
          'ip' => $request->ip(),
143
          'user_id' => $request->user()->id ?? 0,
144
          'user_agent' => $request->userAgent(),
145
          'is_mobile' => (int)$this->dd->isMobile(),
146
          'is_desktop' => (int)$this->dd->isDesktop(),
147
          'is_bot' => (int)$this->dd->isBot(),
148
          'bot' => $this->dd->getBot()['name'] ?? '',
149
          'os' => $this->dd->getOs('name'),
150
          'browser_version' => $this->dd->getClient('version'),
151
          'browser' => $this->dd->getClient('name')
152
        ];
153
    }
154
}
155