Completed
Push — master ( 1daec9...23ac06 )
by Johannes
03:41
created

SingleBlogEntryAction::getMarkdownDocumentParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Lichtenwallner  (https://lichtenwallner.at)
4
 *
5
 * @see https://github.com/jolicht/markdown-cms for the canonical source repository
6
 * @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT
7
 * @copyright Copyright (c) Johannes Lichtenwallner
8
 */
9
declare(strict_types = 1);
10
namespace Jolicht\MarkdownCms\Action;
11
12
use Jolicht\MarkdownCms\Markdown\MarkdownDocumentParser;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Zend\Expressive\Template\TemplateRendererInterface;
16
use Jolicht\MarkdownCms\ContentType\ContentCreator;
17
use Zend\Diactoros\Response\HtmlResponse;
18
19
class SingleBlogEntryAction
20
{
21
    /**
22
     * Template renderer
23
     *
24
     * @var TemplateRendererInterface
25
     */
26
    private $templateRenderer;
27
28
    /**
29
     * Content creator
30
     *
31
     * @var ContentCreator
32
     */
33
    private $contentCreator;
34
35
    /**
36
     * Markdown component parser
37
     *
38
     * @var MarkdownDocumentParser
39
     */
40
    private $markdownDocumentParser;
41
42
    /**
43
     * Markdown cms config
44
     *
45
     * @var array
46
     */
47
    private $markdownCmsConfig;
48
49
    /**
50
     * Constructor
51
     *
52
     * @param TemplateRendererInterface $templateRenderer
53
     * @param ContentCreator $contentCreator
54
     * @param MarkdownDocumentParser $markdownDocumentParser
55
     * @param array $markdownCmsConfig
56
     */
57
    public function __construct(TemplateRendererInterface $templateRenderer, ContentCreator $contentCreator,
58
        MarkdownDocumentParser $markdownDocumentParser, array $markdownCmsConfig)
59
    {
60
        $this->templateRenderer = $templateRenderer;
61
        $this->contentCreator = $contentCreator;
62
        $this->markdownDocumentParser = $markdownDocumentParser;
63
        $this->markdownCmsConfig = $markdownCmsConfig;
64
    }
65
66
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
67
    {
68
69
        $options = $this->markdownCmsConfig['options'];
70
        $config = $this->markdownCmsConfig['content'];
71
        $id = $request->getAttribute('id');
72
73
        $entry = ($this->contentCreator)(
74
            ($this->markdownDocumentParser)(
75
                file_get_contents($options['content_dir'] . '/' .  $config['src'][$id])
76
            )
77
        );
78
79
        $serverParams = $request->getServerParams();
80
81
        $data = [
82
            'entry' => $entry,
83
            'chanonical_url' => $serverParams['REQUEST_SCHEME'] . '://' . $serverParams['SERVER_NAME'] .
84
            '/blog.' . $id . '.html'
85
        ];
86
87
        return new HtmlResponse($this->templateRenderer->render($entry->getTemplate(), $data));
88
    }
89
90
    /**
91
     * Get template renderer
92
     *
93
     * @return TemplateRendererInterface
94
     */
95
    public function getTemplateRenderer() : TemplateRendererInterface
96
    {
97
        return $this->templateRenderer;
98
    }
99
100
    /**
101
     * Get content creator
102
     *
103
     * @return ContentCreator
104
     */
105
    public function getContentCreator() : ContentCreator
106
    {
107
        return $this->contentCreator;
108
    }
109
110
    /**
111
     * Get markdown document parser
112
     *
113
     * @return MarkdownDocumentParser
114
     */
115
    public function getMarkdownDocumentParser() : MarkdownDocumentParser
116
    {
117
        return $this->markdownDocumentParser;
118
    }
119
120
    /**
121
     * Get markdown cms config
122
     *
123
     * @return array
124
     */
125
    public function getMarkdownCmsConfig() : array
126
    {
127
        return $this->markdownCmsConfig;
128
    }
129
130
}