Completed
Push — master ( 8fcee4...77fa1d )
by ARCANEDEV
08:36
created

Tracker::disable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php namespace Arcanedev\LaravelTracker;
2
3
use Arcanedev\LaravelTracker\Contracts\Detectors\CrawlerDetector;
4
use Arcanedev\LaravelTracker\Contracts\Tracker as TrackerContract;
5
use Arcanedev\LaravelTracker\Contracts\TrackingManager as TrackingManagerContract;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class     Tracker
11
 *
12
 * @package  Arcanedev\LaravelTracker
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class Tracker implements TrackerContract
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * The application container.
23
     *
24
     * @var \Illuminate\Contracts\Foundation\Application
25
     */
26
    protected $app;
27
28
    /**
29
     * The request instance.
30
     *
31
     * @var \Illuminate\Http\Request
32
     */
33
    private $request;
34
35
    /**
36
     * The tracking manager.
37
     *
38
     * @var \Arcanedev\LaravelTracker\Contracts\TrackingManager
39
     */
40
    private $trackingManager;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $enabled = false;
46
47
    /**
48
     * The current session data.
49
     *
50
     * @var array
51
     */
52
    protected $sessionData = [];
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Constructor
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Tracker constructor.
60
     *
61
     * @param  \Illuminate\Contracts\Foundation\Application         $app
62
     * @param  \Arcanedev\LaravelTracker\Contracts\TrackingManager  $trackingManager
63
     */
64 24
    public function __construct(Application $app, TrackingManagerContract $trackingManager)
65
    {
66 24
        $this->app             = $app;
67 24
        $this->trackingManager = $trackingManager;
68 24
        $this->enabled         = $this->getConfig('enabled', $this->enabled);
69 24
    }
70
71
    /* ------------------------------------------------------------------------------------------------
72
     |  Getters & Setters
73
     | ------------------------------------------------------------------------------------------------
74
     */
75
    /**
76
     * Get the config repository.
77
     *
78
     * @return \Illuminate\Contracts\Config\Repository
79
     */
80 24
    private function config()
81
    {
82 24
        return $this->app['config'];
83
    }
84
85
    /**
86
     * Get the tracker config.
87
     *
88
     * @param  string      $key
89
     * @param  mixed|null  $default
90
     *
91
     * @return mixed
92
     */
93 24
    private function getConfig($key, $default = null)
94
    {
95 24
        return $this->config()->get("laravel-tracker.$key", $default);
96
    }
97
98
    /**
99
     * Set the request.
100
     *
101
     * @param  \Illuminate\Http\Request  $request
102
     *
103
     * @return self
104
     */
105 6
    private function setRequest(Request $request)
106
    {
107 6
        $this->request = $request;
108
109 6
        return $this;
110
    }
111
112
    /**
113
     * Get the user agent parser.
114
     *
115
     * @return \Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser
116
     */
117 6
    public function getUserAgentParser()
118
    {
119 6
        return $this->trackingManager->getUserAgentTracker()->getUserAgentParser();
120
    }
121
122
    /* ------------------------------------------------------------------------------------------------
123
     |  Main Functions
124
     | ------------------------------------------------------------------------------------------------
125
     */
126
    /**
127
     * Start the tracking.
128
     *
129
     * @param  \Illuminate\Http\Request $request
130
     */
131 6
    public function track(Request $request)
132
    {
133 6
        if ($this->isEnabled()) {
134 6
            $this->setRequest($request);
135
136 6
            $activity = $this->getSessionActivityData();
0 ignored issues
show
Unused Code introduced by
$activity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137 3
        }
138 6
    }
139
140
    /**
141
     * Enable the tracker.
142
     */
143 6
    public function enable()
144
    {
145 6
        if ( ! $this->isEnabled())
146 6
            $this->enabled = true;
147 6
    }
148
149
    /**
150
     * Disable the tracker.
151
     */
152 6
    public function disable()
153
    {
154 6
        if ($this->isEnabled())
155 6
            $this->enabled = false;
156 6
    }
157
158
    /* ------------------------------------------------------------------------------------------------
159
     |  Check Functions
160
     | ------------------------------------------------------------------------------------------------
161
     */
162
    /**
163
     * Check if the tracker is enabled.
164
     *
165
     * @return bool
166
     */
167 24
    public function isEnabled()
168
    {
169 24
        return $this->enabled;
170
    }
171
172
    /* ------------------------------------------------------------------------------------------------
173
     |  Other Functions
174
     | ------------------------------------------------------------------------------------------------
175
     */
176
    /**
177
     * Get the session log data.
178
     *
179
     * @return array
180
     */
181 6
    protected function getSessionActivityData()
182
    {
183
        return [
184 6
            'session_id'  => $this->getSessionId(),
185 6
            'method'      => $this->request->method(),
186 6
            'path_id'     => $this->getPathId(),
187 6
            'query_id'    => $this->getQueryId(),
188 6
            'referrer_id' => $this->getRefererId(),
189 6
            'is_ajax'     => $this->request->ajax(),
190 6
            'is_secure'   => $this->request->isSecure(),
191 6
            'is_json'     => $this->request->isJson(),
192 6
            'wants_json'  => $this->request->wantsJson(),
193 3
        ];
194
    }
195
196
    /**
197
     * Get the stored session id.
198
     *
199
     * @return int
200
     */
201 6
    protected function getSessionId()
202
    {
203 6
        return $this->trackingManager->trackSession(
204 6
            $this->makeSessionData()
205 3
        );
206
    }
207
208
    /**
209
     * @return array
210
     */
211 6
    protected function makeSessionData()
212
    {
213 6
        return $this->sessionData = $this->trackingManager->checkSessionData([
214 6
            'user_id'      => $this->getUserId(),
215 6
            'device_id'    => $this->getDeviceId(),
216 6
            'client_ip'    => $this->request->getClientIp(),
217 6
            'geoip_id'     => $this->getGeoIpId(),
218 6
            'agent_id'     => $this->getAgentId(),
219 6
            'referrer_id'  => $this->getRefererId(),
220 6
            'cookie_id'    => $this->getCookieId(),
221 6
            'language_id'  => $this->getLanguageId(),
222 6
            'is_robot'     => $this->isRobot(),
223
224
            // The key user_agent is not present in the sessions table, but it's internally used to check
225
            // if the user agent changed during a session.
226 6
            'user_agent'   => $this->getUserAgentParser()->getOriginalUserAgent(),
227 6
        ], $this->sessionData);
228
    }
229
230
    /**
231
     * Track the path.
232
     *
233
     * @return int|null
234
     */
235 6
    private function getPathId()
236
    {
237
        return $this->trackIfEnabled('paths', function () {
238 6
            return $this->trackingManager->trackPath(
239 6
                $this->request->path()
240 3
            );
241 6
        });
242
    }
243
244 6
    private function getQueryId()
245
    {
246 6
        return 0;
247
    }
248
249
    /**
250
     * Get the user id.
251
     *
252
     * @return int|null
253
     */
254 6
    private function getUserId()
255
    {
256
        return $this->trackIfEnabled('users', function () {
257 6
            return $this->trackingManager->trackUser();
258 6
        });
259
    }
260
261
    /**
262
     * Get the tracked device id.
263
     *
264
     * @return int|null
265
     */
266 6
    private function getDeviceId()
267
    {
268
        return $this->trackIfEnabled('devices', function () {
269 6
            return $this->trackingManager->trackDevice();
270 6
        });
271
    }
272
273
    /**
274
     * Get the tracked ip address ip.
275
     *
276
     * @return int|null
277
     */
278 6
    private function getGeoIpId()
279
    {
280
        return $this->trackIfEnabled('geoip', function () {
281 6
            return $this->trackingManager->trackGeoIp(
282 6
                $this->request->getClientIp()
283 3
            );
284 6
        });
285
    }
286
287
    /**
288
     * Get the tracked user agent id.
289
     *
290
     * @return int|null
291
     */
292 6
    private function getAgentId()
293
    {
294
        return $this->trackIfEnabled('user-agents', function () {
295 6
            return $this->trackingManager->trackUserAgent();
296 6
        });
297
    }
298
299
    /**
300
     * Get the tracked referer id.
301
     *
302
     * @return int|null
303
     */
304 6
    private function getRefererId()
305
    {
306
        return $this->trackIfEnabled('referers', function () {
307 6
            return $this->trackingManager->trackReferer(
308 6
                $this->request->headers->get('referer'),
309 6
                $this->request->url()
310 3
            );
311 6
        });
312
    }
313
314
    /**
315
     * Get the tracked cookie id.
316
     *
317
     * @return int|null
318
     */
319 6
    private function getCookieId()
320
    {
321
        return $this->trackIfEnabled('cookies', function () {
322 6
            return $this->trackingManager->trackCookie(
323 6
                $this->request->cookie($this->getConfig('cookie.name'))
324 3
            );
325 6
        });
326
    }
327
328
    /**
329
     * Get the tracked language id.
330
     *
331
     * @return int|null
332
     */
333
    private function getLanguageId()
334
    {
335 6
        return $this->trackIfEnabled('languages', function () {
336 6
            return $this->trackingManager->trackLanguage();
337 6
        });
338
    }
339
340
    /**
341
     * Check if the visitor is a robot.
342
     *
343
     * @return bool
344
     */
345 6
    protected function isRobot()
346
    {
347
        /** @var  \Arcanedev\LaravelTracker\Contracts\Detectors\CrawlerDetector  $crawler */
348 6
        $crawler = $this->app[CrawlerDetector::class];
349
350 6
        return $crawler->isRobot();
351
    }
352
353
    /**
354
     * Track the trackable if enabled.
355
     *
356
     * @param  string      $key
357
     * @param  \Closure    $callback
358
     * @param  mixed|null  $default
359
     *
360
     * @return mixed
361
     */
362 6
    private function trackIfEnabled($key, \Closure $callback, $default = null)
363
    {
364 6
        return $this->getConfig("tracking.$key", false) ? $callback() : $default;
365
    }
366
}
367