ARCANEDEV /
LaravelTracker
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php namespace Arcanedev\LaravelTracker\Trackers; |
||
| 2 | |||
| 3 | use Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser; |
||
| 4 | use Arcanedev\LaravelTracker\Contracts\Trackers\RefererTracker as RefererTrackerContract; |
||
| 5 | use Arcanedev\LaravelTracker\Models\AbstractModel; |
||
| 6 | use Illuminate\Support\Arr; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class RefererTracker |
||
| 10 | * |
||
| 11 | * @package Arcanedev\LaravelTracker\Trackers |
||
| 12 | * @author ARCANEDEV <[email protected]> |
||
| 13 | */ |
||
| 14 | class RefererTracker extends AbstractTracker implements RefererTrackerContract |
||
| 15 | { |
||
| 16 | /* ------------------------------------------------------------------------------------------------ |
||
| 17 | | Getters and Setters |
||
| 18 | | ------------------------------------------------------------------------------------------------ |
||
| 19 | */ |
||
| 20 | /** |
||
| 21 | * Get the model. |
||
| 22 | * |
||
| 23 | * @return \Arcanedev\LaravelTracker\Models\Referer |
||
| 24 | */ |
||
| 25 | 6 | protected function getModel() |
|
| 26 | { |
||
| 27 | 6 | return $this->makeModel(AbstractModel::MODEL_REFERER); |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser |
||
| 32 | */ |
||
| 33 | 18 | private function getRefererParser() |
|
| 34 | { |
||
| 35 | 18 | return $this->make(RefererParser::class); |
|
| 36 | } |
||
| 37 | |||
| 38 | /* ------------------------------------------------------------------------------------------------ |
||
| 39 | | Main Functions |
||
| 40 | | ------------------------------------------------------------------------------------------------ |
||
| 41 | */ |
||
| 42 | /** |
||
| 43 | * Track the referer and return the id. |
||
| 44 | * |
||
| 45 | * @param string $refererUrl |
||
| 46 | * @param string $pageUrl |
||
| 47 | * |
||
| 48 | * @return int|null |
||
| 49 | */ |
||
| 50 | 18 | public function track($refererUrl, $pageUrl) |
|
| 51 | { |
||
| 52 | 18 | $firstParsed = $this->getRefererParser()->parseUrl($refererUrl); |
|
| 53 | |||
| 54 | 18 | if ($firstParsed) { |
|
|
0 ignored issues
–
show
|
|||
| 55 | 6 | $domainId = $this->trackDomain($firstParsed['domain']); |
|
| 56 | $attributes = [ |
||
| 57 | 6 | 'url' => $firstParsed['url'], |
|
| 58 | 6 | 'host' => $firstParsed['host'], |
|
| 59 | 6 | 'domain_id' => $domainId, |
|
| 60 | 3 | 'medium' => null, |
|
| 61 | 3 | 'source' => null, |
|
| 62 | 3 | 'search_terms_hash' => null, |
|
| 63 | 3 | ]; |
|
| 64 | |||
| 65 | 6 | $secondParsed = $this->getRefererParser()->parse($firstParsed['url'], $pageUrl); |
|
| 66 | |||
| 67 | 6 | if ($secondParsed->isKnown()) { |
|
| 68 | 6 | $attributes['medium'] = $secondParsed->getMedium(); |
|
| 69 | 6 | $attributes['source'] = $secondParsed->getSource(); |
|
| 70 | 6 | $attributes['search_terms_hash'] = sha1($secondParsed->getSearchTerm()); |
|
| 71 | 3 | } |
|
| 72 | |||
| 73 | 6 | $referer = $this->getModel()->firstOrCreate( |
|
| 74 | 6 | Arr::only($attributes, ['url', 'search_terms_hash']), |
|
| 75 | $attributes |
||
| 76 | 3 | ); |
|
| 77 | |||
| 78 | 6 | if ($secondParsed->isKnown()) { |
|
| 79 | 6 | $this->trackRefererSearchTerms($referer, $secondParsed->getSearchTerm()); |
|
| 80 | 3 | } |
|
| 81 | |||
| 82 | 6 | return $referer->id; |
|
| 83 | } |
||
| 84 | |||
| 85 | 12 | return null; |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Track the domain and get the id. |
||
| 90 | * |
||
| 91 | * @param string $name |
||
| 92 | * |
||
| 93 | * @return int |
||
| 94 | */ |
||
| 95 | 6 | private function trackDomain($name) |
|
| 96 | { |
||
| 97 | 6 | return $this->makeModel(AbstractModel::MODEL_DOMAIN) |
|
| 98 | 6 | ->firstOrCreate(compact('name'))->id; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Store the referer's search terms. |
||
| 103 | * |
||
| 104 | * @param \Arcanedev\LaravelTracker\Models\Referer $referer |
||
| 105 | * @param string $searchTerms |
||
| 106 | */ |
||
| 107 | 6 | private function trackRefererSearchTerms($referer, $searchTerms) |
|
| 108 | { |
||
| 109 | 6 | $terms = []; |
|
| 110 | |||
| 111 | 6 | foreach (explode(' ', $searchTerms) as $term) { |
|
| 112 | 6 | $terms[] = $this->makeModel(AbstractModel::MODEL_REFERER_SEARCH_TERM)->fill([ |
|
| 113 | 6 | 'search_term' => $term, |
|
| 114 | 3 | ]); |
|
| 115 | 3 | } |
|
| 116 | |||
| 117 | 6 | if (count($terms) > 0) |
|
| 118 | 6 | $referer->searchTerms()->saveMany($terms); |
|
| 119 | |||
| 120 | 6 | } |
|
| 121 | } |
||
| 122 |
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.