Completed
Push — master ( f76772...1672bb )
by ARCANEDEV
07:47
created

Tracker::getRefererId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
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 1
        }
138 2
    }
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 2
            'method'      => $this->request->method(),
186 2
            'path_id'     => $this->getPathId(),
187 2
            'query_id'    => $this->getQueryId(),
188 2
            'referrer_id' => $this->getRefererId(),
189 2
            'is_ajax'     => $this->request->ajax(),
190 2
            'is_secure'   => $this->request->isSecure(),
191 2
            'is_json'     => $this->request->isJson(),
192 2
            'wants_json'  => $this->request->wantsJson(),
193 1
        ];
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 2
    private function getPathId()
231
    {
232 2
        return 0;
233
    }
234
235 2
    private function getQueryId()
236
    {
237 2
        return 0;
238
    }
239
240
    /**
241
     * Get the user id.
242
     *
243
     * @return int|null
244
     */
245 6
    private function getUserId()
246
    {
247 6
        return $this->getConfig('tracking.users', false)
248 6
            ? $this->trackingManager->trackUser()
249 6
            : null;
250
    }
251
252
    /**
253
     * Get the tracked device id.
254
     *
255
     * @return int|null
256
     */
257 6
    private function getDeviceId()
258
    {
259 6
        return $this->getConfig('tracking.devices', false)
260 6
            ? $this->trackingManager->trackDevice()
261 6
            : null;
262
    }
263
264
    /**
265
     * Get the tracked ip address ip.
266
     *
267
     * @return int|null
268
     */
269 6
    private function getGeoIpId()
270
    {
271 6
        return $this->getConfig('tracking.geoip', false)
272 6
            ? $this->trackingManager->trackGeoIp($this->request->getClientIp())
273 6
            : null;
274
    }
275
276
    /**
277
     * Get the tracked user agent id.
278
     *
279
     * @return int|null
280
     */
281 6
    private function getAgentId()
282
    {
283 6
        return $this->getConfig('tracking.user-agents', false)
284 6
            ? $this->trackingManager->trackUserAgent()
285 6
            : null;
286
    }
287
288
    /**
289
     * Get the tracked referer id.
290
     *
291
     * @return int|null
292
     */
293 6
    private function getRefererId()
294
    {
295 6
        return $this->getConfig('tracking.referers', false)
296 6
            ? $this->trackingManager->trackReferer($this->request->headers->get('referer'), $this->request->url())
297 6
            : null;
298
    }
299
300
    /**
301
     * Get the tracked cookie id.
302
     *
303
     * @return int|null
304
     */
305 6
    private function getCookieId()
306
    {
307 6
        return $this->getConfig('tracking.cookies', false)
308 6
            ? $this->trackingManager->trackCookie($this->request->cookie($this->getConfig('cookie.name')))
309 6
            : null;
310
    }
311
312
    /**
313
     * Get the tracked language id.
314
     *
315
     * @return int|null
316
     */
317 6
    private function getLanguageId()
318
    {
319 6
        return $this->getConfig('tracking.languages', false)
320 6
            ? $this->trackingManager->trackLanguage()
321 6
            : null;
322
    }
323
324
    /**
325
     * Check if the visitor is a robot.
326
     *
327
     * @return bool
328
     */
329 6
    protected function isRobot()
330
    {
331
        /** @var  \Arcanedev\LaravelTracker\Contracts\Detectors\CrawlerDetector  $crawler */
332 6
        $crawler = $this->app[CrawlerDetector::class];
333
334 6
        return $crawler->isRobot();
335
    }
336
}
337