Passed
Push — fix ( 65c360 )
by Arnaud
03:21
created

Homepage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 34
ccs 16
cts 17
cp 0.9412
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 29 6
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
16
/**
17
 * Class Generator\Homepage.
18
 */
19
class Homepage extends AbstractGenerator implements GeneratorInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function generate(): void
25
    {
26
        $subPages = $this->builder->getPages()->filter(function (Page $page) {
27 1
            return $page->getType() == TYPE::PAGE
28 1
                && $page->isVirtual() === false // excludes virtual pages
29 1
                && $page->getVariable('exclude') !== true;
30 1
        });
31
        /** @var \Cecil\Collection\Page\Collection $subPages */
32 1
        $pages = $subPages->sortByDate();
33
34
        // creates a new index page...
35 1
        $page = (new Page('index'))->setPath('')->setVariable('title', 'Home');
36
        // ... clones it if already exists
37 1
        if ($this->builder->getPages()->has('index')) {
38 1
            $page = clone $this->builder->getPages()->get('index');
39
        }
40
        /** @var \Cecil\Collection\Page\Page $page */
41 1
        $page->setType(Type::HOMEPAGE)
42 1
            ->setVariable('pages', $pages);
43 1
        if ($pages->first()) {
44 1
            $page->setVariable('date', $pages->first()->getVariable('date'));
45
        }
46
        // default menu
47 1
        if (!$page->getVariable('menu')) {
48
            $page->setVariable('menu', [
49
                'main' => ['weight' => 0],
50
            ]);
51 1
        }
52 1
        $this->generatedPages->add($page);
53 1
    }
54
}
55