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

EmbedExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureSchema() 0 6 1
A register() 0 14 2
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;
15
16
use League\CommonMark\Environment\EnvironmentBuilderInterface;
17
use League\CommonMark\Event\DocumentParsedEvent;
18
use League\CommonMark\Extension\ConfigurableExtensionInterface;
19
use League\Config\ConfigurationBuilderInterface;
20
use Nette\Schema\Expect;
21
22
final class EmbedExtension implements ConfigurableExtensionInterface
23
{
24 8
    public function configureSchema(ConfigurationBuilderInterface $builder): void
25
    {
26 8
        $builder->addSchema('embed', Expect::structure([
27 8
            'adapter' => Expect::type(EmbedAdapterInterface::class),
28 8
            'allowed_domains' => Expect::arrayOf('string')->default([]),
29 8
            'fallback' => Expect::anyOf('link', 'remove')->default('link'),
30
        ]));
31
    }
32
33 8
    public function register(EnvironmentBuilderInterface $environment): void
34
    {
35 8
        $adapter = $environment->getConfiguration()->get('embed.adapter');
36
        \assert($adapter instanceof EmbedAdapterInterface);
37
38 8
        $allowedDomains = $environment->getConfiguration()->get('embed.allowed_domains');
39 8
        if ($allowedDomains !== []) {
40 4
            $adapter = new DomainFilteringAdapter($adapter, $allowedDomains);
41
        }
42
43
        $environment
44 8
            ->addBlockStartParser(new EmbedStartParser(), 300)
45 8
            ->addEventListener(DocumentParsedEvent::class, new EmbedProcessor($adapter, $environment->getConfiguration()->get('embed.fallback')))
46 8
            ->addRenderer(Embed::class, new EmbedRenderer());
47
    }
48
}
49