Completed
Push — master ( b7d603...d97993 )
by Paweł
07:44
created

ArticleBodyProcessor::fillArticleMedia()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 24
Code Lines 13

Duplication

Lines 9
Ratio 37.5 %

Importance

Changes 0
Metric Value
dl 9
loc 24
rs 5.2139
c 0
b 0
f 0
cc 12
eloc 13
nc 10
nop 2

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
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2017 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2017 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Processor;
16
17
use Doctrine\Common\Collections\ArrayCollection;
18
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface;
19
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface;
20
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
21
use SWP\Bundle\ContentBundle\Processor\ArticleBodyProcessor as BaseProcessor;
22
use SWP\Bundle\CoreBundle\Model\ArticleMediaInterface;
23
use SWP\Bundle\CoreBundle\Model\PackageInterface;
24
use SWP\Component\Bridge\Model\ItemInterface;
25
26
final class ArticleBodyProcessor extends BaseProcessor implements ArticleBodyProcessorInterface
27
{
28
    /**
29
     * @var MediaFactoryInterface
30
     */
31
    private $mediaFactory;
32
33
    /**
34
     * ArticleBodyProcessor constructor.
35
     *
36
     * @param MediaManagerInterface $mediaManager
37
     * @param MediaFactoryInterface $mediaFactory
38
     */
39
    public function __construct(MediaManagerInterface $mediaManager, MediaFactoryInterface $mediaFactory)
40
    {
41
        parent::__construct($mediaManager);
42
43
        $this->mediaFactory = $mediaFactory;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function fillArticleMedia(PackageInterface $package, ArticleInterface $article): void
50
    {
51 View Code Duplication
        if (null === $package || (null !== $package && 0 === count($package->getItems()))) {
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
            return;
53
        }
54
55
        $articleMedia = new ArrayCollection();
56
        foreach ($package->getItems() as $packageItem) {
57
            $key = $packageItem->getName();
58 View Code Duplication
            if (ItemInterface::TYPE_PICTURE === $packageItem->getType() || ItemInterface::TYPE_FILE === $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...
59
                $articleMedia->add($this->handleMedia($article, $key, $packageItem));
60
            }
61
62
            if (null !== $packageItem->getItems() && 0 !== $packageItem->getItems()->count()) {
63
                foreach ($packageItem->getItems() as $key => $item) {
64 View Code Duplication
                    if (ItemInterface::TYPE_PICTURE === $item->getType() || ItemInterface::TYPE_FILE === $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...
65
                        $articleMedia->add($this->handleMedia($article, $key, $item));
66
                    }
67
                }
68
            }
69
        }
70
71
        $article->setMedia($articleMedia);
72
    }
73
74
    /**
75
     * @param ArticleInterface $article
76
     * @param string           $key
77
     * @param ItemInterface    $item
78
     *
79
     * @return ArticleMediaInterface
80
     */
81
    private function handleMedia(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface
82
    {
83
        $articleMedia = $this->mediaFactory->create($article, $key, $item);
84
        if (ItemInterface::TYPE_PICTURE === $item->getType()) {
85
            $this->replaceBodyImagesWithMedia($article, $articleMedia);
86
        }
87
88
        if (ArticleInterface::KEY_FEATURE_MEDIA === $key) {
89
            $article->setFeatureMedia($articleMedia);
90
        }
91
92
        return $articleMedia;
93
    }
94
}
95