Passed
Push — latest ( 2ee702...5d397b )
by Colin
02:38 queued 12s
created

FrontMatterExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 2
b 0
f 0
dl 0
loc 23
ccs 6
cts 8
cp 0.75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register() 0 4 1
A getFrontMatterParser() 0 3 1
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\FrontMatter;
15
16
use League\CommonMark\Environment\ConfigurableEnvironmentInterface;
17
use League\CommonMark\Event\DocumentPreParsedEvent;
18
use League\CommonMark\Event\DocumentRenderedEvent;
19
use League\CommonMark\Extension\ExtensionInterface;
20
use League\CommonMark\Extension\FrontMatter\Data\FrontMatterDataParserInterface;
21
use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser;
22
use League\CommonMark\Extension\FrontMatter\Listener\FrontMatterPostRenderListener;
23
use League\CommonMark\Extension\FrontMatter\Listener\FrontMatterPreParser;
24
25
final class FrontMatterExtension implements ExtensionInterface
26
{
27
    /**
28
     * @var FrontMatterParserInterface
29
     *
30
     * @psalm-readonly
31
     */
32
    private $frontMatterParser;
33
34 9
    public function __construct(?FrontMatterDataParserInterface $dataParser = null)
35
    {
36 9
        $this->frontMatterParser = new FrontMatterParser($dataParser ?? new SymfonyYamlFrontMatterParser());
37 9
    }
38
39
    public function getFrontMatterParser(): FrontMatterParserInterface
40
    {
41
        return $this->frontMatterParser;
42
    }
43
44 9
    public function register(ConfigurableEnvironmentInterface $environment): void
45
    {
46 9
        $environment->addEventListener(DocumentPreParsedEvent::class, new FrontMatterPreParser($this->frontMatterParser));
47 9
        $environment->addEventListener(DocumentRenderedEvent::class, new FrontMatterPostRenderListener(), -500);
48 9
    }
49
}
50