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); |
|
|
|
|
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
|
|
|
|
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.