Completed
Push — 1.4 ( be8fd1...db7ea8 )
by Paweł
23s queued 11s
created

EmbeddedMediaBlockProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Model\ArticleInterface;
21
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
22
use Symfony\Component\DomCrawler\Crawler;
23
24
final class EmbeddedMediaBlockProcessor implements ArticleBodyProcessorInterface
25
{
26
    /**
27
     * @var FileExtensionCheckerInterface
28
     */
29
    private $fileExtensionChecker;
30
31
    public function __construct(FileExtensionCheckerInterface $fileExtensionChecker)
32
    {
33
        $this->fileExtensionChecker = $fileExtensionChecker;
34
    }
35
36
    public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void
37
    {
38
        if (ArticleInterface::KEY_FEATURE_MEDIA === $articleMedia->getKey()) {
39
            return;
40
        }
41
42
        $body = $article->getBody();
43
        $mediaId = str_replace('/', '\\/', $articleMedia->getKey());
44
45
        preg_match(
46
            '/(<div class="media-block">)(.+?)(<\/div>)/im',
47
            str_replace(PHP_EOL, '', $body),
48
            $embeds
49
        );
50
51
        if (empty($embeds)) {
52
            return;
53
        }
54
55
        [0 => $mediaBlockDiv, 2 => $figureString] = $embeds;
0 ignored issues
show
Bug introduced by
The variable $mediaBlockDiv does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $figureString does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
57
        $crawler = new Crawler($figureString);
58
        $src = $crawler->filterXPath('//img')->attr('src');
59
        $alt = $crawler->filterXPath('//img')->attr('alt');
60
        $captionNode = $crawler->filterXPath('//span[@class="media-block__description"]')->first();
61
        $caption = $captionNode->getNode(0)->textContent;
62
63
        $html = '<!-- EMBED START Image {id: "'.$mediaId.'"} --><figure><img src="'.$src.'" alt="'.$alt.'"/><figcaption>'.$caption.'</figcaption></figure><!-- EMBED END Image {id: "'.$mediaId.'"} -->';
64
65
        $article->setBody(str_replace($mediaBlockDiv, $html, $body));
66
    }
67
68
    public function supports(string $type): bool
69
    {
70
        return $this->fileExtensionChecker->isImage($type);
71
    }
72
}
73