Passed
Push — fix ( af71a1 )
by Arnaud
03:19
created

Section::addNavigationLinks()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 64
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 45
nc 14
nop 3
dl 0
loc 64
ccs 36
cts 36
cp 1
crap 9
rs 7.6444
c 1
b 0
f 0

How to fix   Long Method   

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
 * 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\Collection as PagesCollection;
14
use Cecil\Collection\Page\Page;
15
use Cecil\Collection\Page\Type;
16
use Cecil\Exception\Exception;
17
18
/**
19
 * Class Generator\Section.
20
 */
21
class Section extends AbstractGenerator implements GeneratorInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function generate(): void
27
    {
28 1
        $sections = [];
29
30
        // identifying sections
31
        /** @var Page $page */
32 1
        foreach ($this->builder->getPages() as $page) {
33 1
            if ($page->getSection()) {
34
                // excludes page from its section?
35 1
                if ($page->getVariable('published') !== true || $page->getVariable('exclude')) {
36 1
                    $alteredPage = clone $page;
37 1
                    $alteredPage->setSection('');
38 1
                    $this->builder->getPages()->replace($page->getId(), $alteredPage);
39 1
                    continue;
40
                }
41 1
                $sections[$page->getSection()][$page->getVariable('language') ?? $this->config->getLanguageDefault()][] = $page;
42
            }
43
        }
44
45
        // adds section to pages collection
46 1
        if (count($sections) > 0) {
47 1
            $menuWeight = 100;
48
49 1
            foreach ($sections as $section => $languages) {
50 1
                foreach ($languages as $language => $pagesAsArray) {
51 1
                    $pageId = $path = Page::slugify($section);
52 1
                    if ($language != $this->config->getLanguageDefault()) {
53 1
                        $pageId = sprintf('%s.%s', $pageId, $language);
54
                    }
55 1
                    $page = (new Page($pageId))->setVariable('title', ucfirst($section));
56 1
                    if ($this->builder->getPages()->has($pageId)) {
57 1
                        $page = clone $this->builder->getPages()->get($pageId);
58
                    }
59 1
                    $pages = new PagesCollection($section, $pagesAsArray);
60
                    // cascade
61
                    /** @var \Cecil\Collection\Page\Page $page */
62 1
                    if ($page->hasVariable('cascade')) {
63 1
                        $cascade = $page->getVariable('cascade');
64
                        $pages->map(function (Page $page) use ($cascade) {
65 1
                            foreach ($cascade as $key => $value) {
66 1
                                if (!$page->hasVariable($key)) {
67 1
                                    $page->setVariable($key, $value);
68
                                }
69
                            }
70 1
                        });
71
                    }
72
                    // sorts
73 1
                    $pages = $pages->sortByDate();
74 1
                    if ($page->hasVariable('sortby')) {
75 1
                        $sortMethod = sprintf('sortBy%s', ucfirst((string) $page->getVariable('sortby')));
76 1
                        if (!method_exists($pages, $sortMethod)) {
77
                            throw new Exception(sprintf(
78
                                'In "%s" section "%s" is not a valid value for "sortby" variable.',
79
                                $page->getId(),
80
                                $page->getVariable('sortby')
81
                            ));
82
                        }
83 1
                        $pages = $pages->$sortMethod();
84
                    }
85
                    // adds navigation links (excludes taxonomy pages)
86 1
                    if (!in_array($page->getId(), array_keys($this->config->get('taxonomies')))) {
0 ignored issues
show
Bug introduced by
It seems like $this->config->get('taxonomies') can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

86
                    if (!in_array($page->getId(), array_keys(/** @scrutinizer ignore-type */ $this->config->get('taxonomies')))) {
Loading history...
87
                        $this->addNavigationLinks($pages, $page->getVariable('sortby'), $page->getVariable('circular'));
88 1
                    }
89 1
                    // creates page for each section
90 1
                    $page->setPath($path)
91 1
                        ->setType(Type::SECTION)
92 1
                        ->setVariable('pages', $pages)
93 1
                        ->setVariable('date', $pages->first()->getVariable('date'))
94
                        ->setVariable('language', $language)
95 1
                        ->setVariable('langref', $path);
96 1
                    // default menu
97 1
                    if (!$page->getVariable('menu')) {
98
                        $page->setVariable('menu', [
99
                            'main' => ['weight' => $menuWeight],
100 1
                        ]);
101
                    }
102 1
                    $this->generatedPages->add($page);
103
                }
104
                $menuWeight += 10;
105 1
            }
106
        }
107
    }
108
109
    /**
110
     * Adds navigation (next and prev) to section sub pages.
111
     */
112
    protected function addNavigationLinks(PagesCollection $pages, string $sort = null, $circular = false): void
113
    {
114
        $pagesAsArray = $pages->toArray();
115
        if ($sort === null || $sort == 'date') {
116 1
            $pagesAsArray = array_reverse($pagesAsArray);
117
        }
118 1
        $count = count($pagesAsArray);
119 1
        if ($count > 1) {
120 1
            foreach ($pagesAsArray as $position => $page) {
121
                switch ($position) {
122 1
                    // first
123 1
                    case 0:
124 1
                        if ($circular) {
125
                            $page->setVariables([
126
                                'prev' => [
127 1
                                    'id'    => $pagesAsArray[$count - 1]->getId(),
128 1
                                    'path'  => $pagesAsArray[$count - 1]->getPath(),
129 1
                                    'title' => $pagesAsArray[$count - 1]->getVariable('title'),
130
                                ],
131 1
                            ]);
132 1
                        }
133 1
                        $page->setVariables([
134
                            'next' => [
135
                                'id'    => $pagesAsArray[$position + 1]->getId(),
136
                                'path'  => $pagesAsArray[$position + 1]->getPath(),
137 1
                                'title' => $pagesAsArray[$position + 1]->getVariable('title'),
138
                            ],
139 1
                        ]);
140 1
                        break;
141 1
                    // last
142
                    case $count - 1:
143
                        $page->setVariables([
144 1
                            'prev' => [
145
                                'id'    => $pagesAsArray[$position - 1]->getId(),
146 1
                                'path'  => $pagesAsArray[$position - 1]->getPath(),
147 1
                                'title' => $pagesAsArray[$position - 1]->getVariable('title'),
148
                            ],
149 1
                        ]);
150 1
                        if ($circular) {
151 1
                            $page->setVariables([
152
                                'next' => [
153
                                    'id'    => $pagesAsArray[0]->getId(),
154 1
                                    'path'  => $pagesAsArray[0]->getPath(),
155 1
                                    'title' => $pagesAsArray[0]->getVariable('title'),
156
                                ],
157 1
                            ]);
158 1
                        }
159 1
                        break;
160
                    default:
161
                        $page->setVariables([
162
                            'prev' => [
163 1
                                'id'    => $pagesAsArray[$position - 1]->getId(),
164
                                'path'  => $pagesAsArray[$position - 1]->getPath(),
165 1
                                'title' => $pagesAsArray[$position - 1]->getVariable('title'),
166
                            ],
167 1
                            'next' => [
168 1
                                'id'    => $pagesAsArray[$position + 1]->getId(),
169 1
                                'path'  => $pagesAsArray[$position + 1]->getPath(),
170
                                'title' => $pagesAsArray[$position + 1]->getVariable('title'),
171
                            ],
172 1
                        ]);
173 1
                        break;
174 1
                }
175
                $this->generatedPages->add($page);
176
            }
177 1
        }
178
    }
179
}
180