Passed
Push — master ( 58b566...3d7011 )
by Arnaud
05:04
created

Homepage::generate()   C

Complexity

Conditions 16
Paths 97

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 16.0759

Importance

Changes 0
Metric Value
cc 16
eloc 28
c 0
b 0
f 0
nc 97
nop 0
dl 0
loc 47
ccs 28
cts 30
cp 0.9333
crap 16.0759
rs 5.5666

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
/**
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 1
    public function generate(): void
35
    {
36 1
        foreach ($this->config->getLanguages() as $lang) {
37 1
            $language = $lang['code'];
38 1
            $pageId = 'index';
39 1
            if ($language != $this->config->getLanguageDefault()) {
40 1
                $pageId = "$language/$pageId";
41
            }
42
            // creates a new index page...
43 1
            $page = (new Page($pageId))->setPath('')->setVariable('title', 'Home');
44
            // ... clones it if already exists
45 1
            if ($this->builder->getPages()->has($pageId)) {
46 1
                $page = clone $this->builder->getPages()->get($pageId);
47
            }
48
            /** @var \Cecil\Collection\Page\Page $page */
49 1
            $page->setType(Type::HOMEPAGE->value);
50
            // collects all pages
51 1
            $pages = $this->builder->getPages()->filter(function (Page $page) use ($language) {
52 1
                return $page->getType() == Type::PAGE->value
53 1
                    && $page->getVariable('published') === true
54 1
                    && ($page->getVariable('excluded') !== true && $page->getVariable('exclude') !== true)
55 1
                    && $page->isVirtual() === false
56 1
                    && $page->getVariable('language') == $language;
57 1
            });
58
            // or collects pages from a specified section
59
            /** @var \Cecil\Collection\Page\Collection $pages */
60 1
            if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has((string) $page->getVariable('pagesfrom'))) {
61
                $pages = $this->builder->getPages()->get((string) $page->getVariable('pagesfrom'))->getPages();
62
            }
63 1
            if ($pages instanceof \Cecil\Collection\Page\Collection) {
64
                // sorts pages
65 1
                $sortBy = $page->getVariable('sortby') ?? $this->config->get('pages.sortby');
66 1
                $pages = $pages->sortBy($sortBy);
67 1
                $page->setPages($pages);
68 1
                if ($pages->first()) {
69 1
                    $page->setVariable('date', $pages->first()->getVariable('date'));
70
                }
71
            }
72
            // set default "main" menu
73 1
            if (!$page->getVariable('menu')) {
74 1
                $page->setVariable('menu', ['main' => ['weight' => 0]]);
75
            }
76
            // add an alias redirection from the root directory if language path prefix is enabled for the default language
77 1
            if ($language == $this->config->getLanguageDefault() && $this->config->isEnabled('language.prefix')) {
78
                $page->setVariable('alias', '../');
79
            }
80 1
            $this->generatedPages->add($page);
81
        }
82
    }
83
}
84