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