Completed
Push — 1.5 ( 2f97af...bd34d0 )
by Colin
55:03 queued 53:44
created

MentionExtension::register()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 8.6666
c 0
b 0
f 0
cc 7
nc 10
nop 1
crap 7
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\Mention;
13
14
use League\CommonMark\ConfigurableEnvironmentInterface;
15
use League\CommonMark\Extension\ExtensionInterface;
16
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
17
18
final class MentionExtension implements ExtensionInterface
19
{
20 15
    public function register(ConfigurableEnvironmentInterface $environment)
21
    {
22 15
        $mentions = $environment->getConfig('mentions', []);
23 15
        foreach ($mentions as $name => $mention) {
24 12
            foreach (['symbol', 'regex', 'generator'] as $key) {
25 12
                if (empty($mention[$key])) {
26 4
                    throw new \RuntimeException("Missing \"$key\" from MentionParser configuration");
27
                }
28
            }
29 12
            if ($mention['generator'] instanceof MentionGeneratorInterface) {
30 3
                $environment->addInlineParser(new MentionParser($mention['symbol'], $mention['regex'], $mention['generator']));
31 9
            } elseif (is_string($mention['generator'])) {
32 3
                $environment->addInlineParser(MentionParser::createWithStringTemplate($mention['symbol'], $mention['regex'], $mention['generator']));
33 6
            } elseif (is_callable($mention['generator'])) {
34 3
                $environment->addInlineParser(MentionParser::createWithCallback($mention['symbol'], $mention['regex'], $mention['generator']));
35
            } else {
36 6
                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));
37
            }
38
        }
39 12
    }
40
}
41