Completed
Push — config-virtualpages ( 8f9147...7621fd )
by Arnaud
02:29 queued 12s
created

VirtualPages::collectPagesConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Generator;
10
11
use Cecil\Collection\Page\Page;
12
use Cecil\Collection\Page\Type;
13
use Cecil\Exception\Exception;
14
15
/**
16
 * Class VirtualPages.
17
 */
18
class VirtualPages extends AbstractGenerator implements GeneratorInterface
19
{
20
    protected $configKey = 'virtualpages';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function generate(): void
26
    {
27
        $pagesConfig = $this->collectPagesConfig($this->configKey);
28
29
        if (!is_array($pagesConfig)) {
30
            throw new Exception(sprintf('Config key "%s" is not set.', $this->configKey));
31
        }
32
33
        foreach ($pagesConfig as $frontmatter) {
34
            if (isset($frontmatter['published']) && $frontmatter['published'] === false) {
35
                continue;
36
            }
37
            if (!array_key_exists('path', $frontmatter)) {
38
                throw new Exception(sprintf('Each pages in "%s" config\'s section must have a "path".', $this->configKey));
39
            }
40
            $page = (new Page(Page::slugify($frontmatter['path'])))
41
                ->setPath(Page::slugify($frontmatter['path']))
42
                ->setType(Type::PAGE);
43
            $page->setVariables($frontmatter);
44
            $this->generatedPages->add($page);
45
        }
46
    }
47
48
    /**
49
     * Collect "virtual pages" config.
50
     *
51
     * @param string $configKey
52
     *
53
     * @return array|null
54
     */
55
    private function collectPagesConfig(string $configKey): ?array
56
    {
57
        return $this->config->get($configKey);
58
    }
59
}
60