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

EmbedExtension::configureSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
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;
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