Completed
Push — Assets/Image ( 7a290d...cdd250 )
by Arnaud
16:11 queued 12:51
created

VirtualPages::collectPagesConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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->collectPagesFromConfig($this->configKey);
28
29
        if (!$pagesConfig) {
30
            return;
31
        }
32
        if (!is_array($pagesConfig)) {
0 ignored issues
show
introduced by
The condition is_array($pagesConfig) is always true.
Loading history...
33
            throw new Exception(sprintf('Config key "%s" is not set.', $this->configKey));
34
        }
35
36
        foreach ($pagesConfig as $frontmatter) {
37
            if (isset($frontmatter['published']) && $frontmatter['published'] === false) {
38
                continue;
39
            }
40
            if (!array_key_exists('path', $frontmatter)) {
41
                throw new Exception(sprintf(
42
                    'Each pages in "%s" config\'s section must have a "path".',
43
                    $this->configKey
44
                ));
45
            }
46
            $page = (new Page(Page::slugify($frontmatter['path'])))
47
                ->setPath(Page::slugify($frontmatter['path']))
48
                ->setType(Type::PAGE);
49
            $page->setVariables($frontmatter);
50
            $this->generatedPages->add($page);
51
        }
52
    }
53
54
    /**
55
     * Collect "virtual pages" config.
56
     *
57
     * @param string $configKey
58
     *
59
     * @return array|null
60
     */
61
    private function collectPagesFromConfig(string $configKey): ?array
62
    {
63
        return $this->config->get($configKey);
64
    }
65
}
66