Completed
Push — latest ( 02d08e...e7e0af )
by Colin
18:09 queued 16:46
created

MentionExtension::register()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
ccs 13
cts 14
cp 0.9286
rs 8.6506
cc 7
nc 10
nop 1
crap 7.0178
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\Mention;
15
16
use League\CommonMark\Environment\ConfigurableEnvironmentInterface;
17
use League\CommonMark\Extension\ExtensionInterface;
18
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
19
20
final class MentionExtension implements ExtensionInterface
21
{
22 15
    public function register(ConfigurableEnvironmentInterface $environment): void
23
    {
24 15
        $mentions = $environment->getConfig('mentions', []);
25 15
        foreach ($mentions as $name => $mention) {
26 12
            foreach (['symbol', 'regex', 'generator'] as $key) {
27 12
                if (! \array_key_exists($key, $mention)) {
28
                    throw new \RuntimeException(\sprintf('Missing "%s" from MentionParser configuration', $key));
29
                }
30
            }
31
32 12
            if ($mention['generator'] instanceof MentionGeneratorInterface) {
33 3
                $environment->addInlineParser(new MentionParser($mention['symbol'], $mention['regex'], $mention['generator']));
34 9
            } elseif (\is_string($mention['generator'])) {
35 3
                $environment->addInlineParser(MentionParser::createWithStringTemplate($mention['symbol'], $mention['regex'], $mention['generator']));
36 6
            } elseif (\is_callable($mention['generator'])) {
37 3
                $environment->addInlineParser(MentionParser::createWithCallback($mention['symbol'], $mention['regex'], $mention['generator']));
38
            } else {
39 3
                throw new \RuntimeException(\sprintf('The "generator" provided for the MentionParser configuration must be a string template, callable, or an object that implements %s.', MentionGeneratorInterface::class));
40
            }
41
        }
42 12
    }
43
}
44