OscaroteroEmbedAdapter::updateEmbeds()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Embed\Bridge;
15
16
use Embed\Embed as EmbedLib;
17
use League\CommonMark\Exception\MissingDependencyException;
18
use League\CommonMark\Extension\Embed\Embed;
19
use League\CommonMark\Extension\Embed\EmbedAdapterInterface;
20
21
final class OscaroteroEmbedAdapter implements EmbedAdapterInterface
22
{
23
    private EmbedLib $embedLib;
24
25 2
    public function __construct(?EmbedLib $embed = null)
26
    {
27 2
        if ($embed === null) {
28
            if (! \class_exists(EmbedLib::class)) {
29
                throw new MissingDependencyException('The embed/embed package is not installed. Please install it with Composer to use this adapter.');
30
            }
31
32
            $embed = new EmbedLib();
33
        }
34
35 2
        $this->embedLib = $embed;
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 2
    public function updateEmbeds(array $embeds): void
42
    {
43 2
        $extractors = $this->embedLib->getMulti(...\array_map(static fn (Embed $embed) => $embed->getUrl(), $embeds));
44 2
        foreach ($extractors as $i => $extractor) {
45 2
            if ($extractor->code !== null) {
46 2
                $embeds[$i]->setEmbedCode($extractor->code->html);
47
            }
48
        }
49
    }
50
}
51