Test Failed
Push — develop ( 32ca41...230235 )
by Brent
04:59
created

AbstractParse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 3
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A make() 0 15 1
A addSubTask() 0 6 1
A parsePageConfiguration() 0 12 2
A renderPages() 0 17 2
A executeSubTasks() 0 6 2
1
<?php
2
3
namespace Stitcher\Task;
4
5
use Pageon\Html\SiteMap;
6
use Stitcher\Page\Page;
7
use Stitcher\Page\PageCollection;
8
use Stitcher\Page\PageParser;
9
use Stitcher\Page\PageRenderer;
10
use Stitcher\Task;
11
use Symfony\Component\Filesystem\Filesystem;
12
13
abstract class AbstractParse implements Task
14
{
15
    /** @var string */
16
    protected $publicDirectory;
17
18
    /** @var string */
19
    protected $configurationFile;
20
21
    /** @var PageParser */
22
    protected $pageParser;
23
24
    /** @var PageRenderer */
25
    protected $pageRenderer;
26
27
    /** @var Task[] */
28
    protected $tasks = [];
29
30
    /** @var SiteMap */
31
    protected $siteMap;
32
33
    public function __construct(
34
        string $publicDirectory,
35
        string $configurationFile,
36
        PageParser $pageParser,
37
        PageRenderer $pageRenderer,
38
        SiteMap $siteMap
39
    ) {
40
        $this->publicDirectory = rtrim($publicDirectory, '/');
41
        $this->configurationFile = $configurationFile;
42
        $this->pageParser = $pageParser;
43
        $this->pageRenderer = $pageRenderer;
44
        $this->siteMap = $siteMap;
45
    }
46
47
    public static function make(
48
        string $publicDirectory,
49
        string $configurationFile,
50
        PageParser $pageParser,
51
        PageRenderer $pageRenderer,
52
        SiteMap $siteMap
53
    ) : self {
54
        return new static(
55
            $publicDirectory,
56
            $configurationFile,
57
            $pageParser,
58
            $pageRenderer,
59
            $siteMap
60
        );
61
    }
62
63
    public function addSubTask(Task $task)
64
    {
65
        $this->tasks[] = $task;
66
67
        return $this;
68
    }
69
70
    protected function parsePageConfiguration($config): PageCollection
71
    {
72
        $pages = [];
73
74
        foreach ($config as $pageId => $pageConfiguration) {
75
            $pageConfiguration['id'] = $pageConfiguration['id'] ?? $pageId;
76
77
            $pages += $this->pageParser->parse($pageConfiguration)->toArray();
78
        }
79
80
        return new PageCollection($pages);
81
    }
82
83
    protected function renderPages(PageCollection $pages): void
84
    {
85
        $fs = new Filesystem();
86
87
        $pages->each(function (Page $page, string $pageId) use ($fs) {
88
            $fileName = $pageId === '/' ? 'index' : $pageId;
89
90
            $renderedPage = $this->pageRenderer->render($page);
91
92
            $this->siteMap->addPath($pageId);
93
94
            $fs->dumpFile(
95
                "{$this->publicDirectory}/{$fileName}.html",
96
                $renderedPage
97
            );
98
        });
99
    }
100
101
    protected function executeSubTasks(): void
102
    {
103
        foreach ($this->tasks as $task) {
104
            $task->execute();
105
        }
106
    }
107
}
108