Passed
Push — master ( 727c5c...688de1 )
by Stéphane
02:20
created

ContentType::getConverter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
2
3
use Symfony\Component\Console\Output\OutputInterface;
4
use Todaymade\Daux\Cache;
5
use Todaymade\Daux\Config;
6
use Todaymade\Daux\Daux;
7
use Todaymade\Daux\Tree\Content;
8
9
class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
10
{
11
    /** @var Config */
12
    protected $config;
13
14
    /** @var CommonMarkConverter */
15
    private $converter;
16
17 1
    public function __construct(Config $config)
18
    {
19 1
        $this->config = $config;
20 1
    }
21
22
    protected function createConverter() {
23
        return new CommonMarkConverter(['daux' => $this->config]);
24
    }
25
26
    protected function getConverter() {
27
        if (!$this->converter) {
28
            $this->converter = $this->createConverter();
29
        }
30
31
        return $this->converter;
32
    }
33
34
    /**
35
     * @return string[]
36
     */
37 1
    public function getExtensions()
38
    {
39 1
        return ['md', 'markdown'];
40
    }
41
42
    protected function doConversion($raw)
43
    {
44
        Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE);
45
        return $this->getConverter()->convertToHtml($raw);
46
    }
47
48
    public function convert($raw, Content $node)
49
    {
50
        $this->config->setCurrentPage($node);
51
52
        if (!$this->config->canCache()) {
53
            return $this->doConversion($raw);
54
        }
55
56
        // TODO :: add daux version to cache key
57
        $cacheKey = $this->config->getCacheKey() . sha1($raw);
58
59
        $payload = Cache::get($cacheKey);
60
61
        if ($payload) {
62
            Daux::writeln("Using cached version", OutputInterface::VERBOSITY_VERBOSE);
63
        } else {
64
            Daux::writeln("Not found in the cache, generating...", OutputInterface::VERBOSITY_VERBOSE);
65
            $payload = $this->doConversion($raw);
66
            Cache::put($cacheKey, $payload);
67
        } 
68
69
        return $payload;
70
    }
71
}
72