OscaroteroEmbedAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 26
ccs 8
cts 11
cp 0.7272
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateEmbeds() 0 6 3
A __construct() 0 11 3
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