Completed
Push — 1.4 ( 9d07eb...17ef63 )
by Rafał
22:44 queued 11:46
created

onArticleCreate()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 39

Duplication

Lines 26
Ratio 66.67 %

Importance

Changes 0
Metric Value
dl 26
loc 39
rs 7.6666
c 0
b 0
f 0
cc 10
nc 17
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 2016 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 2015 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\EventListener;
18
19
use SWP\Bundle\ContentBundle\Doctrine\ArticleMediaRepositoryInterface;
20
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
21
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface;
22
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
23
use SWP\Bundle\ContentBundle\Model\SlideshowItem;
24
use SWP\Bundle\ContentBundle\Processor\ArticleBodyProcessorInterface;
25
use SWP\Component\Storage\Factory\FactoryInterface;
26
27
class ProcessArticleSlideshowsListener extends AbstractArticleMediaListener
28
{
29
    /**
30
     * @var FactoryInterface
31
     */
32
    private $slideshowFactory;
33
34
    public function __construct(
35
        ArticleMediaRepositoryInterface $articleMediaRepository,
36
        MediaFactoryInterface $mediaFactory,
37
        ArticleBodyProcessorInterface $articleBodyProcessor,
38
        FactoryInterface $slideshowFactory
39
    ) {
40
        $this->slideshowFactory = $slideshowFactory;
41
42
        parent::__construct($articleMediaRepository, $mediaFactory, $articleBodyProcessor);
43
    }
44
45
    public function onArticleCreate(ArticleEvent $event): void
46
    {
47
        $package = $event->getPackage();
48
        $article = $event->getArticle();
49
50
        if (null === $package || (null !== $package && 0 === \count($package->getGroups()))) {
51
            return;
52
        }
53
54 View Code Duplication
        foreach ($package->getGroups() as $packageGroup) {
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...
55
            foreach ($packageGroup->getItems() as $item) {
56
                if ($this->isTypeAllowed($item->getType())) {
57
                    $this->removeArticleMediaIfNeeded($item->getName(), $article);
58
                }
59
            }
60
        }
61
62
        $this->removeOldArticleSlideshows($article);
63
64 View Code Duplication
        foreach ($package->getGroups() as $packageGroup) {
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...
65
            $slideshow = $this->slideshowFactory->create();
66
            $slideshow->setCode($packageGroup->getCode());
67
            $slideshow->setArticle($article);
68
69
            foreach ($packageGroup->getItems() as $item) {
70
                if ($this->isTypeAllowed($item->getType())) {
71
                    $slideshowItem = new SlideshowItem();
72
                    $articleMedia = $this->handleMedia($article, $item->getName(), $item);
73
74
                    $this->articleMediaRepository->persist($articleMedia);
75
76
                    $slideshowItem->setArticleMedia($articleMedia);
77
                    $slideshowItem->setSlideshow($slideshow);
78
79
                    $this->articleMediaRepository->persist($slideshowItem);
80
                }
81
            }
82
        }
83
    }
84
85
    public function removeOldArticleSlideshows(ArticleInterface $article): void
86
    {
87
        foreach ($article->getSlideshows() as $slideshow) {
88
            $article->removeSlideshow($slideshow);
89
        }
90
    }
91
}
92