Completed
Push — master ( 8c83e8...6a8128 )
by Davide
02:23
created

MarkdownRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Slim Framework (http://slimframework.com).
4
 *
5
 * @link      https://github.com/DavidePastore/Markdown-View
6
 */
7
namespace DavidePastore\Slim\Views;
8
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class MarkdownRenderer.
13
 */
14
class MarkdownRenderer
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $templatePath;
20
21
    /**
22
     * The Parsedown instance.
23
     *
24
     * @var \Parsedown
25
     */
26
    protected $parsedown;
27
28
    /**
29
     * SlimRenderer constructor.
30
     *
31
     * @param string $templatePath
32
     */
33 4
    public function __construct($templatePath = '')
34
    {
35 4
        $this->templatePath = $templatePath;
36 4
        $this->parsedown = new \Parsedown();
37 4
    }
38
39
    /**
40
     * Render a template.
41
     *
42
     * throws RuntimeException if $templatePath . $template does not exist
43
     *
44
     * @param ResponseInterface $response
45
     * @param string            $template
46
     * @param array             $data
47
     *
48
     * @return ResponseInterface
49
     *
50
     * @throws \InvalidArgumentException
51
     * @throws \RuntimeException
52
     */
53 2
    public function render(ResponseInterface $response, $template, array $data = [])
54
    {
55 2
        $output = $this->fetch($template, $data);
56
57 1
        $response->getBody()->write($output);
58
59 1
        return $response;
60
    }
61
62
    /**
63
     * Get the template path.
64
     *
65
     * @return string
66
     */
67 2
    public function getTemplatePath()
68
    {
69 2
        return $this->templatePath;
70
    }
71
72
    /**
73
     * Set the template path.
74
     *
75
     * @param string $templatePath
76
     */
77 1
    public function setTemplatePath($templatePath)
78
    {
79 1
        $this->templatePath = $templatePath;
80 1
    }
81
82
    /**
83
     * Renders a template and returns the result as a string.
84
     *
85
     * throws RuntimeException if $templatePath . $template does not exist
86
     *
87
     * @param $template
88
     * @param array $data
89
     *
90
     * @return mixed
91
     *
92
     * @throws \InvalidArgumentException
93
     * @throws \RuntimeException
94
     */
95 2
    public function fetch($template, array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97 2
        if (!is_file($this->templatePath.$template)) {
98 1
            throw new \RuntimeException("View cannot render `$template` because the template does not exist");
99
        }
100 1
        $output = $this->parsedown->text(file_get_contents($this->templatePath.$template));
101
102 1
        return $output;
103
    }
104
}
105