Completed
Push — master ( d1e0a9...cb6f48 )
by David
11s
created

Page::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 12
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace TheCodingMachine\CMS\StaticRegistry\Loaders;
3
4
use Mni\FrontYAML\Parser;
5
6
class Page
7
{
8
    /**
9
     * @var string|null
10
     */
11
    private $id;
12
    /**
13
     * @var string
14
     */
15
    private $title;
16
    /**
17
     * @var string
18
     */
19
    private $content;
20
    /**
21
     * @var string
22
     */
23
    private $url;
24
    /**
25
     * @var string
26
     */
27
    private $lang;
28
    /**
29
     * @var null|string
30
     */
31
    private $website;
32
    /**
33
     * @var null|string[]
34
     */
35
    private $menu;
36
    /**
37
     * @var int|null
38
     */
39
    private $menuOrder;
40
    /**
41
     * @var null|string
42
     */
43
    private $metaTitle;
44
    /**
45
     * @var null|string
46
     */
47
    private $metaDescription;
48
    /**
49
     * @var null|string
50
     */
51
    private $theme;
52
    /**
53
     * @var null|string
54
     */
55
    private $menuCssClass;
56
57
    /**
58
     * @param string[]|null $menu
59
     */
60
    public function __construct(?string $id, string $title, string $content, string $url, string $lang, ?string $website, ?array $menu, ?int $menuOrder, ?string $menuCssClass, ?string $metaTitle, ?string $metaDescription, ?string $theme)
61
    {
62
        $this->id = $id;
63
        $this->title = $title;
64
        $this->content = $content;
65
        $this->url = $url;
66
        $this->lang = $lang;
67
        $this->website = $website;
68
        $this->menu = $menu;
69
        $this->menuOrder = $menuOrder;
70
        $this->menuCssClass = $menuCssClass;
71
        $this->metaTitle = $metaTitle;
72
        $this->metaDescription = $metaDescription;
73
        $this->theme = $theme;
74
    }
75
76
    public static function fromFile(string $file): self
77
    {
78
        if (!is_readable($file)) {
79
            throw new UnableToLoadFileException('Cannot read file '.$file);
80
        }
81
82
        $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
83
84
        switch ($extension) {
85
            case 'md':
86
                $parseMarkDown = true;
87
                break;
88
            case 'html':
89
                $parseMarkDown = false;
90
                break;
91
            default:
92
                throw new InvalidExtensionException(sprintf('Invalid extension for block %s. Valid extensions are .md and .html', $file));
93
        }
94
95
        $parser = new Parser();
96
97
        $document = $parser->parse(file_get_contents($file), $parseMarkDown);
98
99
        $yaml = $document->getYAML();
100
101
        $compulsoryFields = ['title', 'url', 'lang'];
102
103
        foreach ($compulsoryFields as $field) {
104
            if (!isset($yaml[$field])) {
105
                throw new UnableToLoadFileException('Missing field '.$field.' in YAML front matter of file '.$file);
106
            }
107
        }
108
109
        return new self(
110
            $yaml['id'] ?? null,
111
            $yaml['title'],
112
            $document->getContent(),
113
            '/'.ltrim($yaml['url'], '/'),
114
            $yaml['lang'],
115
            $yaml['website'] ?? null,
116
            isset($yaml['menu']) ? array_map('trim', explode('/', $yaml['menu'])) : null,
117
            $yaml['menu_order'] ?? null,
118
            $yaml['menu_css_class'] ?? null,
119
            $yaml['meta_title'] ?? null,
120
            $yaml['meta_description'] ?? null,
121
            $yaml['theme'] ?? null
122
        );
123
    }
124
125
    /**
126
     * @return null|string
127
     */
128
    public function getId(): ?string
129
    {
130
        return $this->id;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getTitle(): string
137
    {
138
        return $this->title;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getContent(): string
145
    {
146
        return $this->content;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getUrl(): string
153
    {
154
        return $this->url;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getLang(): string
161
    {
162
        return $this->lang;
163
    }
164
165
    /**
166
     * @return string|null
167
     */
168
    public function getWebsite(): ?string
169
    {
170
        return $this->website;
171
    }
172
173
    /**
174
     * @return null|string[]
175
     */
176
    public function getMenu(): ?array
177
    {
178
        return $this->menu;
179
    }
180
181
    /**
182
     * @return int|null
183
     */
184
    public function getMenuOrder(): ?int
185
    {
186
        return $this->menuOrder;
187
    }
188
189
    /**
190
     * @return null|string
191
     */
192
    public function getMenuCssClass(): ?string
193
    {
194
        return $this->menuCssClass;
195
    }
196
197
    /**
198
     * @return null|string
199
     */
200
    public function getMetaTitle(): ?string
201
    {
202
        return $this->metaTitle;
203
    }
204
205
    /**
206
     * @return null|string
207
     */
208
    public function getMetaDescription(): ?string
209
    {
210
        return $this->metaDescription;
211
    }
212
213
    /**
214
     * @return null|string
215
     */
216
    public function getTheme(): ?string
217
    {
218
        return $this->theme;
219
    }
220
}
221