Passed
Push — master ( d1c6a8...c93284 )
by Arnaud
06:11
created

Section   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 78
dl 0
loc 141
ccs 78
cts 82
cp 0.9512
rs 10
c 2
b 1
f 0
wmc 30

3 Methods

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