Passed
Push — master ( 874a4c...aa75e7 )
by Arnaud
06:27
created

Section   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 97.5%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 75
c 5
b 2
f 0
dl 0
loc 135
ccs 78
cts 80
cp 0.975
rs 10
wmc 29

3 Methods

Rating   Name   Duplication   Size   Complexity  
F generate() 0 68 16
A sortSubPages() 0 12 3
B addNavigationLinks() 0 38 10
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\Collection as PagesCollection;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Collection\Page\Type;
19
use Cecil\Exception\RuntimeException;
20
21
/**
22
 * Class Generator\Section.
23
 */
24
class Section extends AbstractGenerator implements GeneratorInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function generate(): void
30
    {
31 1
        $sections = [];
32
33
        // identifying sections
34 1
        foreach ($this->builder->getPages() as $page) {
35
            /** @var Page $page */
36 1
            if ($page->getSection()) {
37
                // do not add not published and not excluded pages to its section
38 1
                if ($page->getVariable('published') !== true || $page->getVariable('exclude')) {
39 1
                    continue;
40
                }
41 1
                $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page;
42
            }
43
        }
44
45
        // adds each 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 = "$language/$pageId";
54
                    }
55 1
                    $page = (new Page($pageId))->setVariable('title', ucfirst($section))
56 1
                        ->setPath($path);
57 1
                    if ($this->builder->getPages()->has($pageId)) {
58 1
                        $page = clone $this->builder->getPages()->get($pageId);
59
                    }
60 1
                    $subPages = new PagesCollection("section-$pageId", $pagesAsArray);
61
                    // cascade variables
62 1
                    if ($page->hasVariable('cascade')) {
63 1
                        $cascade = $page->getVariable('cascade');
64 1
                        $subPages->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 pages
73 1
                    $pages = Section::sortSubPages($this->config, $page, $subPages);
74
                    // adds navigation links (excludes taxonomy pages)
75 1
                    $sortBy = $page->getVariable('sortby')['variable'] ?? $page->getVariable('sortby') ?? $this->config->get('pages.sortby')['variable'] ?? $this->config->get('pages.sortby') ?? 'date';
76 1
                    if (!\in_array($page->getId(), array_keys((array) $this->config->get('taxonomies')))) {
77 1
                        $this->addNavigationLinks($pages, $sortBy, $page->getVariable('circular') ?? false);
78
                    }
79
                    // creates page for each section
80 1
                    $page->setType(Type::SECTION->value)
81 1
                        ->setSection($path)
82 1
                        ->setPages($pages)
83 1
                        ->setVariable('language', $language)
84 1
                        ->setVariable('date', $pages->first()->getVariable('date'))
85 1
                        ->setVariable('langref', $path);
86
                    // human readable title
87 1
                    if ($page->getVariable('title') == 'index') {
88 1
                        $page->setVariable('title', $section);
89
                    }
90
                    // default menu
91 1
                    if (!$page->getVariable('menu')) {
92 1
                        $page->setVariable('menu', ['main' => ['weight' => $menuWeight]]);
93
                    }
94 1
                    $this->generatedPages->add($page);
95
                }
96 1
                $menuWeight += 10;
97
            }
98
        }
99
    }
100
101
    /**
102
     * Sorts subpages.
103
     */
104 1
    public static function sortSubPages(\Cecil\Config $config, Page $page, PagesCollection $subPages): PagesCollection
105
    {
106 1
        $subPages = $subPages->sortBy($config->get('pages.sortby'));
107 1
        if ($page->hasVariable('sortby')) {
108
            try {
109 1
                $subPages = $subPages->sortBy($page->getVariable('sortby'));
110
            } catch (RuntimeException $e) {
111
                throw new RuntimeException(sprintf('In page "%s", %s', $page->getId(), $e->getMessage()));
112
            }
113
        }
114
115 1
        return $subPages;
116
    }
117
118
    /**
119
     * Adds navigation (next and prev) to section subpages.
120
     */
121 1
    protected function addNavigationLinks(PagesCollection $pages, string|null $sortBy = null, bool $circular = false): void
122
    {
123 1
        $pagesAsArray = $pages->toArray();
124 1
        if ($sortBy === null || $sortBy == 'date' || $sortBy == 'updated') {
125 1
            $pagesAsArray = array_reverse($pagesAsArray);
126
        }
127 1
        $count = \count($pagesAsArray);
128 1
        if ($count > 1) {
129 1
            foreach ($pagesAsArray as $position => $page) {
130
                switch ($position) {
131 1
                    case 0: // first
132 1
                        if ($circular) {
133 1
                            $page->setVariables([
134 1
                                'prev' => $pagesAsArray[$count - 1],
135 1
                            ]);
136
                        }
137 1
                        $page->setVariables([
138 1
                            'next' => $pagesAsArray[$position + 1],
139 1
                        ]);
140 1
                        break;
141 1
                    case $count - 1: // last
142 1
                        $page->setVariables([
143 1
                            'prev' => $pagesAsArray[$position - 1],
144 1
                        ]);
145 1
                        if ($circular) {
146 1
                            $page->setVariables([
147 1
                                'next' => $pagesAsArray[0],
148 1
                            ]);
149
                        }
150 1
                        break;
151
                    default:
152 1
                        $page->setVariables([
153 1
                            'prev' => $pagesAsArray[$position - 1],
154 1
                            'next' => $pagesAsArray[$position + 1],
155 1
                        ]);
156 1
                        break;
157
                }
158 1
                $this->generatedPages->add($page);
159
            }
160
        }
161
    }
162
}
163