Passed
Push — develop ( c5beb1...7d7ea1 )
by Brent
06:28 queued 02:06
created

AbstractParse::renderPages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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