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) { |
|
|
|
|
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
|
|
|
|
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.