Passed
Pull Request — master (#2215)
by Arnaud
08:40 queued 03:51
created

Pagination::generate()   D

Complexity

Conditions 20
Paths 95

Size

Total Lines 119
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 86
CRAP Score 20.0153

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 80
c 1
b 0
f 0
nc 95
nop 0
dl 0
loc 119
ccs 86
cts 89
cp 0.9663
crap 20.0153
rs 4.1666

How to fix   Long Method    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
20
/**
21
 * Pagination generator.
22
 *
23
 * This generator creates paginated pages for sections, terms, and the homepage.
24
 * It filters the pages based on their type, checks for pagination settings,
25
 * and generates paginated pages accordingly.
26
 */
27
class Pagination extends AbstractGenerator implements GeneratorInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function generate(): void
33
    {
34 1
        if (!$this->config->isEnabled('pages.pagination')) {
35
            return;
36
        }
37
38
        // filters list pages (home, sections and terms)
39 1
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
40 1
            return \in_array($page->getType(), [Type::HOMEPAGE->value, Type::SECTION->value, Type::TERM->value]);
41 1
        });
42
        /** @var Page $page */
43 1
        foreach ($filteredPages as $page) {
44
            // if no sub-pages: by-pass
45 1
            if ($page->getPages() === null) {
46 1
                continue;
47
            }
48 1
            $pages = $page->getPages()->filter(function (Page $page) {
49 1
                return $page->getType() == Type::PAGE->value && $page->getVariable('published');
50 1
            });
51
            // if no published sub-pages: by-pass
52 1
            if ($pages === null) {
53
                continue;
54
            }
55 1
            $path = $page->getPath();
56
            // site pagination configuration
57 1
            $paginationPerPage = \intval($this->config->get('pages.pagination.max') ?? 5);
58 1
            $paginationPath = $this->config->get('pages.pagination.path') ?? 'page';
59
            // page pagination configuration
60 1
            $pagePagination = $page->getVariable('pagination');
61 1
            if ($pagePagination) {
62 1
                if (isset($pagePagination['enabled']) && $pagePagination['enabled'] === false) {
63
                    continue;
64
                }
65 1
                if (isset($pagePagination['max'])) {
66 1
                    $paginationPerPage = \intval($pagePagination['max']);
67
                }
68 1
                if (isset($pagePagination['path'])) {
69 1
                    $paginationPath = (string) $pagePagination['path'];
70
                }
71
            }
72 1
            $pagesTotal = \count($pages);
73
            // is pagination not necessary?
74 1
            if ($pagesTotal <= $paginationPerPage) {
75 1
                continue;
76
            }
77
            // sorts pages
78 1
            $pages = Section::sortSubPages($this->config, $page, $pages);
79
            // builds paginator
80 1
            $paginatorPagesCount = \intval(ceil($pagesTotal / $paginationPerPage));
81 1
            for ($i = 0; $i < $paginatorPagesCount; $i++) {
82 1
                $itPagesInPagination = new \LimitIterator($pages->getIterator(), $i * $paginationPerPage, $paginationPerPage);
83 1
                $pagesInPagination = new PagesCollection(
84 1
                    $page->getId() . '-page-' . ($i + 1),
85 1
                    iterator_to_array($itPagesInPagination)
86 1
                );
87 1
                $alteredPage = clone $page;
88
                // first page (ie: blog/page/1 -> blog)
89 1
                $pageId = $page->getId();
90 1
                $alteredPage
91 1
                    ->setVariable('alias', [
92 1
                        \sprintf('%s/%s/%s', $path, $paginationPath, 1),
93 1
                    ]);
94
                // others pages (ie: blog/page/X)
95 1
                if ($i > 0) {
96 1
                    $pageId = Page::slugify(\sprintf('%s/%s/%s', $page->getId(), $paginationPath, $i + 1));
97 1
                    $alteredPage
98 1
                        ->setId($pageId)
99 1
                        ->setVirtual(true)
100 1
                        ->setPath(Page::slugify(\sprintf('%s/%s/%s', $path, $paginationPath, $i + 1)))
101 1
                        ->unVariable('menu')
102 1
                        ->unVariable('alias')
103 1
                        ->unVariable('aliases') // backward compatibility
104 1
                        ->unVariable('langref')
105 1
                        ->setVariable('paginated', true);
106
                }
107
                // set paginator values
108 1
                $paginator = [
109 1
                    'pages'       => $pagesInPagination,
110 1
                    'pages_total' => $pagesTotal,
111 1
                    'totalpages'  => $pagesTotal, // backward compatibility
112 1
                    'count'       => $paginatorPagesCount,
113 1
                    'current'     => $i + 1,
114 1
                ];
115
                // adds links
116 1
                $paginator['links'] = ['first' => $page->getId() ?: 'index'];
117 1
                if ($i == 1) {
118 1
                    $paginator['links'] += ['prev' => $page->getId() ?: 'index'];
119
                }
120 1
                if ($i > 1) {
121 1
                    $paginator['links'] += ['prev' => Page::slugify(\sprintf(
122 1
                        '%s/%s/%s',
123 1
                        $page->getId(),
124 1
                        $paginationPath,
125 1
                        $i
126 1
                    ))];
127
                }
128 1
                $paginator['links'] += ['self' => $pageId ?: 'index'];
129 1
                if ($i < $paginatorPagesCount - 1) {
130 1
                    $paginator['links'] += ['next' => Page::slugify(\sprintf(
131 1
                        '%s/%s/%s',
132 1
                        $page->getId(),
133 1
                        $paginationPath,
134 1
                        $i + 2
135 1
                    ))];
136
                }
137 1
                $paginator['links'] += ['last' => Page::slugify(\sprintf(
138 1
                    '%s/%s/%s',
139 1
                    $page->getId(),
140 1
                    $paginationPath,
141 1
                    $paginatorPagesCount
142 1
                ))];
143 1
                $paginator['links'] += ['path' => Page::slugify(\sprintf('%s/%s', $page->getId(), $paginationPath))];
144
                // set paginator to cloned page
145 1
                $alteredPage->setPaginator($paginator);
146 1
                $alteredPage->setVariable('pagination', $paginator); // backward compatibility
147
                // updates date with the first element of the collection
148 1
                $alteredPage->setVariable('date', $pagesInPagination->first()->getVariable('date'));
149
150 1
                $this->generatedPages->add($alteredPage);
151
            }
152
        }
153
    }
154
}
155