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

AbstractParse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 8 1
A __construct() 0 11 1
A parsePageConfiguration() 0 12 2
A renderPages() 0 16 3
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