| 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\CoreBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Doctrine\Common\Persistence\ObjectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Bundle\AnalyticsBundle\Model\ArticleEventInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Bundle\AnalyticsBundle\Model\ArticleStatisticsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Bundle\AnalyticsBundle\Services\ArticleStatisticsServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Bundle\ContentBundle\Model\ArticleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use SWP\Component\Storage\Factory\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use SWP\Component\Storage\Repository\RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class ArticleStatisticsService implements ArticleStatisticsServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $articleStatisticsRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $articleStatisticsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $articleObjectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         RepositoryInterface $articleStatisticsRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         FactoryInterface $articleStatisticsFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ObjectManager $articleObjectManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->articleStatisticsRepository = $articleStatisticsRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->articleStatisticsFactory = $articleStatisticsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->articleObjectManager = $articleObjectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function addArticleEvent(int $articleId, string $action, array $extraData): ArticleStatisticsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $articleStatistics = $this->getOrCreateNewArticleStatistics($articleId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (ArticleEventInterface::ACTION_PAGEVIEW === $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->addNewPageViewEvent($articleStatistics, $articleId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $articleStatistics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function getOrCreateNewArticleStatistics(int $articleId): ArticleStatisticsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         /** @var ArticleStatisticsInterface $articleStatistics */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $articleStatistics = $this->articleStatisticsRepository->findOneBy(['article' => $articleId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (null === $articleStatistics) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             /** @var ArticleStatisticsInterface $articleStatistics */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $articleStatistics = $this->articleStatisticsFactory->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $articleStatistics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     protected function addNewPageViewEvent(ArticleStatisticsInterface $articleStatistics, int $articleId): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         /** @var ArticleInterface $article */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $article = $this->articleObjectManager->getReference(ArticleInterface::class, $articleId); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (null === $article) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->articleStatisticsRepository->add($articleStatistics); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: