Passed
Push — v2 ( aaaa8e...a625ff )
by Brent
03:52
created

Parse::parsePageConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Stitcher\Command;
4
5
use Stitcher\Command;
6
use Stitcher\File;
7
use Stitcher\Page\Page;
8
use Stitcher\Page\PageParser;
9
use Stitcher\Page\PageRenderer;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\Yaml\Yaml;
12
13
class Parse implements Command
14
{
15
    private $outputDirectory;
16
    private $configurationFile;
17
    private $pageParser;
18
    private $pageRenderer;
19
20
    public function __construct(
21
        string $outputDirectory,
22
        string $configurationFile,
23
        PageParser $pageParser,
24
        PageRenderer $pageRenderer
25
    ) {
26
        $this->outputDirectory = rtrim($outputDirectory, '/');
27
        $this->configurationFile = $configurationFile;
28
        $this->pageParser = $pageParser;
29
        $this->pageRenderer = $pageRenderer;
30
    }
31
32
    public static function make(
33
        string $outputDirectory,
34
        string $configurationFile,
35
        PageParser $pageParser,
36
        PageRenderer $pageRenderer
37
    ): Parse {
38
        return new self($outputDirectory, $configurationFile, $pageParser, $pageRenderer);
39
    }
40
41
    public function execute(): void
42
    {
43
        $parsedConfiguration = Yaml::parse(File::read($this->configurationFile));
44
45
        $pages = $this->parsePageConfiguration($parsedConfiguration);
46
47
        $this->renderPages($pages);
48
    }
49
50
    protected function parsePageConfiguration($config): array
51
    {
52
        $pages = [];
53
54
        foreach ($config as $pageId => $pageConfiguration) {
55
            $pageConfiguration['id'] = $pageConfiguration['id'] ?? $pageId;
56
57
            $pages += $this->pageParser->parse($pageConfiguration)->toArray();
58
        }
59
60
        return $pages;
61
    }
62
63
    protected function renderPages($pages): void
64
    {
65
        $fs = new Filesystem();
66
67
        /**
68
         * @var string $pageId
69
         * @var Page   $page
70
         */
71
        foreach ($pages as $pageId => $page) {
72
            $fileName = $pageId === '/' ? 'index' : $pageId;
73
            $renderedPage = $this->pageRenderer->render($page);
74
75
            $fs->dumpFile("{$this->outputDirectory}/{$fileName}.html", $renderedPage);
76
        }
77
    }
78
}
79