Passed
Push — master ( 59db0f...9fb939 )
by Brent
04:37
created

MarkdownParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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