Section::addNavigationLinks()   B
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
c 0
b 0
f 0
nc 14
nop 3
dl 0
loc 38
rs 7.6666

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\Collection as PagesCollection;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Collection\Page\Type;
19
use Cecil\Exception\RuntimeException;
20
21
/**
22
 * Section generator class.
23
 *
24
 * This class is responsible for generating sections from the pages in the builder.
25
 * It identifies sections based on the 'section' variable in each page, and
26
 * creates a new page for each section. The generated pages are added to the
27
 * collection of generated pages. It also handles sorting of subpages and
28
 * adding navigation links (next and previous) to the section pages.
29
 */
30
class Section extends AbstractGenerator implements GeneratorInterface
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function generate(): void
36
    {
37
        $sections = [];
38
39
        // identifying sections from all pages
40
        /** @var Page $page */
41
        foreach ($this->builder->getPages() ?? [] as $page) {
42
            // top level (root) sections
43
            if ($page->getSection()) {
44
                // do not add "not published" and "not excluded" pages to its section
45
                if (
46
                    $page->getVariable('published') !== true
47
                    || ($page->getVariable('excluded') || $page->getVariable('exclude'))
48
                ) {
49
                    continue;
50
                }
51
                $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page;
52
            }
53
        }
54
55
        // adds each section to pages collection
56
        if (\count($sections) > 0) {
57
            $menuWeight = 100;
58
59
            foreach ($sections as $section => $languages) {
60
                foreach ($languages as $language => $pagesAsArray) {
61
                    $pageId = $path = Page::slugify($section);
62
                    if ($language != $this->config->getLanguageDefault()) {
63
                        $pageId = "$language/$pageId";
64
                    }
65
                    $page = (new Page($pageId))->setVariable('title', ucfirst($section))
66
                        ->setPath($path);
67
                    if ($this->builder->getPages()->has($pageId)) {
68
                        $page = clone $this->builder->getPages()->get($pageId);
69
                    }
70
                    $pages = new PagesCollection("section-$pageId", $pagesAsArray);
71
                    // cascade variables
72
                    if ($page->hasVariable('cascade')) {
73
                        $cascade = $page->getVariable('cascade');
74
                        if (\is_array($cascade)) {
75
                            $pages->map(function (Page $page) use ($cascade) {
76
                                foreach ($cascade as $key => $value) {
77
                                    if (!$page->hasVariable($key)) {
78
                                        $page->setVariable($key, $value);
79
                                    }
80
                                }
81
                            });
82
                        }
83
                    }
84
                    // sorts pages
85
                    $sortBy = $page->getVariable('sortby') ?? $this->config->get('pages.sortby');
86
                    $pages = $pages->sortBy($sortBy);
87
                    // adds navigation links (excludes taxonomy pages)
88
                    $sortBy = $page->getVariable('sortby')['variable'] ?? $page->getVariable('sortby') ?? $this->config->get('pages.sortby')['variable'] ?? $this->config->get('pages.sortby') ?? 'date';
89
                    if (!\in_array($page->getId(), array_keys((array) $this->config->get('taxonomies')))) {
90
                        $this->addNavigationLinks($pages, $sortBy, $page->getVariable('circular') ?? false);
91
                    }
92
                    // creates page for each section
93
                    $page->setType(Type::SECTION->value)
94
                        ->setSection($path)
95
                        ->setPages($pages)
96
                        ->setVariable('language', $language)
97
                        ->setVariable('date', $pages->first()->getVariable('date'))
98
                        ->setVariable('langref', $path);
99
                    // human readable title
100
                    if ($page->getVariable('title') == 'index') {
101
                        $page->setVariable('title', $section);
102
                    }
103
                    // default menu
104
                    if (!$page->getVariable('menu')) {
105
                        $page->setVariable('menu', ['main' => ['weight' => $menuWeight]]);
106
                    }
107
108
                    try {
109
                        $this->generatedPages->add($page);
110
                    } catch (\DomainException) {
111
                        $this->generatedPages->replace($page->getId(), $page);
112
                    }
113
                }
114
                $menuWeight += 10;
115
            }
116
        }
117
    }
118
119
    /**
120
     * Adds navigation (next and prev) to each pages of a section.
121
     */
122
    protected function addNavigationLinks(PagesCollection $pages, string|null $sortBy = null, bool $circular = false): void
123
    {
124
        $pagesAsArray = $pages->toArray();
125
        if ($sortBy === null || $sortBy == 'date' || $sortBy == 'updated') {
126
            $pagesAsArray = array_reverse($pagesAsArray);
127
        }
128
        $count = \count($pagesAsArray);
129
        if ($count > 1) {
130
            foreach ($pagesAsArray as $position => $page) {
131
                switch ($position) {
132
                    case 0: // first
133
                        if ($circular) {
134
                            $page->setVariables([
135
                                'prev' => $pagesAsArray[$count - 1],
136
                            ]);
137
                        }
138
                        $page->setVariables([
139
                            'next' => $pagesAsArray[$position + 1],
140
                        ]);
141
                        break;
142
                    case $count - 1: // last
143
                        $page->setVariables([
144
                            'prev' => $pagesAsArray[$position - 1],
145
                        ]);
146
                        if ($circular) {
147
                            $page->setVariables([
148
                                'next' => $pagesAsArray[0],
149
                            ]);
150
                        }
151
                        break;
152
                    default:
153
                        $page->setVariables([
154
                            'prev' => $pagesAsArray[$position - 1],
155
                            'next' => $pagesAsArray[$position + 1],
156
                        ]);
157
                        break;
158
                }
159
                $this->generatedPages->add($page);
160
            }
161
        }
162
    }
163
}
164