Completed
Push — master ( 36d725...742cf9 )
by Paweł
20s queued 10s
created

HandleArticleChangeSubscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 51
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 8 1
A processArticle() 0 5 1
A updateRoute() 0 10 2
A refreshLists() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 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 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\EventSubscriber;
18
19
use Doctrine\ORM\EntityManagerInterface;
20
use SWP\Bundle\ContentBundle\ArticleEvents;
21
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
22
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
23
use SWP\Bundle\CoreBundle\Model\ContentListItemInterface;
24
use SWP\Bundle\CoreBundle\Repository\ContentListItemRepositoryInterface;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
27
class HandleArticleChangeSubscriber implements EventSubscriberInterface
28
{
29
    private $contentListItemRepository;
30
31
    private $manager;
32
33
    public function __construct(ContentListItemRepositoryInterface $contentListItemRepository, EntityManagerInterface $manager)
34
    {
35
        $this->contentListItemRepository = $contentListItemRepository;
36
        $this->manager = $manager;
37
    }
38
39
    public static function getSubscribedEvents()
40
    {
41
        return [
42
            ArticleEvents::POST_UPDATE => 'processArticle',
43
            ArticleEvents::POST_CREATE => 'processArticle',
44
            ArticleEvents::POST_UNPUBLISH => 'processArticle',
45
        ];
46
    }
47
48
    public function processArticle(ArticleEvent $event)
49
    {
50
        $this->refreshLists($event->getArticle());
51
        $this->updateRoute($event->getArticle());
52
    }
53
54
    private function updateRoute(ArticleInterface $article)
55
    {
56
        $route = $article->getRoute();
57
        if (null === $route) {
58
            return;
59
        }
60
61
        $route->setArticlesUpdatedAt(new \DateTime());
62
        $this->manager->flush();
63
    }
64
65
    private function refreshLists(ArticleInterface $article)
66
    {
67
        $contentListItems = $this->contentListItemRepository->findItemsByArticle($article);
0 ignored issues
show
Compatibility introduced by
$article of type object<SWP\Bundle\Conten...Model\ArticleInterface> is not a sub-type of object<SWP\Bundle\CoreBu...Model\ArticleInterface>. It seems like you assume a child interface of the interface SWP\Bundle\ContentBundle\Model\ArticleInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
68
        /** @var ContentListItemInterface $item */
69
        foreach ($contentListItems as $item) {
70
            if (!$article->isPublished()) {
71
                $this->manager->remove($item);
72
            }
73
            $item->getContentList()->setUpdatedAt(new \DateTime('now'));
74
        }
75
        $this->manager->flush();
76
    }
77
}
78