Passed
Push — develop ( 667b67...a1f6a6 )
by Brent
03:21
created

AbstractParse::addSubTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 Stitcher\Task;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
abstract class AbstractParse implements Task
12
{
13
    protected $publicDirectory;
14
    protected $configurationFile;
15
    protected $pageParser;
16
    protected $pageRenderer;
17
    /** @var Task[] */
18
    protected $tasks = [];
19
20 13
    public function __construct(
21
        string $publicDirectory,
22
        string $configurationFile,
23
        PageParser $pageParser,
24
        PageRenderer $pageRenderer
25
    ) {
26 13
        $this->publicDirectory = rtrim($publicDirectory, '/');
27 13
        $this->configurationFile = $configurationFile;
28 13
        $this->pageParser = $pageParser;
29 13
        $this->pageRenderer = $pageRenderer;
30 13
    }
31
32
    /**
33
     * @param string                         $publicDirectory
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 $publicDirectory,
42
        string $configurationFile,
43
        PageParser $pageParser,
44
        PageRenderer $pageRenderer
45
    ) {
46 9
        return new static(
47 9
            $publicDirectory,
48 9
            $configurationFile,
49 9
            $pageParser,
50 9
            $pageRenderer
51
        );
52
    }
53
54 4
    public function addSubTask(Task $task)
55
    {
56 4
        $this->tasks[] = $task;
57
58 4
        return $this;
59
    }
60
61 11
    protected function parsePageConfiguration($config): array
62
    {
63 11
        $pages = [];
64
65 11
        foreach ($config as $pageId => $pageConfiguration) {
66 11
            $pageConfiguration['id'] = $pageConfiguration['id'] ?? $pageId;
67
68 11
            $pages += $this->pageParser->parse($pageConfiguration)->toArray();
69
        }
70
71 11
        return $pages;
72
    }
73
74 11
    protected function renderPages($pages): void
75
    {
76 11
        $fs = new Filesystem();
77
78
        /**
79
         * @var string $pageId
80
         * @var Page   $page
81
         */
82 11
        foreach ($pages as $pageId => $page) {
83 11
            $fileName = $pageId === '/' ? 'index' : $pageId;
84
85 11
            $renderedPage = $this->pageRenderer->render($page);
86
87 11
            $fs->dumpFile("{$this->publicDirectory}/{$fileName}.html", $renderedPage);
88
        }
89 11
    }
90
91 11
    protected function executeSubTasks(): void
92
    {
93 11
        foreach ($this->tasks as $task) {
94 2
            $task->execute();
95
        }
96 11
    }
97
}
98