Passed
Push — i18n ( ac686d...882712 )
by Arnaud
04:27 queued 11s
created

VirtualPages::generate()   C

Complexity

Conditions 12
Paths 15

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 12.9955

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
c 3
b 0
f 0
nc 15
nop 0
dl 0
loc 37
ccs 17
cts 21
cp 0.8095
crap 12.9955
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Generator;
12
13
use Cecil\Collection\Page\Page;
14
use Cecil\Collection\Page\Type;
15
use Cecil\Exception\Exception;
16
17
/**
18
 * Class Generator\VirtualPages.
19
 */
20
class VirtualPages extends AbstractGenerator implements GeneratorInterface
21
{
22
    protected $configKey = 'virtualpages';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function generate(): void
28
    {
29 1
        $pagesConfig = $this->collectPagesFromConfig($this->configKey);
30
31 1
        if (!$pagesConfig) {
32
            return;
33
        }
34
35 1
        foreach ($pagesConfig as $frontmatter) {
36 1
            if (isset($frontmatter['published']) && $frontmatter['published'] === false) {
37 1
                continue;
38
            }
39 1
            if (!array_key_exists('path', $frontmatter)) {
40
                throw new Exception(sprintf(
41
                    'Each pages in "%s" config\'s section must have a "path".',
42
                    $this->configKey
43
                ));
44
            }
45 1
            $path = Page::slugify($frontmatter['path']);
46 1
            foreach ($this->config->getLanguages() as $language) {
47
                $id = !empty($path) ? $path : 'index';
48 1
                if ($language['code'] !== $this->config->getLanguageDefault()) {
49 1
                    $id .= '.'.$language['code'];
50
                    if (isset($frontmatter['multilingual']) && $frontmatter['multilingual'] === false) {
51 1
                        continue;
52 1
                    }
53 1
                }
54 1
                if ($this->builder->getPages()->has($id)) {
55 1
                    continue;
56
                }
57 1
                $page = (new Page($id))
58
                    ->setPath($path)
59
                    ->setType(Type::PAGE)
60
                    ->setVariable('language', $language['code'])
61
                    ->setVariable('langref', $path);
62
                $page->setVariables($frontmatter);
63
                $this->generatedPages->add($page);
64
            }
65
        }
66 1
    }
67
68 1
    /**
69
     * Collects virtual pages configuration.
70
     *
71
     * @param string $configKey
72
     *
73
     * @return array|null
74
     */
75
    private function collectPagesFromConfig(string $configKey): ?array
76
    {
77
        return $this->config->get($configKey);
78
    }
79
}
80