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

ProcessArticleMediaListener   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 56
Duplicated Lines 33.93 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 19
loc 56
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C onArticleCreate() 19 53 13

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