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

ProcessArticleMediaListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B onArticleCreate() 0 40 10
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