Completed
Push — master ( 069b3e...031a7a )
by Paweł
09:08
created

ProcessArticleMediaListener::onArticleCreate()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 6
cts 6
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 17
nop 1
crap 10

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