Passed
Pull Request — master (#1013)
by lee
07:38
created

Pagination::generate()   F

Complexity

Conditions 21
Paths 398

Size

Total Lines 126
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 81
CRAP Score 21.0061

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 21
eloc 86
c 1
b 0
f 0
nc 398
nop 0
dl 0
loc 126
ccs 81
cts 83
cp 0.9759
crap 21.0061
rs 0.8583

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
 * 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
17
/**
18
 * Class Generator\Pagination.
19
 */
20
class Pagination extends AbstractGenerator implements GeneratorInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 1
    public function generate(): void
26
    {
27 1
        if (false === $this->config->get('pagination.enabled')) {
28
            return;
29
        }
30
31
        // filters pages: home, sections and terms
32
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
33 1
            return in_array($page->getType(), [Type::HOMEPAGE, Type::SECTION, Type::TERM]);
34 1
        });
35
        /** @var Page $page */
36 1
        foreach ($filteredPages as $page) {
37
            // global config
38 1
            $paginationPerPage = intval($this->config->get('pagination.max'));
39 1
            $paginationPath = $this->config->get('pagination.path');
40
            // global page config
41 1
            $path = $page->getPath();
42 1
            $pages = $page->getVariable('pages');
43 1
            $sortby = $page->getVariable('sortby');
44
            // pagination page config
45 1
            $pagePagination = $page->getVariable('pagination');
46 1
            if ($pagePagination) {
47 1
                if (array_key_exists('enabled', $pagePagination) && !$pagePagination['enabled']) {
48
                    continue;
49
                }
50 1
                if (array_key_exists('max', $pagePagination)) {
51 1
                    $paginationPerPage = intval($pagePagination['max']);
52
                }
53 1
                if (array_key_exists('path', $pagePagination)) {
54 1
                    $paginationPath = $pagePagination['path'];
55
                }
56
            }
57 1
            $pagesTotal = count($pages);
58
            // abords pagination?
59 1
            if ($pagesTotal <= $paginationPerPage) {
60 1
                continue;
61
            }
62
            // sorts pages
63 1
            $pages = $pages->sortByDate();
64 1
            if ($sortby) {
65 1
                $sortMethod = sprintf('sortBy%s', ucfirst($sortby));
66 1
                if (method_exists($pages, $sortMethod)) {
67 1
                    $pages = $pages->$sortMethod();
68
                }
69
            }
70
71
            // builds pagination
72 1
            if ($pagesTotal > $paginationPerPage) {
73 1
                $paginationPagesCount = ceil($pagesTotal / $paginationPerPage);
74 1
                for ($i = 0; $i < $paginationPagesCount; $i++) {
75 1
                    $pagesInPagination = new \LimitIterator(
76 1
                        $pages->getIterator(),
77 1
                        ($i * $paginationPerPage),
78 1
                        $paginationPerPage
79
                    );
80 1
                    $pagesInPagination = new PagesCollection(
81 1
                        $page->getId().'-page-'.($i + 1),
82 1
                        iterator_to_array($pagesInPagination)
83
                    );
84 1
                    $alteredPage = clone $page;
85
                    // first page
86 1
                    $firstPath = Page::slugify(sprintf('%s', $path));
87 1
                    if ($i == 0) {
88
                        // ie: blog + blog/page/1 (alias)
89 1
                        $pageId = Page::slugify(sprintf('%s', $path));
90
                        // homepage special case
91 1
                        if ($path == '') {
92 1
                            $pageId = 'index';
93
                        }
94 1
                        $currentPath = $firstPath;
95
                        $alteredPage
96 1
                            ->setId($pageId)
97 1
                            ->setPath($firstPath)
98 1
                            ->setVariable('aliases', [
99 1
                                sprintf('%s/%s/%s', $path, $paginationPath, 1),
100
                            ]);
101
                    } else {
102
                        // ie: blog/page/2
103 1
                        $pageId = Page::slugify(sprintf('%s/%s/%s', $path, $paginationPath, $i + 1));
104 1
                        $currentPath = $pageId;
105
                        $alteredPage
106 1
                            ->setId($pageId)
107 1
                            ->setVirtual(true)
108 1
                            ->setPath($currentPath)
109 1
                            ->unVariable('menu')
110 1
                            ->setVariable('paginated', true);
111
                    }
112
                    // updates 'pagination' variable
113
                    $pagination = [
114 1
                        'totalpages' => $pagesTotal,
115 1
                        'pages'      => $pagesInPagination,
116 1
                        'current'    => $i + 1,
117 1
                        'count'      => $paginationPagesCount,
118
                    ];
119
                    // adds links
120 1
                    $pagination['links'] = ['self' => $currentPath ?: 'index'];
121 1
                    $pagination['links'] += ['first' => $firstPath ?: 'index'];
122 1
                    if ($i == 1) {
123 1
                        $pagination['links'] += ['prev' => Page::slugify($path ?: 'index')];
124
                    }
125 1
                    if ($i > 1) {
126 1
                        $pagination['links'] += ['prev' => Page::slugify(sprintf(
127 1
                            '%s/%s/%s',
128 1
                            $path,
129 1
                            $paginationPath,
130 1
                            $i
131
                        ))];
132
                    }
133 1
                    if ($i < $paginationPagesCount - 1) {
134 1
                        $pagination['links'] += ['next' => Page::slugify(sprintf(
135 1
                            '%s/%s/%s',
136 1
                            $path,
137 1
                            $paginationPath,
138 1
                            $i + 2
139
                        ))];
140
                    }
141 1
                    $pagination['links'] += ['last' => Page::slugify(sprintf(
142 1
                        '%s/%s/%s',
143 1
                        $path,
144 1
                        $paginationPath,
145 1
                        $paginationPagesCount
146
                    ))];
147 1
                    $alteredPage->setVariable('pagination', $pagination);
148
                    // updates date with the first element of the collection
149 1
                    $alteredPage->setVariable('date', $pagesInPagination->first()->getVariable('date'));
150 1
                    $this->generatedPages->add($alteredPage);
151
                }
152
            }
153
        }
154 1
    }
155
}
156