Completed
Push — master ( 05bb5a...5289b8 )
by Rafał
34:32 queued 04:15
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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