Completed
Push — master ( d01ad3...6b97e6 )
by Paweł
21s queued 10s
created

ArticleStatisticsService::addArticleEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
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);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getReference() does only exist in the following implementations of said interface: Doctrine\ODM\PHPCR\Decor...ocumentManagerDecorator, Doctrine\ODM\PHPCR\DocumentManager, Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager, Doctrine\Tests\ODM\PHPCR...ManagerGetClassMetadata, Pixers\DoctrineProfilerBundle\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
71
        if (null === $article) {
72
            return;
73
        }
74
75
        $this->articleStatisticsRepository->add($articleStatistics);
76
    }
77
}
78