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

MarkdownConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 54
ccs 0
cts 31
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 21 4
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