Passed
Push — develop ( 388ba5...5ea0af )
by Brent
06:57 queued 58s
created

AbstractParse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

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 18 2
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
        $path = File::path();
65 10
        $pool = Pool::create()
66 10
            ->autoload(__DIR__ . '/../../../vendor/autoload.php');
67
68
        /**
69
         * @var string $pageId
70
         * @var Page   $page
71
         */
72 10
        foreach ($pages as $pageId => $page) {
73 10
            $pool[] = async(function () use ($path, $pageId, $page) {
74
                RenderPage::execute($path, $pageId, $page);
75 10
            });
76
        }
77
78 10
        await($pool);
79 10
    }
80
}
81