MarkdownRenderer::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Slim Framework (http://slimframework.com).
4
 *
5
 * @link      https://github.com/DavidePastore/Markdown-View
6
 */
7
8
namespace DavidePastore\Slim\Views;
9
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Class MarkdownRenderer.
14
 */
15
class MarkdownRenderer
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $templatePath;
21
22
    /**
23
     * The Parsedown instance.
24
     *
25
     * @var \Parsedown
26
     */
27
    protected $parsedown;
28
29
    /**
30
     * SlimRenderer constructor.
31
     *
32
     * @param string     $templatePath
33
     * @param \Parsedown $parsedown    The instance to use to parse.
34
     */
35 7
    public function __construct($templatePath = '', $parsedown = null)
36
    {
37 7
        $this->templatePath = $templatePath;
38 7
        if ($parsedown === null) {
39 6
            $this->parsedown = new \Parsedown();
40
        } else {
41 1
            $this->parsedown = $parsedown;
42
        }
43 7
    }
44
45
    /**
46
     * Render a template.
47
     *
48
     * throws RuntimeException if $templatePath . $template does not exist
49
     *
50
     * @param ResponseInterface $response
51
     * @param string            $template
52
     *
53
     * @throws \InvalidArgumentException
54
     * @throws \RuntimeException
55
     *
56
     * @return ResponseInterface
57
     */
58 3
    public function render(ResponseInterface $response, $template)
59
    {
60 3
        $output = $this->fetch($template);
61
62 2
        $response->getBody()->write($output);
63
64 2
        return $response;
65
    }
66
67
    /**
68
     * Get the template path.
69
     *
70
     * @return string
71
     */
72 2
    public function getTemplatePath()
73
    {
74 2
        return $this->templatePath;
75
    }
76
77
    /**
78
     * Set the template path.
79
     *
80
     * @param string $templatePath
81
     */
82 1
    public function setTemplatePath($templatePath)
83
    {
84 1
        $this->templatePath = $templatePath;
85 1
    }
86
87
    /**
88
     * Get the parsedown.
89
     *
90
     * @return \Parsedown
91
     */
92 2
    public function getParsedown()
93
    {
94 2
        return $this->parsedown;
95
    }
96
97
    /**
98
     * Set the parsedown.
99
     *
100
     * @param \Parsedown
101
     */
102 1
    public function setParsedown($parsedown)
103
    {
104 1
        $this->parsedown = $parsedown;
105 1
    }
106
107
    /**
108
     * Renders a template and returns the result as a string.
109
     *
110
     * throws RuntimeException if $templatePath . $template does not exist
111
     *
112
     * @param $template
113
     *
114
     * @throws \InvalidArgumentException
115
     * @throws \RuntimeException
116
     *
117
     * @return mixed
118
     */
119 3
    public function fetch($template)
120
    {
121 3
        if (!is_file($this->templatePath.$template)) {
122 1
            throw new \RuntimeException("View cannot render `$template` because the template does not exist");
123
        }
124 2
        $output = $this->parsedown->text(file_get_contents($this->templatePath.$template));
125
126 2
        return $output;
127
    }
128
}
129