AbstractWriter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A toMarkdown() 0 9 1
A isMarkdownEnabled() 0 3 2
1
<?php
2
3
namespace Fillet\Writer;
4
5
use Fillet\TextUtils\GenerateSlug;
6
7
/**
8
 * Abstract class for setting up a basic output writer
9
 *
10
 * Class AbstractWriter
11
 * @package Fillet\Writer
12
 */
13
abstract class AbstractWriter implements WriterInterface
14
{
15
    use GenerateSlug;
16
17
    /**
18
     * Destination that we should write to
19
     * @var string
20
     */
21
    protected $destinationFolder;
22
23
    /**
24
     * Configuration options for writer
25
     * @var array
26
     */
27
    protected $config;
28
29
    /**
30
     * @param string $destinationFolder Destination that we should write to. Should be the full path ending in /
31
     */
32
    public function __construct($destinationFolder, $config)
33
    {
34
        $this->destinationFolder = $destinationFolder;
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * Converts content from HTML to Markdown via pandoc
40
     *
41
     * @param $content
42
     * @return string
43
     */
44
    protected function toMarkdown($content)
45
    {
46
        $tmpfname = tempnam(sys_get_temp_dir(), 'fillet'); // good
47
        file_put_contents($tmpfname, $content);
48
        $cmd = $this->config['pandoc']['bin'] . ' --no-wrap -f html -t markdown ' . $tmpfname;
49
        $content = shell_exec($cmd);
50
        unlink($tmpfname);
51
        return $content;
52
    }
53
54
    /**
55
     * Tests if markdown conversion is enabled
56
     * @return bool
57
     */
58
    protected function isMarkdownEnabled() {
59
        return isset($this->config['pandoc']['to_markdown']) && true == $this->config['pandoc']['to_markdown'];
60
    }
61
}