Total Complexity | 40 |
Total Lines | 363 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like Builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Builder implements LoggerAwareInterface |
||
29 | { |
||
30 | const VERSION = '5.x-dev'; |
||
31 | const VERBOSITY_QUIET = -1; |
||
32 | const VERBOSITY_NORMAL = 0; |
||
33 | const VERBOSITY_VERBOSE = 1; |
||
34 | const VERBOSITY_DEBUG = 2; |
||
35 | |||
36 | /** |
||
37 | * @var array Steps processed by build(). |
||
38 | */ |
||
39 | protected $steps = [ |
||
40 | 'Cecil\Step\Themes\Import', |
||
41 | 'Cecil\Step\Content\Load', |
||
42 | 'Cecil\Step\Content\DataLoad', |
||
43 | 'Cecil\Step\StaticFiles\Load', |
||
44 | 'Cecil\Step\Pages\Create', |
||
45 | 'Cecil\Step\Pages\Convert', |
||
46 | 'Cecil\Step\Taxonomies\Create', |
||
47 | 'Cecil\Step\Pages\Generate', |
||
48 | 'Cecil\Step\Menus\Create', |
||
49 | 'Cecil\Step\StaticFiles\Copy', |
||
50 | 'Cecil\Step\Pages\Render', |
||
51 | 'Cecil\Step\Pages\Save', |
||
52 | 'Cecil\Step\PostProcess\Html', |
||
53 | 'Cecil\Step\PostProcess\Css', |
||
54 | 'Cecil\Step\PostProcess\Js', |
||
55 | 'Cecil\Step\PostProcess\Images', |
||
56 | ]; |
||
57 | |||
58 | /** @var Config Configuration. */ |
||
59 | protected $config; |
||
60 | |||
61 | /** @var LoggerInterface Logger. */ |
||
62 | protected $logger; |
||
63 | |||
64 | /** @var bool Debug mode. */ |
||
65 | protected $debug = false; |
||
66 | |||
67 | /** @var array Build options. */ |
||
68 | protected $options; |
||
69 | |||
70 | /** @var Finder Content iterator. */ |
||
71 | protected $content; |
||
72 | |||
73 | /** @var array Data collection. */ |
||
74 | protected $data = []; |
||
75 | |||
76 | /** @var array Static files collection. */ |
||
77 | protected $static = []; |
||
78 | |||
79 | /** @var PagesCollection Pages collection. */ |
||
80 | protected $pages; |
||
81 | |||
82 | /** @var array Menus collection. */ |
||
83 | protected $menus; |
||
84 | |||
85 | /** @var Collection\Taxonomy\Collection Taxonomies collection. */ |
||
86 | protected $taxonomies; |
||
87 | |||
88 | /** @var Renderer\RendererInterface Renderer. */ |
||
89 | protected $renderer; |
||
90 | |||
91 | /** @var GeneratorManager Generators manager. */ |
||
92 | protected $generatorManager; |
||
93 | |||
94 | /** @var string Application version. */ |
||
95 | protected static $version; |
||
96 | |||
97 | /** |
||
98 | * @param Config|array|null $config |
||
99 | * @param LoggerInterface|null $logger |
||
100 | */ |
||
101 | public function __construct($config = null, LoggerInterface $logger = null) |
||
102 | { |
||
103 | // set logger |
||
104 | if ($logger === null) { |
||
105 | $logger = new PrintLogger(self::VERBOSITY_VERBOSE); |
||
106 | } |
||
107 | $this->setLogger($logger); |
||
108 | // set config |
||
109 | $this->setConfig($config)->setSourceDir(null)->setDestinationDir(null); |
||
110 | // debug mode? |
||
111 | if (getenv('CECIL_DEBUG') == 'true' || (bool) $this->getConfig()->get('debug')) { |
||
112 | $this->debug = true; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Creates a new Builder instance. |
||
118 | */ |
||
119 | public static function create(): self |
||
120 | { |
||
121 | $class = new \ReflectionClass(get_called_class()); |
||
122 | |||
123 | return $class->newInstanceArgs(func_get_args()); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Builds a new website. |
||
128 | */ |
||
129 | public function build(array $options): self |
||
130 | { |
||
131 | // set start script time |
||
132 | $startTime = microtime(true); |
||
133 | |||
134 | // checks baseurl |
||
135 | if (empty(trim((string) $this->config->get('baseurl'), '/'))) { |
||
136 | $this->getLogger()->error("The 'baseurl' configuration key is required in production (e.g.: 'baseurl: https://example.com/')."); |
||
137 | } |
||
138 | |||
139 | // prepare options |
||
140 | $this->options = array_merge([ |
||
141 | 'drafts' => false, // build drafts or not |
||
142 | 'dry-run' => false, // if dry-run is true, generated files are not saved |
||
143 | 'page' => '', // specific page to build |
||
144 | ], $options); |
||
145 | |||
146 | // process each step |
||
147 | $steps = []; |
||
148 | // init... |
||
149 | foreach ($this->steps as $step) { |
||
150 | /** @var Step\StepInterface $stepObject */ |
||
151 | $stepObject = new $step($this); |
||
152 | $stepObject->init($this->options); |
||
153 | if ($stepObject->canProcess()) { |
||
154 | $steps[] = $stepObject; |
||
155 | } |
||
156 | } |
||
157 | // ...and process! |
||
158 | $stepNumber = 0; |
||
159 | $stepsTotal = count($steps); |
||
160 | foreach ($steps as $step) { |
||
161 | $stepNumber++; |
||
162 | /** @var Step\StepInterface $step */ |
||
163 | $this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]); |
||
164 | $step->process(); |
||
165 | } |
||
166 | |||
167 | // process duration |
||
168 | $message = \sprintf('Built in %ss', round(microtime(true) - $startTime, 2)); |
||
169 | $this->getLogger()->notice($message); |
||
170 | |||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Set configuration. |
||
176 | * |
||
177 | * @param Config|array|null $config |
||
178 | */ |
||
179 | public function setConfig($config): self |
||
180 | { |
||
181 | if (!$config instanceof Config) { |
||
182 | $config = new Config($config); |
||
183 | } |
||
184 | if ($this->config !== $config) { |
||
185 | $this->config = $config; |
||
186 | } |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Returns configuration. |
||
193 | */ |
||
194 | public function getConfig(): Config |
||
195 | { |
||
196 | return $this->config; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Config::setSourceDir() alias. |
||
201 | */ |
||
202 | public function setSourceDir(string $sourceDir = null): self |
||
203 | { |
||
204 | $this->config->setSourceDir($sourceDir); |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Config::setDestinationDir() alias. |
||
211 | */ |
||
212 | public function setDestinationDir(string $destinationDir = null): self |
||
213 | { |
||
214 | $this->config->setDestinationDir($destinationDir); |
||
215 | |||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function setLogger(LoggerInterface $logger) |
||
223 | { |
||
224 | $this->logger = $logger; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns the logger instance. |
||
229 | */ |
||
230 | public function getLogger(): LoggerInterface |
||
231 | { |
||
232 | return $this->logger; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Returns debug mode state. |
||
237 | */ |
||
238 | public function isDebug(): bool |
||
239 | { |
||
240 | return $this->debug; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Returns build options. |
||
245 | */ |
||
246 | public function getBuildOptions(): array |
||
247 | { |
||
248 | return $this->options; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Set collected content. |
||
253 | */ |
||
254 | public function setContent(Finder $content): void |
||
255 | { |
||
256 | $this->content = $content; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Returns content. |
||
261 | */ |
||
262 | public function getContent(): ?Finder |
||
263 | { |
||
264 | return $this->content; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Set collected data. |
||
269 | */ |
||
270 | public function setData(array $data): void |
||
271 | { |
||
272 | $this->data = $data; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Returns data collection. |
||
277 | */ |
||
278 | public function getData(): array |
||
279 | { |
||
280 | return $this->data; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Set collected static files. |
||
285 | */ |
||
286 | public function setStatic(array $static): void |
||
287 | { |
||
288 | $this->static = $static; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Returns static files collection. |
||
293 | */ |
||
294 | public function getStatic(): array |
||
295 | { |
||
296 | return $this->static; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Set/update Pages collection. |
||
301 | */ |
||
302 | public function setPages(PagesCollection $pages): void |
||
303 | { |
||
304 | $this->pages = $pages; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Returns pages collection. |
||
309 | */ |
||
310 | public function getPages(): ?PagesCollection |
||
311 | { |
||
312 | return $this->pages; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Set menus collection. |
||
317 | */ |
||
318 | public function setMenus(array $menus): void |
||
319 | { |
||
320 | $this->menus = $menus; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Returns all menus, for a language. |
||
325 | */ |
||
326 | public function getMenus(string $language): Collection\Menu\Collection |
||
327 | { |
||
328 | return $this->menus[$language]; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Set taxonomies collection. |
||
333 | */ |
||
334 | public function setTaxonomies(Collection\Taxonomy\Collection $taxonomies): void |
||
335 | { |
||
336 | $this->taxonomies = $taxonomies; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Returns taxonomies collection. |
||
341 | */ |
||
342 | public function getTaxonomies(): ?Collection\Taxonomy\Collection |
||
343 | { |
||
344 | return $this->taxonomies; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Set renderer object. |
||
349 | */ |
||
350 | public function setRenderer(Renderer\RendererInterface $renderer): void |
||
351 | { |
||
352 | $this->renderer = $renderer; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Returns Renderer object. |
||
357 | */ |
||
358 | public function getRenderer(): Renderer\RendererInterface |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Returns application version. |
||
365 | * |
||
366 | * @throws RuntimeException |
||
367 | */ |
||
368 | public static function getVersion(): string |
||
369 | { |
||
370 | if (!isset(self::$version)) { |
||
371 | $filePath = __DIR__.'/../VERSION'; |
||
372 | if (Plateform::isPhar()) { |
||
373 | $filePath = Plateform::getPharPath().'/VERSION'; |
||
374 | } |
||
375 | |||
391 | } |
||
392 | } |
||
393 |