Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

Pagination::generate()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 58

Duplication

Lines 6
Ratio 10.34 %

Importance

Changes 0
Metric Value
dl 6
loc 58
rs 7.3608
c 0
b 0
f 0
cc 9
nc 12
nop 2

How to fix   Long Method   

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
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Generator;
10
11
use Cecil\Collection\Collection as PageCollection;
12
use Cecil\Collection\Page\Page;
13
use Cecil\Page\NodeType;
14
15
/**
16
 * Class Pagination.
17
 */
18
class Pagination extends AbstractGenerator implements GeneratorInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function generate(PageCollection $pageCollection, \Closure $messageCallback)
24
    {
25
        $generatedPages = new PageCollection();
26
27
        $filteredPages = $pageCollection->filter(function (Page $page) {
28
            return in_array($page->getNodeType(), [NodeType::HOMEPAGE, NodeType::SECTION]);
29
        });
30
31
        /* @var $page Page */
32
        foreach ($filteredPages as $page) {
33
            if ($this->config->get('site.paginate.disabled')) {
34
                return $generatedPages;
35
            }
36
37
            $paginateMax = intval($this->config->get('site.paginate.max'));
38
            $paginatePath = $this->config->get('site.paginate.path');
39
            $pages = $page->getVariable('pages');
40
            $path = $page->getPathname();
41
42
            // paginate
43
            if (count($pages) > $paginateMax) {
44
                $paginateCount = ceil(count($pages) / $paginateMax);
45
                for ($i = 0; $i < $paginateCount; $i++) {
46
                    $pagesInPagination = array_slice($pages, ($i * $paginateMax), $paginateMax);
47
                    $alteredPage = clone $page;
48
                    // first page, if homepage id = 'index'
49
                    if ($i == 0) {
50
                        $alteredPage
51
                            ->setId(Page::urlize(sprintf('%s/%s', $path, $path == '' ? 'index' : '')))
52
                            ->setPathname(Page::urlize(sprintf('%s', $path)))
53
                            ->setVariable('aliases', [
54
                                sprintf('%s/%s/%s', $path, $paginatePath, 1),
55
                            ]);
56
                    } else {
57
                        $alteredPage
58
                            ->setId(Page::urlize(sprintf('%s/%s/%s/', $path, $paginatePath, $i + 1)))
59
                            ->setPathname(Page::urlize(sprintf('%s/%s/%s', $path, $paginatePath, $i + 1)))
60
                            ->unVariable('menu');
61
                    }
62
                    // pagination
63
                    $pagination = ['pages' => $pagesInPagination];
64 View Code Duplication
                    if ($i > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
                        $pagination += ['prev' => Page::urlize(sprintf('%s/%s/%s', $path, $paginatePath, $i))];
66
                    }
67 View Code Duplication
                    if ($i < $paginateCount - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                        $pagination += ['next' => Page::urlize(sprintf('%s/%s/%s', $path, $paginatePath, $i + 2))];
69
                    }
70
                    $alteredPage
71
                        ->setVariable('pagination', $pagination)
72
                        ->setVariable('date', reset($pagination['pages'])->getDate());
73
74
                    $generatedPages->add($alteredPage);
75
                }
76
            }
77
        }
78
79
        return $generatedPages;
80
    }
81
}
82