| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Superdesk Web Publisher Analytics Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright 2017 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright 2017 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace SWP\Bundle\AnalyticsBundle\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SWP\Bundle\AnalyticsBundle\Model\ArticleEventInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Bundle\AnalyticsBundle\Model\ArticleStatisticsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Bundle\ContentBundle\Model\ArticleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Bundle\ContentBundle\Model\RouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use SWP\Component\Storage\Factory\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use SWP\Component\Storage\Repository\RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Class ArticleStatisticsService. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class ArticleStatisticsService implements ArticleStatisticsServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var ArticleRepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $articleRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $articleStatisticsRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var FactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected $articleStatisticsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var FactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $articleEventFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ArticleRepositoryInterface $articleRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         RepositoryInterface $articleStatisticsRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         FactoryInterface $articleStatisticsFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         FactoryInterface $articleEventFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->articleRepository = $articleRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->articleStatisticsRepository = $articleStatisticsRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->articleStatisticsFactory = $articleStatisticsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->articleEventFactory = $articleEventFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function addArticleEvent(int $articleId, string $action, array $extraData): ArticleStatisticsInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $articleStatistics = $this->getOrCreateNewArticleStatistics($articleId); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         switch ($action) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             case ArticleEventInterface::ACTION_PAGEVIEW: | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 $this->addNewPageViewEvent($articleStatistics, $articleId); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             case ArticleEventInterface::ACTION_IMPRESSION: | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 $sourceArticle = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                 $sourceRoute = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 $type = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 if (array_key_exists('sourceArticle', $extraData)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                     $sourceArticle = $extraData['sourceArticle']; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 if (array_key_exists('sourceRoute', $extraData)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                     $sourceRoute = $extraData['sourceRoute']; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 if (array_key_exists('type', $extraData)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                     $type = $extraData['type']; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 $this->addNewImpressionEvent($articleStatistics, $articleId, $sourceArticle, $sourceRoute, $type); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         return $articleStatistics; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     protected function getOrCreateNewArticleStatistics(int $articleId): ArticleStatisticsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         /** @var ArticleStatisticsInterface $articleStatistics */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $articleStatistics = $this->articleStatisticsRepository->findOneBy(['article' => $articleId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (null === $articleStatistics) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             /** @var ArticleStatisticsInterface $articleStatistics */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $articleStatistics = $this->articleStatisticsFactory->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $articleStatistics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected function addNewPageViewEvent(ArticleStatisticsInterface $articleStatistics, int $articleId): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         /** @var ArticleInterface $article */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $article = $this->articleRepository->findOneBy(['id' => $articleId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->getArticleEvent($articleStatistics, $article, ArticleEventInterface::ACTION_PAGEVIEW); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $articleStatistics->increasePageViewsNumber(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->articleStatisticsRepository->add($articleStatistics); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     protected function addNewImpressionEvent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         ArticleStatisticsInterface $articleStatistics, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         int $articleId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ArticleInterface $sourceArticle = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         RouteInterface $sourceRoute = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         string $type = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         /** @var ArticleInterface $article */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $article = $this->articleRepository->findOneBy(['id' => $articleId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $articleEvent = $this->getArticleEvent($articleStatistics, $article, ArticleEventInterface::ACTION_IMPRESSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $articleEvent->setImpressionArticle($sourceArticle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $articleEvent->setImpressionRoute($sourceRoute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $articleEvent->setImpressionType($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $articleStatistics->increaseImpressionsNumber(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->articleStatisticsRepository->add($articleStatistics); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     private function getArticleEvent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ArticleStatisticsInterface $articleStatistics, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         ArticleInterface $article, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     ): ArticleEventInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         /** @var ArticleEventInterface $articleEvent */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $articleEvent = $this->articleEventFactory->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $articleEvent->setAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $articleEvent->setArticleStatistics($articleStatistics); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->articleStatisticsRepository->persist($articleEvent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $articleStatistics->addEvent($articleEvent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $articleStatistics->setArticle($article); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return $articleEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |