Homepage   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 31
c 0
b 0
f 0
dl 0
loc 55
rs 10

1 Method

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