Passed
Push — Assets/Image ( bd550f...7a290d )
by Arnaud
09:48 queued 04:01
created

Section::addNavigationLinks()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 30
nc 5
nop 1
dl 0
loc 42
rs 9.1288
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Generator;
10
11
use Cecil\Collection\Page\Collection as PagesCollection;
12
use Cecil\Collection\Page\Page;
13
use Cecil\Collection\Page\Type;
14
15
/**
16
 * Class Section.
17
 */
18
class Section extends AbstractGenerator implements GeneratorInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function generate(): void
24
    {
25
        $sections = [];
26
27
        // identify sections
28
        /* @var $page Page */
29
        foreach ($this->pagesCollection as $page) {
30
            if ($page->getSection()) {
31
                $sections[$page->getSection()][] = $page;
32
            }
33
        }
34
35
        // adds section to pages collection
36
        if (count($sections) > 0) {
37
            $menuWeight = 100;
38
            foreach ($sections as $section => $pagesAsArray) {
39
                $pageId = $path = Page::slugify($section);
40
                $page = (new Page($pageId))->setVariable('title', ucfirst($section));
41
                if ($this->pagesCollection->has($pageId)) {
42
                    $page = clone $this->pagesCollection->get($pageId);
43
                }
44
                $pages = new PagesCollection($section, $pagesAsArray);
45
                // sort
46
                $pages = $pages->sortByDate();
47
                if ($page->getVariable('sortby')) {
1 ignored issue
show
Bug introduced by
The method getVariable() does not exist on Cecil\Collection\ItemInterface. It seems like you code against a sub-type of Cecil\Collection\ItemInterface such as Cecil\Collection\Page\Page. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                if ($page->/** @scrutinizer ignore-call */ getVariable('sortby')) {
Loading history...
Bug introduced by
Are you sure the usage of $page->getVariable('sortby') targeting Cecil\Collection\Page\Page::getVariable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
                    $sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby')));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $page->getVariable('sortby') targeting Cecil\Collection\Page\Page::getVariable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49
                    if (method_exists($pages, $sortMethod)) {
50
                        $pages = $pages->$sortMethod();
51
                    }
52
                }
53
                // add navigation links
54
                $this->addNavigationLinks($pages);
55
                // create page for each section
56
                $page->setPath($path)
0 ignored issues
show
Bug introduced by
The method setPath() does not exist on Cecil\Collection\ItemInterface. It seems like you code against a sub-type of Cecil\Collection\ItemInterface such as Cecil\Collection\Page\Page. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
                $page->/** @scrutinizer ignore-call */ 
57
                       setPath($path)
Loading history...
57
                    ->setType(Type::SECTION)
0 ignored issues
show
Bug introduced by
The method setType() does not exist on Cecil\Collection\ItemInterface. It seems like you code against a sub-type of Cecil\Collection\ItemInterface such as Cecil\Collection\Page\Page. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
                    ->/** @scrutinizer ignore-call */ setType(Type::SECTION)
Loading history...
58
                    ->setVariable('pages', $pages)
0 ignored issues
show
Bug introduced by
The method setVariable() does not exist on Cecil\Collection\ItemInterface. It seems like you code against a sub-type of Cecil\Collection\ItemInterface such as Cecil\Collection\Page\Page. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
                    ->/** @scrutinizer ignore-call */ setVariable('pages', $pages)
Loading history...
59
                    ->setVariable('date', $pages->first()->getVariable('date'));
60
                // default menu
61
                if (!$page->getVariable('menu')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $page->getVariable('menu') targeting Cecil\Collection\Page\Page::getVariable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
                    $page->setVariable('menu', [
63
                        'main' => ['weight' => $menuWeight],
64
                    ]);
65
                }
66
                $this->generatedPages->add($page);
67
                $menuWeight += 10;
68
            }
69
        }
70
    }
71
72
    /**
73
     * Add navigation (next and prev) to section sub pages.
74
     *
75
     * @param PagesCollection $pages
76
     *
77
     * @return void
78
     */
79
    protected function addNavigationLinks(PagesCollection $pages): void
80
    {
81
        $pagesAsArray = $pages->toArray();
82
        if (count($pagesAsArray) > 1) {
83
            foreach ($pagesAsArray as $position => $page) {
84
                switch ($position) {
85
                    // first
86
                    case 0:
87
                        $page->setVariables([
88
                            'next' => [
89
                                'id'    => $pagesAsArray[$position + 1]->getId(),
90
                                'path'  => $pagesAsArray[$position + 1]->getPath(),
91
                                'title' => $pagesAsArray[$position + 1]->getVariable('title'),
92
                            ],
93
                        ]);
94
                        break;
95
                    // last
96
                    case count($pagesAsArray) - 1:
97
                        $page->setVariables([
98
                            'prev' => [
99
                                'id'    => $pagesAsArray[$position - 1]->getId(),
100
                                'path'  => $pagesAsArray[$position - 1]->getPath(),
101
                                'title' => $pagesAsArray[$position - 1]->getVariable('title'),
102
                            ],
103
                        ]);
104
                        break;
105
                    default:
106
                        $page->setVariables([
107
                            'prev' => [
108
                                'id'    => $pagesAsArray[$position - 1]->getId(),
109
                                'path'  => $pagesAsArray[$position - 1]->getPath(),
110
                                'title' => $pagesAsArray[$position - 1]->getVariable('title'),
111
                            ],
112
                            'next' => [
113
                                'id'    => $pagesAsArray[$position + 1]->getId(),
114
                                'path'  => $pagesAsArray[$position + 1]->getPath(),
115
                                'title' => $pagesAsArray[$position + 1]->getVariable('title'),
116
                            ],
117
                        ]);
118
                        break;
119
                }
120
                $this->generatedPages->add($page);
121
            }
122
        }
123
    }
124
}
125