Passed
Push — develop ( ec1e42...80559e )
by Brent
03:22
created

AbstractParse::parsePageConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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