| 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\Domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Arcanedev\LaravelTracker\Models\Referer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Arcanedev\LaravelTracker\Models\RefererSearchTerm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class     RefererTracker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package  Arcanedev\LaravelTracker\Trackers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author   ARCANEDEV <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class RefererTracker implements RefererTrackerContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      |  Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $refererParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      |  Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * RefererTracker constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param  \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser  $refererParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 24 |  |     public function __construct(RefererParser $refererParser) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 24 |  |         $this->refererParser = $refererParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 24 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      |  Main Functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Track the referer and return the id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param  string  $refererUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param  string  $pageUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return int|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function track($refererUrl, $pageUrl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $firstParsed = $this->refererParser->parseUrl($refererUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($firstParsed) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $domainId   = $this->trackDomain($firstParsed['domain']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $attributes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 'url'               => $firstParsed['url'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 'host'              => $firstParsed['host'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 'domain_id'         => $domainId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'medium'            => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 'source'            => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'search_terms_hash' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $secondParsed = $this->refererParser->parse($firstParsed['url'], $pageUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             if ($secondParsed->isKnown()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $attributes['medium']            = $secondParsed->getMedium(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $attributes['source']            = $secondParsed->getSource(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $attributes['search_terms_hash'] = sha1($secondParsed->getSearchTerm()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $referer = Referer::firstOrCreate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 Arr::only($attributes, ['url', 'search_terms_hash']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             if ($secondParsed->isKnown()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $this->trackRefererSearchTerms($referer->id, $secondParsed->getSearchTerm()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             return $referer->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Track the domain and get the id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param  string  $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     private function trackDomain($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $data = compact('name'); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         return Domain::firstOrCreate($data, $data)->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Store the referer's search terms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param  int     $refererId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param  string  $searchTerms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     private function trackRefererSearchTerms($refererId, $searchTerms) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         foreach (explode(' ', $searchTerms) as $term) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $attributes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 'referer_id'  => $refererId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 'search_term' => $term, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             RefererSearchTerm::firstOrCreate($attributes, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
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.