Completed
Pull Request — master (#11)
by Tomáš
04:45 queued 25s
created

MarkdownConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
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 Symplify\PHP7_Sculpin\Markdown;
13
14
use Michelf\MarkdownExtra;
15
use Symplify\PHP7_Sculpin\Converter\SourceConverterContext;
16
use Symplify\PHP7_Sculpin\Event\ConvertEvent;
17
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
18
use Symplify\PHP7_Sculpin\Source\SourceInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symplify\PHP7_Sculpin\TwigBundle\SculpinTwigBundle;
21
22
final class MarkdownConverter
23
{
24
    /**
25
     * @var string
26
     */
27
    const NAME = 'markdown';
28
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34
    /**
35
     * @var MarkdownExtra
36
     */
37
    private $markdown;
38
39
    public function __construct(EventDispatcherInterface $eventDispatcher, MarkdownExtra $markdown)
40
    {
41
        $this->eventDispatcher = $eventDispatcher;
42
        $this->markdown = $markdown;
43
    }
44
45
    public function convert(SourceConverterContext $converterContext)
46
    {
47
        $converterContext->setContent(
48
            $this->markdown->transform(
49
                $converterContext->content()
50
            )
51
        );
52
    }
53
54
    public function convertSource(SourceInterface $source)
55
    {
56
        if (!fnmatch("*.md", $source->filename())) {
57
            return;
58
        }
59
60
        $this->eventDispatcher->dispatch(
61
            SculpinEvents::BEFORE_CONVERT,
62
            new ConvertEvent($source, MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)
63
        );
64
65
        $this->convert(new SourceConverterContext($source));
66
67
        $this->eventDispatcher->dispatch(
68
            SculpinEvents::AFTER_CONVERT,
69
            new ConvertEvent($source, MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)
70
        );
71
    }
72
}
73