Completed
Push — master ( a4831f...a4416a )
by ARCANEDEV
07:50
created

Tracker::getPathId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Http\Request;
7
8
/**
9
 * Class     Tracker
10
 *
11
 * @package  Arcanedev\LaravelTracker
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class Tracker implements TrackerContract
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * The application container.
22
     *
23
     * @var \Illuminate\Contracts\Foundation\Application
24
     */
25
    protected $app;
26
27
    /**
28
     * The request instance.
29
     *
30
     * @var \Illuminate\Http\Request
31
     */
32
    private $request;
33
34
    /**
35
     * The tracking manager.
36
     *
37
     * @var \Arcanedev\LaravelTracker\TrackingManager
38
     */
39
    private $trackingManager;
40
41
    /**
42
     * @var bool
43
     */
44
    protected $enabled = false;
45
46
    /**
47
     * The current session data.
48
     *
49
     * @var array
50
     */
51
    protected $sessionData = [];
52
53
    /* ------------------------------------------------------------------------------------------------
54
     |  Constructor
55
     | ------------------------------------------------------------------------------------------------
56
     */
57
    /**
58
     * Tracker constructor.
59
     *
60
     * @param  \Illuminate\Contracts\Foundation\Application  $app
61
     */
62 24
    public function __construct(Application $app)
63
    {
64 24
        $this->app             = $app;
65 24
        $this->enabled         = $this->getConfig('enabled', $this->enabled);
66 24
        $this->trackingManager = new TrackingManager;
67 24
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Getters & Setters
71
     | ------------------------------------------------------------------------------------------------
72
     */
73
    /**
74
     * Get the config repository.
75
     *
76
     * @return \Illuminate\Contracts\Config\Repository
77
     */
78 24
    private function config()
79
    {
80 24
        return $this->app['config'];
81
    }
82
83
    /**
84
     * Get the tracker config.
85
     *
86
     * @param  string      $key
87
     * @param  mixed|null  $default
88
     *
89
     * @return mixed
90
     */
91 24
    private function getConfig($key, $default = null)
92
    {
93 24
        return $this->config()->get("laravel-tracker.$key", $default);
94
    }
95
96
    /**
97
     * Set the request.
98
     *
99
     * @param  \Illuminate\Http\Request  $request
100
     *
101
     * @return self
102
     */
103 6
    private function setRequest(Request $request)
104
    {
105 6
        $this->request = $request;
106
107 6
        return $this;
108
    }
109
110
    /**
111
     * Get the user agent parser.
112
     *
113
     * @return \Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser
114
     */
115
    public function getUserAgentParser()
116
    {
117
        return $this->trackingManager->getUserAgentTracker()->getParser();
118
    }
119
120
    /* ------------------------------------------------------------------------------------------------
121
     |  Main Functions
122
     | ------------------------------------------------------------------------------------------------
123
     */
124
    /**
125
     * Start the tracking.
126
     *
127
     * @param  \Illuminate\Http\Request $request
128
     */
129 6
    public function track(Request $request)
130
    {
131 6
        if ($this->isEnabled()) {
132 6
            $this->setRequest($request);
133
134 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...
135
        }
136
    }
137
138
    /**
139
     * Enable the tracker.
140
     */
141 6
    public function enable()
142
    {
143 6
        if ( ! $this->isEnabled())
144 6
            $this->enabled = true;
145 6
    }
146
147
    /**
148
     * Disable the tracker.
149
     */
150 6
    public function disable()
151
    {
152 6
        if ($this->isEnabled())
153 6
            $this->enabled = false;
154 6
    }
155
156
    /* ------------------------------------------------------------------------------------------------
157
     |  Check Functions
158
     | ------------------------------------------------------------------------------------------------
159
     */
160
    /**
161
     * Check if the tracker is enabled.
162
     *
163
     * @return bool
164
     */
165 24
    public function isEnabled()
166
    {
167 24
        return $this->enabled;
168
    }
169
170
    /* ------------------------------------------------------------------------------------------------
171
     |  Other Functions
172
     | ------------------------------------------------------------------------------------------------
173
     */
174
    /**
175
     * Get the session log data.
176
     *
177
     * @return array
178
     */
179 6
    protected function getSessionActivityData()
180
    {
181
        return [
182 6
            'session_id'  => $this->getSessionId(),
183
            'method'      => $this->request->method(),
184
            'path_id'     => $this->getPathId(),
185
            'query_id'    => $this->getQueryId(),
186
            'referrer_id' => $this->getRefererId(),
187
            'is_ajax'     => $this->request->ajax(),
188
            'is_secure'   => $this->request->isSecure(),
189
            'is_json'     => $this->request->isJson(),
190
            'wants_json'  => $this->request->wantsJson(),
191
        ];
192
    }
193
194
    /**
195
     * Get the stored session id.
196
     *
197
     * @return int
198
     */
199 6
    protected function getSessionId()
200
    {
201 6
        return $this->trackingManager->trackSession(
202 6
            $this->makeSessionData()
203
        );
204
    }
205
206
    /**
207
     * @return array
208
     */
209 6
    protected function makeSessionData()
210
    {
211 6
        return $this->sessionData = $this->trackingManager->checkSessionData([
212 6
            'user_id'      => $this->getUserId(),
213 6
            'device_id'    => $this->getDeviceId(),
214
            'client_ip'    => $this->request->getClientIp(),
215
            'geoip_id'     => $this->getGeoIpId(),
216
            'agent_id'     => $this->getAgentId(),
217
            'referrer_id'  => $this->getRefererId(),
218
            'cookie_id'    => $this->getCookieId(),
219
            'language_id'  => $this->getLanguageId(),
220
            'is_robot'     => $this->isRobot(),
221
222
            // The key user_agent is not present in the sessions table, but it's internally used to check
223
            // if the user agent changed during a session.
224
            'user_agent'   => $this->getUserAgentParser()->getOriginalUserAgent(),
225
        ], $this->sessionData);
226
    }
227
228
    private function getPathId()
229
    {
230
        return 0;
231
    }
232
233
    private function getQueryId()
234
    {
235
        return 0;
236
    }
237
238
    /**
239
     * Get the user id.
240
     *
241
     * @return int|null
242
     */
243 6
    private function getUserId()
244
    {
245 6
        return $this->getConfig('tracking.users', false)
246 6
            ? (new TrackingManager)->trackUser()
247 6
            : null;
248
    }
249
250
    /**
251
     * Get the tracked device id.
252
     *
253
     * @return int|null
254
     */
255 6
    private function getDeviceId()
256
    {
257 6
        return $this->getConfig('tracking.devices', false)
258 6
            ? $this->trackingManager->trackDevice()
259
            : null;
260
    }
261
262
    /**
263
     * Get the tracked ip address ip.
264
     *
265
     * @return int|null
266
     */
267
    private function getGeoIpId()
268
    {
269
        return $this->getConfig('tracking.geoip', false)
270
            ? $this->trackingManager->trackGeoIp($this->request->getClientIp())
271
            : null;
272
    }
273
274
    /**
275
     * Get the tracked user agent id.
276
     *
277
     * @return int|null
278
     */
279
    private function getAgentId()
280
    {
281
        return $this->getConfig('tracking.user-agents', false)
282
            ? $this->trackingManager->trackUserAgent()
283
            : null;
284
    }
285
286
    /**
287
     * Get the tracked referer id.
288
     *
289
     * @return int|null
290
     */
291
    private function getRefererId()
292
    {
293
        return $this->getConfig('tracking.referers', false)
294
            ? $this->trackingManager->trackReferer($this->request->headers->get('referer'), $this->request->url())
295
            : null;
296
    }
297
298
    /**
299
     * Get the tracked cookie id.
300
     *
301
     * @return int|null
302
     */
303
    private function getCookieId()
304
    {
305
        return $this->getConfig('tracking.cookies', false)
306
            ? $this->trackingManager->trackCookie($this->request->cookie($this->getConfig('cookie.name')))
307
            : null;
308
    }
309
310
    /**
311
     * Get the tracked language id.
312
     *
313
     * @return int|null
314
     */
315
    private function getLanguageId()
316
    {
317
        return $this->getConfig('tracking.languages', false)
318
            ? $this->trackingManager->trackLanguage()
319
            : null;
320
    }
321
322
    /**
323
     * Check if the visitor is a robot.
324
     *
325
     * @return bool
326
     */
327
    protected function isRobot()
328
    {
329
        /** @var  \Arcanedev\LaravelTracker\Contracts\Detectors\CrawlerDetector  $crawler */
330
        $crawler = $this->app[CrawlerDetector::class];
331
332
        return $crawler->isRobot();
333
    }
334
}
335