Code Duplication    Length = 51-54 lines in 2 locations

src/SWP/Bundle/ContentBundle/Processor/EmbeddedAudioProcessor.php 1 location

@@ 26-76 (lines=51) @@
23
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
24
use Symfony\Component\DomCrawler\Crawler;
25
26
final class EmbeddedAudioProcessor implements ArticleBodyProcessorInterface
27
{
28
    /**
29
     * @var FileExtensionCheckerInterface
30
     */
31
    private $fileExtensionChecker;
32
33
    /**
34
     * @var MediaManagerInterface
35
     */
36
    private $mediaManager;
37
38
    public function __construct(FileExtensionCheckerInterface $fileExtensionChecker, MediaManagerInterface $mediaManager)
39
    {
40
        $this->fileExtensionChecker = $fileExtensionChecker;
41
        $this->mediaManager = $mediaManager;
42
    }
43
44
    public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void
45
    {
46
        $body = $article->getBody();
47
48
        preg_match(
49
            '/<audio[^>]*>/',
50
            str_replace(PHP_EOL, '', $body),
51
            $matches
52
        );
53
54
        if (empty($matches) || !isset($matches[0])) {
55
            return;
56
        }
57
58
        $audioString = $matches[0];
59
        $crawler = new Crawler($audioString);
60
        $audio = $crawler->filter('audio');
61
62
        /* @var \DOMElement $videoElement */
63
        foreach ($audio as $audioElement) {
64
            if (false !== strpos($audioElement->getAttribute('src'), ArticleMedia::getOriginalMediaId($articleMedia->getFile()->getAssetId()))) {
65
                $audioElement->setAttribute('src', $this->mediaManager->getMediaUri($articleMedia->getFile()));
66
            }
67
        }
68
69
        $article->setBody(str_replace($audioString, $crawler->filter('body')->html(), $body));
70
    }
71
72
    public function supports(string $type): bool
73
    {
74
        return $this->fileExtensionChecker->isAudio($type);
75
    }
76
}
77

src/SWP/Bundle/ContentBundle/Processor/EmbeddedVideoProcessor.php 1 location

@@ 26-79 (lines=54) @@
23
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
24
use Symfony\Component\DomCrawler\Crawler;
25
26
final class EmbeddedVideoProcessor implements ArticleBodyProcessorInterface
27
{
28
    /**
29
     * @var FileExtensionCheckerInterface
30
     */
31
    private $fileExtensionChecker;
32
33
    /**
34
     * @var MediaManagerInterface
35
     */
36
    private $mediaManager;
37
38
    public function __construct(FileExtensionCheckerInterface $fileExtensionChecker, MediaManagerInterface $mediaManager)
39
    {
40
        $this->fileExtensionChecker = $fileExtensionChecker;
41
        $this->mediaManager = $mediaManager;
42
    }
43
44
    public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void
45
    {
46
        if (null === $articleMedia->getFile()) {
47
            return;
48
        }
49
50
        $body = $article->getBody();
51
        preg_match(
52
            '/<video[^>]*>/',
53
            str_replace(PHP_EOL, '', $body),
54
            $matches
55
        );
56
57
        if (empty($matches) || !isset($matches[0])) {
58
            return;
59
        }
60
61
        $videoString = $matches[0];
62
        $crawler = new Crawler($videoString);
63
        $videos = $crawler->filter('video');
64
65
        /** @var \DOMElement $videoElement */
66
        foreach ($videos as $videoElement) {
67
            if (false !== strpos($videoElement->getAttribute('src'), ArticleMedia::getOriginalMediaId($articleMedia->getFile()->getAssetId()))) {
68
                $videoElement->setAttribute('src', $this->mediaManager->getMediaUri($articleMedia->getFile()));
69
            }
70
        }
71
72
        $article->setBody(str_replace($videoString, $crawler->filter('body')->html(), $body));
73
    }
74
75
    public function supports(string $type): bool
76
    {
77
        return $this->fileExtensionChecker->isVideo($type);
78
    }
79
}
80