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

ProcessArticleMediaListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 16.28 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 7
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B onArticleCreate() 7 40 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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