1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\EventSubscriber\Meta; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
18
|
|
|
use SWP\Bundle\CoreBundle\Repository\ContentListItemRepositoryInterface; |
19
|
|
|
use SWP\Component\ContentList\Model\ContentListItemInterface; |
20
|
|
|
use SWP\Component\ContentList\Model\ListContentInterface; |
21
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
22
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Event\MetaEvent; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
24
|
|
|
|
25
|
|
|
class ArticleContentListsSubscriber implements EventSubscriberInterface |
26
|
|
|
{ |
27
|
|
|
private $contentListItemRepository; |
28
|
|
|
|
29
|
|
|
public function __construct(ContentListItemRepositoryInterface $contentListItemRepository) |
30
|
|
|
{ |
31
|
|
|
$this->contentListItemRepository = $contentListItemRepository; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function getSubscribedEvents() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
Context::META_EVENT_NAME => 'fetchContentLists', |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function fetchContentLists(MetaEvent $event) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if ('contentLists' !== $event->getPropertyName()) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$object = $event->getData(); |
48
|
|
|
if (!$object instanceof ArticleInterface || !$object instanceof ListContentInterface) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$contentListItems = $this->contentListItemRepository->findItemsByArticle($object); |
53
|
|
|
if (0 === \count($contentListItems)) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$contentLists = []; |
58
|
|
|
/** @var ContentListItemInterface $contentListItem */ |
59
|
|
|
foreach ($contentListItems as $contentListItem) { |
60
|
|
|
if (!in_array($contentList = $contentListItem->getContentList(), $contentLists)) { |
61
|
|
|
$contentLists[] = $contentList; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$event->setResult($contentLists); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.