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

ProcessRelatedArticlesListener::onArticleCreate()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 28

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 0
Metric Value
dl 3
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2019 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 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\EventListener;
18
19
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
20
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
21
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
22
use SWP\Component\Bridge\Model\GroupInterface;
23
use SWP\Component\Storage\Factory\FactoryInterface;
24
25
class ProcessRelatedArticlesListener
26
{
27
    /**
28
     * @var FactoryInterface
29
     */
30
    private $relatedArticleFactory;
31
32
    /**
33
     * @var ArticleRepositoryInterface
34
     */
35
    private $articleRepository;
36
37
    public function __construct(
38
        FactoryInterface $relatedArticleFactory,
39
        ArticleRepositoryInterface $articleRepository
40
    ) {
41
        $this->relatedArticleFactory = $relatedArticleFactory;
42
        $this->articleRepository = $articleRepository;
43
    }
44
45
    public function onArticleCreate(ArticleEvent $event): void
46
    {
47
        $package = $event->getPackage();
48
        $article = $event->getArticle();
49
50
        $this->removeOldRelatedArticles($article);
51
52
        $relatedItemsGroups = $package->getGroups()->filter(function ($group) {
53
            return GroupInterface::TYPE_RELATED === $group->getType();
54
        });
55
56 View Code Duplication
        if (null === $package || (null !== $package && 0 === \count($relatedItemsGroups))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
            return;
58
        }
59
60
        foreach ($relatedItemsGroups as $relatedItemsGroup) {
61
            foreach ($relatedItemsGroup->getItems() as $item) {
62
                if (null === ($existingArticle = $this->articleRepository->findOneBy(['code' => $item->getGuid()]))) {
63
                    continue;
64
                }
65
66
                $relatedArticle = $this->relatedArticleFactory->create();
67
68
                $relatedArticle->setArticle($existingArticle);
69
                $article->addRelatedArticle($relatedArticle);
70
            }
71
        }
72
    }
73
74
    private function removeOldRelatedArticles(ArticleInterface $article): void
75
    {
76
        foreach ($article->getRelatedArticles() as $relatedArticle) {
77
            $article->removeRelatedArticle($relatedArticle);
78
        }
79
    }
80
}
81