AdmingeneratorView::render()   F
last analyzed

Complexity

Conditions 16
Paths 3072

Size

Total Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 1.1198
c 0
b 0
f 0
cc 16
nc 3072
nop 3

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
namespace Admingenerator\GeneratorBundle\Pagerfanta\View;
4
5
use Pagerfanta\PagerfantaInterface;
6
use Pagerfanta\View\ViewInterface;
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
class AdmingeneratorView implements ViewInterface
10
{
11
12
    protected $translator;
13
14
    public function __construct(TranslatorInterface $translator)
15
    {
16
        $this->translator = $translator;
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array())
23
    {
24
        $options = array_merge(array(
25
            'proximity'              => 2,
26
            'previous_message'       => $this->translator->trans('pagerfanta.previous', array(), 'Admingenerator'),
27
            'next_message'           => $this->translator->trans('pagerfanta.next', array(), 'Admingenerator'),
28
            'css_disabled_class'     => 'disabled',
29
            'css_dots_class'         => 'dots',
30
            'css_current_class'      => 'active',
31
            'css_alignment_class'    => 'pagination-right',
32
            'css_buttons_size_class' => 'pagination-sm',
33
            'css_custom_class'       => ''
34
        ), $options);
35
36
        $currentPage = $pagerfanta->getCurrentPage();
37
38
        $startPage = $currentPage - $options['proximity'];
39
        $endPage = $currentPage + $options['proximity'];
40
41
        if ($startPage < 1) {
42
            $endPage = min($endPage + (1 - $startPage), $pagerfanta->getNbPages());
43
            $startPage = 1;
44
        }
45
        if ($endPage > $pagerfanta->getNbPages()) {
46
            $startPage = max($startPage - ($endPage - $pagerfanta->getNbPages()), 1);
47
            $endPage = $pagerfanta->getNbPages();
48
        }
49
50
        $pages = array();
51
52
        // previous
53
        if ($pagerfanta->hasPreviousPage()) {
54
            $pages[] = array($pagerfanta->getPreviousPage(), $options['previous_message']);
55
        }
56
57
        // first
58
        if ($startPage > 1) {
59
            $pages[] = array(1, 1);
60
            if (3 == $startPage) {
61
                $pages[] = array(2, 2);
62
            } elseif (2 != $startPage) {
63
                $pages[] = sprintf(
64
                    '<li class="%s"><span class="%s">...</span></li>',
65
                    $options['css_disabled_class'],
66
                    $options['css_dots_class']
67
                );
68
            }
69
        }
70
71
        // pages
72
        for ($page = $startPage; $page <= $endPage; $page++) {
73
            if ($page == $currentPage) {
74
                $pages[] = sprintf(
75
                    '<li class="%s"><a href="#" class="number">%s</a></li>',
76
                    $options['css_current_class'],
77
                    $page
78
                );
79
            } else {
80
                $pages[] = array($page, $page);
81
            }
82
        }
83
84
        // last
85
        if ($pagerfanta->getNbPages() > $endPage) {
86
            if ($pagerfanta->getNbPages() > ($endPage + 1)) {
87
                if ($pagerfanta->getNbPages() > ($endPage + 2)) {
88
                    $pages[] = sprintf(
89
                        '<li class="%s"><span class="%s">...</span></li>',
90
                        $options['css_disabled_class'],
91
                        $options['css_dots_class']
92
                    );
93
                } else {
94
                    $pages[] = array($endPage + 1, $endPage + 1);
95
                }
96
            }
97
98
            $pages[] = array($pagerfanta->getNbPages(), $pagerfanta->getNbPages());
99
        }
100
101
        // next
102
        if ($pagerfanta->hasNextPage()) {
103
            $pages[] = array($pagerfanta->getNextPage(), $options['next_message']);
104
        }
105
106
        // process
107
        $pagesHtml = '';
108
        foreach ($pages as $page) {
109
            if (is_string($page)) {
110
                $pagesHtml .= $page;
111
            } else {
112
                if (is_string($page[1])) {
113
                    $pagesHtml .= '<li><a href="'.$routeGenerator($page[0]).'">'.$page[1].'</a></li>';
114
                } else {
115
                    $pagesHtml .= '<li><a href="'.$routeGenerator($page[0]).'" class="number">'.$page[1].'</a></li>';
116
                }
117
            }
118
        }
119
120
        return sprintf(
121
            '<ul class="pagination %s %s">%s</ul>',
122
            $options['css_buttons_size_class'],
123
            $options['css_custom_class'],
124
            $pagesHtml
125
        );
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getName()
132
    {
133
        return 'admingenerator';
134
    }
135
}
136