Passed
Push — language-path ( 1fb85b...c04822 )
by Arnaud
03:07
created

Homepage::generate()   D

Complexity

Conditions 16
Paths 169

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 17.0743

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 31
c 1
b 0
f 0
nc 169
nop 0
dl 0
loc 52
ccs 26
cts 31
cp 0.8387
crap 17.0743
rs 4.9916

How to fix   Long Method    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
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);
44
            // collects all pages
45 1
            $subPages = $this->builder->getPages()->filter(function (Page $page) use ($language) {
46 1
                return $page->getType() == TYPE::PAGE
47 1
                    && $page->isVirtual() === false
48 1
                    && $page->getVariable('exclude') !== true
49 1
                    && $page->getVariable('language') == $language;
50 1
            });
51
            // collects pages of a section
52
            /** @var \Cecil\Collection\Page\Collection $subPages */
53 1
            if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has((string) $page->getVariable('pagesfrom'))) {
54
                $subPages = $this->builder->getPages()->get((string) $page->getVariable('pagesfrom'))->getPages();
55
            }
56 1
            if ($subPages instanceof \Cecil\Collection\Page\Collection) {
57
                // sorts
58 1
                $pages = $subPages->sortByDate();
59 1
                if ($page->hasVariable('sortby')) {
60
                    $sortMethod = sprintf('sortBy%s', ucfirst((string) $page->getVariable('sortby')));
61
                    if (!method_exists($pages, $sortMethod)) {
62
                        throw new RuntimeException(sprintf('In page "%s" "%s" is not a valid value for "sortby" variable.', $page->getId(), $page->getVariable('sortby')));
63
                    }
64
                    $pages = $pages->$sortMethod();
65
                }
66 1
                $page->setPages($pages);
67 1
                if ($pages->first()) {
68 1
                    $page->setVariable('date', $pages->first()->getVariable('date'));
69
                }
70
            }
71
            // set default "main" menu
72 1
            if (!$page->getVariable('menu')) {
73 1
                $page->setVariable('menu', ['main' => ['weight' => 0]]);
74
            }
75 1
            // add an alias redirection from the root if language path prefix is forced for the default language
76
            if ($language == $this->config->getLanguageDefault() && $this->config->get('language.prefix')) {
77
                $page->setVariable('alias', '../');
78
            }
79
            $this->generatedPages->add($page);
80
        }
81
    }
82
}
83