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

Pagination   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 6
loc 64
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 6 58 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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