Passed
Push — master ( 874a4c...aa75e7 )
by Arnaud
06:27
created

Homepage   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 51
ccs 27
cts 29
cp 0.931
rs 10
wmc 15

1 Method

Rating   Name   Duplication   Size   Complexity  
C generate() 0 46 15
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Generator;
15
16
use Cecil\Collection\Page\Page;
17
use Cecil\Collection\Page\Type;
18
use Cecil\Exception\RuntimeException;
19
20
/**
21
 * Class Generator\Homepage.
22
 */
23
class Homepage extends AbstractGenerator implements GeneratorInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function generate(): void
29
    {
30 1
        foreach ($this->config->getLanguages() as $lang) {
31 1
            $language = $lang['code'];
32 1
            $pageId = 'index';
33 1
            if ($language != $this->config->getLanguageDefault()) {
34 1
                $pageId = "$language/$pageId";
35
            }
36
            // creates a new index page...
37 1
            $page = (new Page($pageId))->setPath('')->setVariable('title', 'Home');
38
            // ... clones it if already exists
39 1
            if ($this->builder->getPages()->has($pageId)) {
40 1
                $page = clone $this->builder->getPages()->get($pageId);
41
            }
42
            /** @var \Cecil\Collection\Page\Page $page */
43 1
            $page->setType(Type::HOMEPAGE->value);
44
            // collects all pages
45 1
            $subPages = $this->builder->getPages()->filter(function (Page $page) use ($language) {
46 1
                return $page->getType() == Type::PAGE->value
47 1
                    && $page->getVariable('published') === true
48 1
                    && $page->isVirtual() === false
49 1
                    && $page->getVariable('exclude') !== true
50 1
                    && $page->getVariable('language') == $language;
51 1
            });
52
            // collects pages of a section
53
            /** @var \Cecil\Collection\Page\Collection $subPages */
54 1
            if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has((string) $page->getVariable('pagesfrom'))) {
55
                $subPages = $this->builder->getPages()->get((string) $page->getVariable('pagesfrom'))->getPages();
56
            }
57 1
            if ($subPages instanceof \Cecil\Collection\Page\Collection) {
58
                // sorts pages
59 1
                $pages = Section::sortSubPages($this->config, $page, $subPages);
60 1
                $page->setPages($pages);
61 1
                if ($pages->first()) {
62 1
                    $page->setVariable('date', $pages->first()->getVariable('date'));
63
                }
64
            }
65
            // set default "main" menu
66 1
            if (!$page->getVariable('menu')) {
67 1
                $page->setVariable('menu', ['main' => ['weight' => 0]]);
68
            }
69
            // add an alias redirection from the root directory if language path prefix is enabled for the default language
70 1
            if ($language == $this->config->getLanguageDefault() && (bool) $this->config->get('language.prefix') === true) {
71
                $page->setVariable('alias', '../');
72
            }
73 1
            $this->generatedPages->add($page);
74
        }
75
    }
76
}
77