Tracker   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 94%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 11
eloc 50
c 8
b 0
f 0
dl 0
loc 110
ccs 47
cts 50
cp 0.94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B recordVisit() 0 65 9
A collect() 0 15 1
A __construct() 0 4 1
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 Core
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 Illuminate\Support\Facades\Auth;
30
use Redbox\Tracker\Events\NewVisitorEvent;
31
32
/**
33
 * Tracker class
34
 *
35
 * Facade for tracking website visitors.
36
 *
37
 * @category Facades
38
 * @package  RedboxTracker
39
 * @author   Johnny Mast <[email protected]>
40
 * @license  https://opensource.org/licenses/MIT MIT
41
 * @link     https://github.com/johnnymast/redbox-tracker
42
 * @since    GIT:1.0
43
 */
44
class Tracker
45
{
46
    /**
47
     * The DeviceDetector.
48
     *
49
     * @var DeviceDetector
50
     */
51
    private $dd;
52
    
53
    /**
54
     * Tracker constructor.
55
     */
56 18
    public function __construct()
57
    {
58 18
        $this->dd = new DeviceDetector((string)request()->header('User-Agent'));
59 18
        $this->dd->parse();
60 18
    }
61
    
62
    /**
63
     * Record the visit can create a new record if needed.
64
     *
65
     * @return bool
66
     */
67 18
    public function recordVisit(): bool
68
    {
69 18
        $config = config('redbox-tracker');
70 18
        $routeName = request()->route()->getName();
71 18
        $methodName = request()->getMethod();
72
        
73
        /**
74
         * Check if we should skip the current round.
75
         */
76 18
        if (in_array($routeName, $config['skip_routes'])) {
77
            return false;
78
        }
79
        
80
        /**
81
         * If the request method is not in the allowed methods
82
         * array return false.
83
         */
84 18
        if (!in_array($methodName, $config['allowed_methods'])) {
85
            return false;
86
        }
87
        
88 18
        if (Auth::check()) {
89
            /**
90
             * If we are not allowed to tracked authenticated users return false.
91
             */
92 4
            if ($config['track_authenticated_visitors'] == false) {
93 4
                return false;
94
            }
95 14
        } elseif ($config['track_unauthenticated_visitors'] == false) {
96 4
            return false;
97
        }
98
        
99
        
100 12
        if (session()->has('visitor') === true) {
101
            $visitor = session()->get('visitor');
102
        } else {
103 12
            $visitor = new Visitor();
104
        }
105
        
106 12
        $visitor->fill($this->collect());
107
        
108 12
        if ($visitor->exists === false) {
109 12
            if ($config['events']['dispatch']) {
110 12
                event(new NewVisitorEvent($visitor));
111
            }
112
        }
113
        
114 12
        $visitor->save();
115
        
116 10
        $visitorRequest = new VisitorRequest();
117 10
        $visitorRequest->visitor_id = $visitor['id'];
118 10
        $visitorRequest->domain = request()->getHttpHost();
119 10
        $visitorRequest->method = $methodName;
120 10
        $visitorRequest->route = $routeName;
121 10
        $visitorRequest->referer = request()->headers->get('Referer');
122 10
        $visitorRequest->is_secure = request()->isSecure();
123 10
        $visitorRequest->is_ajax = \request()->ajax();
124 10
        $visitorRequest->path = request()->path() ?? '';
125
        
126 10
        $visitor->requests()->save($visitorRequest);
127
        
128 10
        request()->merge(['visitor' => $visitor]);
129 10
        session()->put('visitor', $visitor);
130
        
131 10
        return true;
132
    }
133
    
134
    /**
135
     * Collect Visitor information so we can store i with the visitor.
136
     *
137
     * @return array
138
     */
139 12
    public function collect(): array
140
    {
141 12
        $request = request();
142
        
143
        return [
144 12
          'ip' => $request->ip(),
145 12
          'user_id' => $request->user()->id ?? 0,
146 12
          'user_agent' => $request->userAgent(),
147 12
          'is_mobile' => (int) $this->dd->isMobile(),
148 12
          'is_desktop' => (int) $this->dd->isDesktop(),
149 12
          'is_bot' => (int) $this->dd->isBot(),
150 12
          'bot' => $this->dd->getBot()['name'] ?? '',
151 12
          'os' => $this->dd->getOs('name'),
152 12
          'browser_version' => $this->dd->getClient('version'),
153 12
          'browser' => $this->dd->getClient('name')
154
        ];
155
    }
156
}
157