Completed
Push — 1.5 ( 51c4c2...846d5e )
by Rafał
27:12 queued 16:58
created

ProcessArticleMediaListener::onArticleCreate()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 40

Duplication

Lines 7
Ratio 17.5 %

Importance

Changes 0
Metric Value
dl 7
loc 40
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\Event\ArticleEvent;
20
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
21
22
class ProcessArticleMediaListener extends AbstractArticleMediaListener
23
{
24
    public function onArticleCreate(ArticleEvent $event): void
25
    {
26
        $package = $event->getPackage();
27
        $article = $event->getArticle();
28
29
        if (null === $package || (null !== $package && 0 === \count($package->getItems()))) {
30
            return;
31
        }
32
33
        $this->removeOldArticleMedia($article);
34
35
        $guids = [];
36 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...
37
            foreach ($packageGroup->getItems() as $item) {
38
                if ($this->isTypeAllowed($item->getType())) {
39
                    $guids[] = $item->getGuid();
40
                }
41
            }
42
        }
43
44
        $items = $package->getItems()->filter(
45
            function ($entry) use ($guids) {
46
                return !\in_array($entry->getGuid(), $guids, true);
47
            }
48
        );
49
50
        foreach ($items as $packageItem) {
51
            $key = $packageItem->getName();
52
            if ($this->isTypeAllowed($packageItem->getType())) {
53
                $this->removeArticleMediaIfNeeded($key, $article);
54
55
                $articleMedia = $this->handleMedia($article, $key, $packageItem);
56
                $this->articleMediaRepository->persist($articleMedia);
57
58
                if (ArticleInterface::KEY_FEATURE_MEDIA === $key) {
59
                    $article->setFeatureMedia($articleMedia);
60
                }
61
            }
62
        }
63
    }
64
}
65