Completed
Push — 1.4 ( f8c621...762927 )
by Rafał
22:37 queued 11:18
created

ArticleMediaProcessor::handleMedia()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\Model\ArticleInterface;
20
use SWP\Bundle\ContentBundle\Processor\ArticleBodyProcessorInterface;
21
use SWP\Bundle\CoreBundle\Model\ArticleMediaInterface;
22
use SWP\Bundle\CoreBundle\Model\PackageInterface;
23
use SWP\Component\Bridge\Model\ItemInterface;
24
use SWP\Component\Storage\Factory\FactoryInterface;
25
26
final class ArticleMediaProcessor implements ArticleMediaProcessorInterface
27
{
28
    /**
29
     * @var MediaFactoryInterface
30
     */
31
    private $mediaFactory;
32
33
    /**
34
     * @var ArticleBodyProcessorInterface
35
     */
36
    private $articleBodyProcessor;
37
38
    /**
39
     * @var FactoryInterface
40
     */
41
    private $slideshowFactory;
42
43
    /**
44
     * @var FactoryInterface
45
     */
46
    private $slideshowItemFactory;
47
48
    public function __construct(
49
        MediaFactoryInterface $mediaFactory,
50
        ArticleBodyProcessorInterface $articleBodyProcessor,
51
        FactoryInterface $slideshowFactory,
52
        FactoryInterface $slideshowItemFactory
53
    ) {
54
        $this->mediaFactory = $mediaFactory;
55
        $this->articleBodyProcessor = $articleBodyProcessor;
56
        $this->slideshowFactory = $slideshowFactory;
57
        $this->slideshowItemFactory = $slideshowItemFactory;
58
    }
59
60
    public function fillArticleMedia(PackageInterface $package, ArticleInterface $article): void
61
    {
62
        $this->handleSlideshows($package, $article);
63
64
        if (0 === \count($package->getItems())) {
65
            return;
66
        }
67
68
        $articleMedia = new ArrayCollection();
69
        foreach ($package->getItems() as $packageItem) {
70
            $key = $packageItem->getName();
71
            $articleMedia->add($this->handleMedia($article, $key, $packageItem));
72
73
            if (null !== $packageItem->getItems() && 0 !== $packageItem->getItems()->count()) {
74
                foreach ($packageItem->getItems() as $key => $item) {
75
                    $articleMedia->add($this->handleMedia($article, $key, $item));
76
                }
77
            }
78
        }
79
80
        $article->setMedia($articleMedia);
81
    }
82
83
    private function handleSlideshows(PackageInterface $package, ArticleInterface $article): void
84
    {
85
        foreach ($package->getGroups()->toArray() as $packageSlideshow) {
86
            $slideshow = $this->slideshowFactory->create();
87
            $slideshow->setCode($packageSlideshow->getCode());
88
89
            foreach ($packageSlideshow->getItems()->toArray() as $packageSlideshowItem) {
90
                $slideshowItem = $this->slideshowItemFactory->create();
91
                $articleMedia = $this->handleMedia($article, $packageSlideshowItem->getName(), $packageSlideshowItem);
92
                $slideshowItem->setArticleMedia($articleMedia);
93
                $slideshowItem->setSlideshow($slideshow);
94
95
                $slideshow->addSlideshowItem($slideshowItem);
96
            }
97
98
            $article->addSlideshow($slideshow);
99
        }
100
    }
101
102
    private function handleMedia(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface
103
    {
104
        $articleMedia = $this->mediaFactory->create($article, $key, $item);
105
106
        $this->articleBodyProcessor->process($article, $articleMedia);
107
108
        if (ArticleInterface::KEY_FEATURE_MEDIA === $key) {
109
            $article->setFeatureMedia($articleMedia);
110
        }
111
112
        return $articleMedia;
113
    }
114
}
115