Passed
Push — master ( 15a8bd...59db0f )
by Brent
10:34 queued 07:57
created

MarkdownParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 17 3
1
<?php
2
3
namespace Brendt\Stitcher\Parser;
4
5
use Brendt\Stitcher\Config;
6
use Parsedown;
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * The MarkDownParser takes a path to a markdown file and will parse it to HTML.
11
 */
12
class MarkdownParser implements Parser
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $srcDir;
19
20
    /**
21
     * MarkdownParser constructor.
22
     *
23
     * @param string $srcDir
24
     */
25
    public function __construct(string $srcDir) {
26
        $this->srcDir = $srcDir;
27
    }
28
29
    /**
30
     * @param string $path
31
     *
32
     * @return string
33
     */
34
    public function parse($path) {
35
        if (!strpos($path, '.md')) {
36
            $path .= '.md';
37
        }
38
39
        $files = Finder::create()->files()->in($this->srcDir)->path($path)->getIterator();
40
        $files->rewind();
41
        $markdownFile = $files->current();
42
43
        if (!$markdownFile) {
44
            return '';
45
        }
46
47
        $html = Parsedown::instance()->parse($markdownFile->getContents());
48
49
        return $html;
50
    }
51
52
}
53