Completed
Push — 1.5 ( 45d47d...375e4c )
by Colin
01:23
created

MentionExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 20 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
                    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 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));
37
            }
38
        }
39 12
    }
40
}
41