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

MarkdownConverter::convertSource()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 18
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 1
crap 20
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
        $converters = $source->data()->get('converters');
56
        if (!$converters || !is_array($converters)) {
57
            return;
58
        }
59
60
        foreach ($converters as $converter) {
61
            $this->eventDispatcher->dispatch(
62
                SculpinEvents::BEFORE_CONVERT,
63
                new ConvertEvent($source, $converter, SculpinTwigBundle::FORMATTER_NAME)
64
            );
65
66
            $this->convert(new SourceConverterContext($source));
67
68
            $this->eventDispatcher->dispatch(
69
                SculpinEvents::AFTER_CONVERT,
70
                new ConvertEvent($source, $converter, SculpinTwigBundle::FORMATTER_NAME)
71
            );
72
        }
73
    }
74
}
75