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

AbstractParse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 3
cbo 5

6 Methods

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