Completed
Push — 1.4 ( 5f1c3d...6a61c0 )
by Paweł
09:30
created

EmbeddedImageProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 2018 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 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Processor;
18
19
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface;
20
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface;
21
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
22
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
23
use SWP\Bundle\ContentBundle\Model\ImageRendition;
24
use Symfony\Component\DomCrawler\Crawler;
25
26
final class EmbeddedImageProcessor implements EmbeddedImageProcessorInterface
27
{
28
    private const DEFAULT_ARTICLE_BODY_IMAGE_RENDITION = 'original';
29
30
    /**
31
     * @var MediaManagerInterface
32
     */
33
    private $mediaManager;
34
35
    /**
36
     * @var FileExtensionCheckerInterface
37
     */
38
    private $fileExtensionChecker;
39
40
    /**
41
     * @var string
42
     */
43
    private $defaultImageRendition;
44
45
    public function __construct(MediaManagerInterface $mediaManager, FileExtensionCheckerInterface $fileExtensionChecker)
46
    {
47
        $this->mediaManager = $mediaManager;
48
        $this->fileExtensionChecker = $fileExtensionChecker;
49
    }
50
51
    public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void
52
    {
53
        $body = $article->getBody();
54
        $mediaId = str_replace('/', '\\/', $articleMedia->getKey());
55
56
        preg_match(
57
            "/(<!-- ?EMBED START Image {id: \"$mediaId\"} ?-->)(.+?)(<!-- ?EMBED END Image {id: \"$mediaId\"} ?-->)/im",
58
            str_replace(PHP_EOL, '', $body),
59
            $embeds
60
        );
61
62
        if (empty($embeds)) {
63
            return;
64
        }
65
66
        $figureString = $embeds[2];
67
        $crawler = new Crawler($figureString);
68
        $images = $crawler->filter('figure img');
69
70
        /** @var \DOMElement $imageElement */
71
        foreach ($images as $imageElement) {
72
            /** @var ImageRendition $rendition */
73
            foreach ($articleMedia->getRenditions() as $rendition) {
74
                if ($this->getDefaultImageRendition() === $rendition->getName()) {
75
                    $attributes = $imageElement->attributes;
76
                    $altAttribute = null;
77
                    if ($imageElement->hasAttribute('alt')) {
78
                        $altAttribute = $attributes->getNamedItem('alt');
79
                    }
80
81
                    while ($attributes->length) {
82
                        $imageElement->removeAttribute($attributes->item(0)->name);
83
                    }
84
85
                    $imageElement->setAttribute('src', $this->mediaManager->getMediaUri($rendition->getImage()));
86
87
                    if (null === $rendition->getImage()->getId()) {
88
                        $imageElement->setAttribute('src', $rendition->getPreviewUrl());
89
                    }
90
91
                    $imageElement->setAttribute('data-media-id', $mediaId);
92
                    $imageElement->setAttribute('data-image-id', $rendition->getImage()->getAssetId());
93
                    $imageElement->setAttribute('data-rendition-name', $this->getDefaultImageRendition());
94
                    $imageElement->setAttribute('width', (string) $rendition->getWidth());
95
                    $imageElement->setAttribute('height', (string) $rendition->getHeight());
96
97
                    if (null !== $altAttribute) {
98
                        $imageElement->setAttribute('alt', $altAttribute->nodeValue);
99
                    }
100
                }
101
            }
102
        }
103
104
        $article->setBody(str_replace($figureString, $crawler->filter('body')->html(), $body));
105
    }
106
107
    public function setDefaultImageRendition(string $renditionName): void
108
    {
109
        $this->defaultImageRendition = $renditionName;
110
    }
111
112
    public function supports(string $type): bool
113
    {
114
        return $this->fileExtensionChecker->isImage($type);
115
    }
116
117
    private function getDefaultImageRendition(): string
118
    {
119
        if (null === $this->defaultImageRendition) {
120
            return self::DEFAULT_ARTICLE_BODY_IMAGE_RENDITION;
121
        }
122
123
        return $this->defaultImageRendition;
124
    }
125
}
126