| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Class PageUpdateService is used to update the count on pages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class PageUpdateService | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param array $rows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @throws \ValidationException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function updateVisits($rows) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $count = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         foreach ($rows as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             $metrics = $row->getMetrics(); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             $values = $metrics[0]->getValues(); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             $dimensions = $row->getDimensions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             $dimensions = explode('/', $dimensions[0]); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             $page = $this->findPage($dimensions); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             // Only write if a page is found | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             if ($page) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                 $count++; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 $this->updatePageVisit($page, $values); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         // If we're not getting any results back, we're out of data from Google. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         // Stop the batching process. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         if ($count === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             $this->batched = false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         return $count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param array $dimensions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return Page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function findPage($dimensions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Because analytics returns a page link that starts with a forward slash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // The first element of the array needs to be removed as it's empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         array_shift($dimensions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $firstItem = array_shift($dimensions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // If dimensions is 2 empty keys, we're looking at the homepage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (!$firstItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $firstItem = 'home'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // Start at the root of the site page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         /** @var Page $page */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $page = Page::get()->filter(['URLSegment' => $firstItem])->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         // If there are items left in the dimensions, continue traversing down | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (count($dimensions) && $page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $page = $this->findPageChildren($dimensions, $page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param array $dimensions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param Page $page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     protected function findPageChildren($dimensions, $page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         foreach ($dimensions as $segment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (strpos($segment, '?') === 0 || !$page || !$segment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             /** @var DataList|Page[] $children */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $children = $page->AllChildren(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $page = $children->filter(['URLSegment' => $segment])->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param Page $page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param array $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @throws \ValidationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected function updatePageVisit($page, $values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $rePublish = $page->isPublished(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $page->VisitCount = $values[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $page->LastAnalyticsUpdate = date('Y-m-d'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $page->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // If the page was published before write, republish or the change won't be applied | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if ($rePublish) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $page->publish('Stage', 'Live'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $page->destroy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.