Completed
Pull Request — master (#11)
by Tomáš
05:06
created

MarkdownConverter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 51
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A convert() 0 8 1
A convertSource() 0 18 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\Converter;
13
14
use Michelf\MarkdownExtra;
15
use Symplify\PHP7_Sculpin\Event\ConvertEvent;
16
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
17
use Symplify\PHP7_Sculpin\Source\SourceInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symplify\PHP7_Sculpin\TwigBundle\SculpinTwigBundle;
20
21
final class MarkdownConverter
22
{
23
    /**
24
     * @var string
25
     */
26
    const NAME = 'markdown';
27
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    private $eventDispatcher;
32
33
    /**
34
     * @var MarkdownExtra
35
     */
36
    private $markdown;
37
38
    public function __construct(EventDispatcherInterface $eventDispatcher, MarkdownExtra $markdown)
39
    {
40
        $this->eventDispatcher = $eventDispatcher;
41
        $this->markdown = $markdown;
42
    }
43
44
    public function convert(SourceConverterContext $converterContext)
45
    {
46
        $converterContext->setContent(
47
            $this->markdown->transform(
48
                $converterContext->content()
49
            )
50
        );
51
    }
52
53
    public function convertSource(SourceInterface $source)
54
    {
55
        if (!fnmatch("*.md", $source->filename())) {
56
            return;
57
        }
58
59
        $this->eventDispatcher->dispatch(
60
            SculpinEvents::BEFORE_CONVERT,
61
            new ConvertEvent($source, MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)
62
        );
63
64
        $this->convert(new SourceConverterContext($source));
65
66
        $this->eventDispatcher->dispatch(
67
            SculpinEvents::AFTER_CONVERT,
68
            new ConvertEvent($source, MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)
69
        );
70
    }
71
}
72