Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

ProcessArticleMediaListener::createImageMedia()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4.0072

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 24
cts 26
cp 0.9231
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 26
nc 4
nop 2
crap 4.0072
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 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 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\EventListener;
16
17
use Doctrine\Common\Persistence\ObjectManager;
18
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
19
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface;
20
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface;
21
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
22
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
23
use SWP\Component\Bridge\Model\ItemInterface;
24
use Symfony\Component\DomCrawler\Crawler;
25
26
class ProcessArticleMediaListener
27
{
28
    /**
29
     * @var ObjectManager
30
     */
31
    protected $objectManager;
32
33
    /**
34
     * @var MediaManagerInterface
35
     */
36
    protected $mediaManager;
37
38
    /**
39
     * @var MediaFactoryInterface
40
     */
41
    protected $mediaFactory;
42
43
    /**
44
     * ProcessArticleMediaListener constructor.
45
     *
46
     * @param ObjectManager         $objectManager
47
     * @param MediaManagerInterface $mediaManager
48
     * @param MediaFactoryInterface $mediaFactory
49
     */
50 17
    public function __construct(
51
        ObjectManager $objectManager,
52
        MediaManagerInterface $mediaManager,
53
        MediaFactoryInterface $mediaFactory
54
    ) {
55 17
        $this->objectManager = $objectManager;
56 17
        $this->mediaManager = $mediaManager;
57 17
        $this->mediaFactory = $mediaFactory;
58 17
    }
59
60
    /**
61
     * @param ArticleEvent $event
62
     */
63 16
    public function onArticleCreate(ArticleEvent $event)
64
    {
65 16
        $package = $event->getPackage();
66 16
        $article = $event->getArticle();
67 16
        $this->objectManager->persist($article);
68
69 16
        if (null !== $package && 0 === count($package->getItems())) {
70 6
            return;
71
        }
72
73 11
        foreach ($package->getItems() as $key => $packageItem) {
74 11 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...
75 2
                $articleMedia = $this->handleMedia($article, $key, $packageItem);
76 2
                $this->objectManager->persist($articleMedia);
77
            }
78
79 11
            if (null !== $packageItem->getItems() && 0 !== $packageItem->getItems()->count()) {
80 1
                foreach ($packageItem->getItems() as $key => $item) {
81 1 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...
82 1
                        $articleMedia = $this->handleMedia($article, $key, $item);
83 11
                        $this->objectManager->persist($articleMedia);
84
                    }
85
                }
86
            }
87
        }
88 11
    }
89
90
    /**
91
     * @param ArticleInterface $article
92
     * @param string           $key
93
     * @param ItemInterface    $item
94
     *
95
     * @return ArticleMediaInterface
96
     */
97 4
    public function handleMedia(ArticleInterface $article, string $key, ItemInterface $item)
98
    {
99 4
        $articleMedia = $this->mediaFactory->create($article, $key, $item);
100
101 4
        if (ItemInterface::TYPE_PICTURE === $item->getType()) {
102 3
            $this->replaceBodyImagesWithMedia($article, $articleMedia);
103 1
        } elseif (ItemInterface::TYPE_FILE === $item->getType()) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
104
            //TODO: handle files upload
105
        }
106
107 4
        return $articleMedia;
108
    }
109
110
    /**
111
     * @param ArticleInterface      $article
112
     * @param ArticleMediaInterface $articleMedia
113
     */
114 3
    private function replaceBodyImagesWithMedia(ArticleInterface $article, ArticleMediaInterface $articleMedia)
115
    {
116 3
        $body = $article->getBody();
117 3
        $mediaId = $articleMedia->getKey();
118 3
        preg_match(
119 3
            "/(<!-- EMBED START Image {id: \"$mediaId\"} -->)(.+?)(<!-- EMBED END Image {id: \"$mediaId\"} -->)/im",
120 3
            str_replace(PHP_EOL, '', $body),
121
            $embeds
122
        );
123 3
        if (empty($embeds)) {
124
            return;
125
        }
126
127 3
        $figureString = $embeds[2];
128 3
        $crawler = new Crawler($figureString);
129 3
        $images = $crawler->filter('figure img');
130
        /** @var \DOMElement $imageElement */
131 3
        foreach ($images as $imageElement) {
132 3
            foreach ($articleMedia->getRenditions() as $rendition) {
133 3
                if (strpos($imageElement->getAttribute('src'), $rendition->getImage()->getAssetId()) !== false) {
134 3
                    $attributes = $imageElement->attributes;
135 3
                    while ($attributes->length) {
136 3
                        $imageElement->removeAttribute($attributes->item(0)->name);
137
                    }
138 3
                    $imageElement->setAttribute('src', $this->mediaManager->getMediaUri($rendition->getImage()));
139 3
                    $imageElement->setAttribute('data-media-id', $mediaId);
140 3
                    $imageElement->setAttribute('data-image-id', $rendition->getImage()->getAssetId());
141
                }
142
            }
143
        }
144
145 3
        $article->setBody(str_replace($figureString, $crawler->filter('body')->html(), $body));
146 3
    }
147
}
148