Passed
Push — 2.3 ( 32a49e )
by Colin
03:51 queued 01:09
created

OscaroteroEmbedAdapter   A

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\Extension\Embed\Embed;
18
use League\CommonMark\Extension\Embed\EmbedAdapterInterface;
19
20
final class OscaroteroEmbedAdapter implements EmbedAdapterInterface
21
{
22
    private EmbedLib $embedLib;
23
24 2
    public function __construct(?EmbedLib $embed = null)
25
    {
26 2
        if ($embed === null) {
27
            if (! \class_exists(EmbedLib::class)) {
28
                throw new \RuntimeException('The embed/embed package is not installed. Please install it with Composer to use this adapter.');
29
            }
30
31
            $embed = new EmbedLib();
32
        }
33
34 2
        $this->embedLib = $embed;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 2
    public function updateEmbeds(array $embeds): void
41
    {
42 2
        $extractors = $this->embedLib->getMulti(...\array_map(static fn (Embed $embed) => $embed->getUrl(), $embeds));
43 2
        foreach ($extractors as $i => $extractor) {
44 2
            if ($extractor->code !== null) {
45 2
                $embeds[$i]->setEmbedCode($extractor->code->html);
46
            }
47
        }
48
    }
49
}
50