1 | <?php namespace Arcanedev\LaravelTracker; |
||
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) |
|
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() |
|
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) |
|
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) |
|
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(); |
|
|
|||
137 | 1 | } |
|
138 | 2 | } |
|
139 | |||
140 | /** |
||
141 | * Enable the tracker. |
||
142 | */ |
||
143 | 6 | public function enable() |
|
148 | |||
149 | /** |
||
150 | * Disable the tracker. |
||
151 | */ |
||
152 | 6 | public function disable() |
|
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() |
|
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() |
|
251 | |||
252 | /** |
||
253 | * Get the tracked device id. |
||
254 | * |
||
255 | * @return int|null |
||
256 | */ |
||
257 | 6 | private function getDeviceId() |
|
258 | { |
||
263 | |||
264 | /** |
||
265 | * Get the tracked ip address ip. |
||
266 | * |
||
267 | * @return int|null |
||
268 | */ |
||
269 | 6 | private function getGeoIpId() |
|
275 | |||
276 | /** |
||
277 | * Get the tracked user agent id. |
||
278 | * |
||
279 | * @return int|null |
||
280 | */ |
||
281 | 6 | private function getAgentId() |
|
287 | |||
288 | /** |
||
289 | * Get the tracked referer id. |
||
290 | * |
||
291 | * @return int|null |
||
292 | */ |
||
293 | 6 | private function getRefererId() |
|
299 | |||
300 | /** |
||
301 | * Get the tracked cookie id. |
||
302 | * |
||
303 | * @return int|null |
||
304 | */ |
||
305 | 6 | private function getCookieId() |
|
311 | |||
312 | /** |
||
313 | * Get the tracked language id. |
||
314 | * |
||
315 | * @return int|null |
||
316 | */ |
||
317 | 6 | private function getLanguageId() |
|
323 | |||
324 | /** |
||
325 | * Check if the visitor is a robot. |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 6 | protected function isRobot() |
|
336 | } |
||
337 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.