1 | <?php namespace Arcanedev\LaravelTracker\Trackers; |
||
14 | class RefererTracker extends AbstractTracker implements RefererTrackerContract |
||
15 | { |
||
16 | /* ----------------------------------------------------------------- |
||
17 | | Getters and Setters |
||
18 | | ----------------------------------------------------------------- |
||
19 | */ |
||
20 | |||
21 | /** |
||
22 | * Get the model. |
||
23 | * |
||
24 | * @return \Arcanedev\LaravelTracker\Models\Referer |
||
25 | */ |
||
26 | 3 | protected function getModel() |
|
30 | |||
31 | /** |
||
32 | * @return \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser |
||
33 | */ |
||
34 | 9 | private function getRefererParser() |
|
38 | |||
39 | /* ----------------------------------------------------------------- |
||
40 | | Main Methods |
||
41 | | ----------------------------------------------------------------- |
||
42 | */ |
||
43 | |||
44 | /** |
||
45 | * Track the referer and return the id. |
||
46 | * |
||
47 | * @param string $refererUrl |
||
48 | * @param string $pageUrl |
||
49 | * |
||
50 | * @return int|null |
||
51 | */ |
||
52 | 9 | public function track($refererUrl, $pageUrl) |
|
53 | { |
||
54 | 9 | $firstParsed = $this->getRefererParser()->parseUrl($refererUrl); |
|
55 | |||
56 | 9 | if ($firstParsed) { |
|
|
|||
57 | $attributes = [ |
||
58 | 3 | 'url' => $firstParsed['url'], |
|
59 | 3 | 'host' => $firstParsed['host'], |
|
60 | 3 | 'domain_id' => $this->trackDomain($firstParsed['domain']), |
|
61 | 'medium' => null, |
||
62 | 'source' => null, |
||
63 | 'search_terms_hash' => null, |
||
64 | ]; |
||
65 | |||
66 | 3 | $secondParsed = $this->getRefererParser()->parse($firstParsed['url'], $pageUrl); |
|
67 | |||
68 | 3 | if ($secondParsed->isKnown()) { |
|
69 | 3 | $attributes['medium'] = $secondParsed->getMedium(); |
|
70 | 3 | $attributes['source'] = $secondParsed->getSource(); |
|
71 | 3 | $attributes['search_terms_hash'] = sha1($secondParsed->getSearchTerm()); |
|
72 | } |
||
73 | |||
74 | /** @var \Arcanedev\LaravelTracker\Models\Referer $referer */ |
||
75 | 3 | $referer = $this->getModel()->newQuery()->firstOrCreate( |
|
76 | 3 | Arr::only($attributes, ['url', 'search_terms_hash']), |
|
77 | 2 | $attributes |
|
78 | ); |
||
79 | |||
80 | 3 | if ($secondParsed->isKnown()) { |
|
81 | 3 | $this->trackRefererSearchTerms($referer, $secondParsed->getSearchTerm()); |
|
82 | } |
||
83 | |||
84 | 3 | return $referer->id; |
|
85 | } |
||
86 | |||
87 | 6 | return null; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Track the domain and get the id. |
||
92 | * |
||
93 | * @param string $name |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | 3 | private function trackDomain($name) |
|
104 | |||
105 | /** |
||
106 | * Store the referer's search terms. |
||
107 | * |
||
108 | * @param \Arcanedev\LaravelTracker\Contracts\Models\Referer $referer |
||
109 | * @param string $searchTerms |
||
110 | */ |
||
111 | 3 | private function trackRefererSearchTerms($referer, $searchTerms) |
|
125 | } |
||
126 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.