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\ContentType; |
11
|
|
|
|
12
|
|
|
use Jolicht\MarkdownCms\Markdown\MarkdownDocumentParser; |
13
|
|
|
|
14
|
|
|
class EntryParser |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Content creator |
18
|
|
|
* |
19
|
|
|
* @var ContentCreator |
20
|
|
|
*/ |
21
|
|
|
private $contentCreator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Markdown document parser |
25
|
|
|
* |
26
|
|
|
* @var MarkdownDocumentParser |
27
|
|
|
*/ |
28
|
|
|
private $mdDocumentParser; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Config |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Content dir |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $contentDir; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param ContentCreator $contentCreator |
48
|
|
|
* @param MarkdownDocumentParser $mdDocumentParser |
49
|
|
|
* @param array $config |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function __construct(ContentCreator $contentCreator, MarkdownDocumentParser $mdDocumentParser, |
|
|
|
|
52
|
|
|
array $config, string $contentDir) |
53
|
|
|
{ |
54
|
|
|
$this->contentCreator = $contentCreator; |
55
|
|
|
$this->mdDocumentParser = $mdDocumentParser; |
56
|
|
|
$this->config = $config; |
57
|
|
|
$this->contentDir = rtrim($contentDir, '/'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get created content type |
62
|
|
|
* |
63
|
|
|
* @param string $id |
64
|
|
|
* @return ContentTypeInterface |
65
|
|
|
*/ |
66
|
|
|
public function __invoke(string $id) : ContentTypeInterface |
67
|
|
|
{ |
68
|
|
|
$config = $this->getConfig(); |
69
|
|
|
$src = $config[$id]['src']; |
70
|
|
|
|
71
|
|
|
return ($this->getContentCreator())( |
72
|
|
|
($this->getMarkdownDocumentParser())(file_get_contents($this->getContentDir() . '/' . $src)) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get content creator |
78
|
|
|
* |
79
|
|
|
* @return ContentCreator |
80
|
|
|
*/ |
81
|
|
|
public function getContentCreator() : ContentCreator |
82
|
|
|
{ |
83
|
|
|
return $this->contentCreator; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get markdown document parser |
88
|
|
|
* |
89
|
|
|
* @return MarkdownDocumentParser |
90
|
|
|
*/ |
91
|
|
|
public function getMarkdownDocumentParser() : MarkdownDocumentParser |
92
|
|
|
{ |
93
|
|
|
return $this->mdDocumentParser; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get config |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function getConfig() : array |
102
|
|
|
{ |
103
|
|
|
return $this->config; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get content dir |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getContentDir() : string |
112
|
|
|
{ |
113
|
|
|
return $this->contentDir; |
114
|
|
|
} |
115
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.