Completed
Push — master ( 47a956...6a7dae )
by Stéphane
02:49
created

ContentType::doConversion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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
    protected $converter;
16
17
    public function __construct(Config $config)
18
    {
19
        $this->config = $config;
20
        $this->converter = new CommonMarkConverter(['daux' => $config]);
21
    }
22
23
    /**
24
     * @return string[]
25
     */
26
    public function getExtensions()
27
    {
28
        return ['md', 'markdown'];
29
    }
30
31
    protected function doConversion($raw)
32
    {
33
        Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE);
34
        return $this->converter->convertToHtml($raw);
35
    }
36
37
    public function convert($raw, Content $node)
38
    {
39
        $this->config->setCurrentPage($node);
40
41
        if (!$this->config->canCache()) {
42
            return $this->doConversion($raw);
43
        }
44
45
        $cacheKey = $this->config->getCacheKey() . sha1($raw);
46
47
        $payload = Cache::get($cacheKey);
48
49
        if ($payload) {
50
            Daux::writeln("Using cached version", OutputInterface::VERBOSITY_VERBOSE);
51
        } else {
52
            Daux::writeln("Not found in the cache, generating...", OutputInterface::VERBOSITY_VERBOSE);
53
            $payload = $this->doConversion($raw);
54
            Cache::put($cacheKey, $payload);
55
        } 
56
57
        return $payload;
58
    }
59
}
60