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

AbstractParse::renderPages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 2.004
rs 9.4285
c 0
b 0
f 0
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