Test Failed
Push — master ( c81326...794fe0 )
by Johannes
03:04
created

ContentParser::__invoke()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 4
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\Parser;
11
12
use Jolicht\MarkdownCms\Iterator\MarkdownFileIterator;
13
use Jolicht\MarkdownCms\ContentType\ContentCreator;
14
use Jolicht\MarkdownCms\Markdown\MarkdownDocumentParser;
15
16
class ContentParser
17
{
18
    /**
19
     * Markdown file iterator
20
     *
21
     * @var MarkdownFileIterator
22
     */
23
    private $markdownFileIterator;
24
25
    /**
26
     * Content creator
27
     *
28
     * @var ContentCreator
29
     */
30
    private $contentCreator;
31
32
    /**
33
     * Markdown document parser
34
     *
35
     * @var MarkdownDocumentParser
36
     */
37
    private $markdownDocumentParser;
38
39
    /**
40
     * Markdown options
41
     *
42
     * @var array
43
     */
44
    private $markdownOptions;
45
46
    /**
47
     * Content path
48
     *
49
     * @var string
50
     */
51
    private $contentDir;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param MarkdownFileIterator $markdownFileIterator
57
     * @param ContentCreator $contentCreator
58
     * @param MarkdownDocumentParser $markdownDocumentParser
59
     */
60
    public function __construct(MarkdownFileIterator $markdownFileIterator, ContentCreator $contentCreator,
61
        MarkdownDocumentParser $markdownDocumentParser, array $markdownOptions)
62
    {
63
        $this->markdownFileIterator = $markdownFileIterator;
64
        $this->contentCreator = $contentCreator;
65
        $this->markdownDocumentParser = $markdownDocumentParser;
66
        $this->markdownOptions = $markdownOptions;
67
        $this->contentDir = $markdownOptions['content_dir'];
68
    }
69
70
    public function __invoke()
71
    {
72
73
        $config = [
74
            'src' => [],
75
            'content_types' => [
76
                'blogEntry' => [
77
                    'byCreated' => [],
78
                    'tags' => [],
79
                ],
80
            ]
81
        ];
82
        $markdownDocumentParser = $this->getMarkdownDocumentParser();
83
        $contentCreator = $this->getContentCreator();
84
        foreach ($this->markdownFileIterator as $file) {
85
            $documentContent = file_get_contents($file->getPathname());
86
87
            $markdownDocument = $markdownDocumentParser($documentContent);
88
            $entry = $contentCreator($markdownDocument);
89
            $id = $entry->getId();
90
            $config['src'][$id] = $this->getRelativePath($file);
91
            if ('blogEntry' == $entry->getType()) {
92
                $config['content_types']['blogEntry']['byCreated'][$entry->getCreated()->format('c')] = $entry->getId();
93
                foreach ($entry->getTags() as $tag) {
94
                    $config['content_types']['blogEntry']['tags'][$tag->getId()][] = $entry->getId();
95
                }
96
            }
97
        }
98
        krsort($config['content_types']['blogEntry']['byCreated']);
99
100
        return $config;
101
    }
102
103
    /**
104
     * Get markdown file iterator
105
     *
106
     * @return MarkdownFileIterator
107
     */
108
    public function getMarkdownFileIterator() : MarkdownFileIterator
109
    {
110
        return $this->markdownFileIterator;
111
    }
112
113
    /**
114
     * Get content creator
115
     *
116
     * @return ContentCreator
117
     */
118
    public function getContentCreator() : ContentCreator
119
    {
120
        return $this->contentCreator;
121
    }
122
123
    /**
124
     * Get markdown document parser
125
     *
126
     * @return MarkdownDocumentParser
127
     */
128
    public function getMarkdownDocumentParser() : MarkdownDocumentParser
129
    {
130
        return $this->markdownDocumentParser;
131
    }
132
133
    /**
134
     * Get markdown option
135
     *
136
     * @return array
137
     */
138
    public function getMarkdownOptions() : array
139
    {
140
        return $this->markdownOptions;
141
    }
142
143
    /**
144
     * Get relative path
145
     *
146
     * @param \SplFileInfo $file
147
     * @return string
148
     */
149
    private function getRelativePath(\SplFileInfo $file) : string
150
    {
151
        $path = $file->getPathname();
152
        return substr($path, strrpos($path, $this->contentDir) + strlen($this->contentDir) + 1);
153
    }
154
}