Passed
Push — master ( 1b9271...7ca014 )
by Johannes
02:14
created

ContentParser::sortByDate()   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 1
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
                    'all' => [],
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
                $entryDate = $entry->getCreated()->format('Y-m-d\TH:i:s');
93
                $config['content_types']['blogEntry']['all'][$entryDate] = $entry->getId();
94
                foreach ($entry->getTags() as $tag) {
95
                    $config['content_types']['blogEntry']['tags'][$tag->getId()][$entryDate] = $entry->getId();
96
                }
97
            }
98
        }
99
        $this->sortByDate($config['content_types']['blogEntry']['all']);
100
        ksort($config['content_types']['blogEntry']['tags']);
101
        uasort($config['content_types']['blogEntry']['tags'], array($this, 'sortByEntryCountDescending'));
102
        foreach ($config['content_types']['blogEntry']['tags'] as $tag => $entries) {
103
            $this->sortByDate($config['content_types']['blogEntry']['tags'][$tag]);
104
        }
105
106
        return $config;
107
    }
108
109
    /**
110
     * Get markdown file iterator
111
     *
112
     * @return MarkdownFileIterator
113
     */
114
    public function getMarkdownFileIterator() : MarkdownFileIterator
115
    {
116
        return $this->markdownFileIterator;
117
    }
118
119
    /**
120
     * Get content creator
121
     *
122
     * @return ContentCreator
123
     */
124
    public function getContentCreator() : ContentCreator
125
    {
126
        return $this->contentCreator;
127
    }
128
129
    /**
130
     * Get markdown document parser
131
     *
132
     * @return MarkdownDocumentParser
133
     */
134
    public function getMarkdownDocumentParser() : MarkdownDocumentParser
135
    {
136
        return $this->markdownDocumentParser;
137
    }
138
139
    /**
140
     * Get markdown option
141
     *
142
     * @return array
143
     */
144
    public function getMarkdownOptions() : array
145
    {
146
        return $this->markdownOptions;
147
    }
148
149
    /**
150
     * Sort by date
151
     *
152
     * @param array $config
153
     */
154
    private function sortByDate(array $config)
155
    {
156
        krsort($config);
157
    }
158
159
    /**
160
     * Get relative path
161
     *
162
     * @param \SplFileInfo $file
163
     * @return string
164
     */
165
    private function getRelativePath(\SplFileInfo $file) : string
166
    {
167
        $path = $file->getPathname();
168
        return substr($path, strrpos($path, $this->contentDir) + strlen($this->contentDir) + 1);
169
    }
170
171
    /**
172
     * Sort by entry count descending
173
     *
174
     * @param array $a
175
     * @param array $b
176
     * @return number
177
     */
178
    private function sortByEntryCountDescending(array $a, array $b)
179
    {
180
        if (count($a) == count($b)) {
181
            return 0;
182
        }
183
        return (count($a) >count($b)) ? -1 : 1;
184
    }
185
}