SimplifiedPager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 70
ccs 36
cts 36
cp 1
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 30 8
A getPager() 0 3 1
A __toString() 0 3 1
A __construct() 0 15 2
1
<?php
2
3
namespace kalanis\kw_paging\Render;
4
5
6
use kalanis\kw_pager\Interfaces\IPager;
7
use kalanis\kw_paging\Interfaces;
8
use kalanis\kw_paging\Traits;
9
use kalanis\kw_paging\Translations;
10
11
12
/**
13
 * Class SimplifiedPager
14
 * @package kalanis\kw_paging\Render
15
 * Simplified pager with less classes
16
 */
17
class SimplifiedPager implements Interfaces\IOutput
18
{
19
    use Traits\TDisplayPages;
20
21
    public const PREV_PAGE = '&lt;';
22
    public const NEXT_PAGE = '&gt;';
23
24
    protected Interfaces\ILink $link;
25
    protected SimplifiedPager\Pager $pager;
26
    protected SimplifiedPager\CurrentPage $currentPage;
27
    protected SimplifiedPager\AnotherPage $anotherPage;
28
    protected SimplifiedPager\DisabledPage $disabledPage;
29
30 5
    public function __construct(
31
        Interfaces\IPositions $positions,
32
        Interfaces\ILink $link,
33
        int $displayPages = Interfaces\IPositions::DEFAULT_DISPLAY_PAGES_COUNT,
34
        ?Interfaces\IPGTranslations $lang = null
35
    )
36
    {
37 5
        $this->positions = $positions;
38 5
        $this->link = $link;
39 5
        $this->displayPagesCount = $displayPages;
40 5
        $this->pager = new SimplifiedPager\Pager();
41 5
        $this->pager->setKpgLang($lang ?: new Translations());
42 5
        $this->currentPage = new SimplifiedPager\CurrentPage();
43 5
        $this->anotherPage = new SimplifiedPager\AnotherPage();
44 5
        $this->disabledPage = new SimplifiedPager\DisabledPage();
45 5
    }
46
47 4
    public function __toString()
48
    {
49 4
        return $this->render();
50
    }
51
52 4
    public function render(bool $showPositions = true): string
53
    {
54 4
        if (!$this->getPositions()->prevPageExists() && !$this->getPositions()->nextPageExists()) {
55 1
            return '';
56
        }
57 3
        $pages = [];
58
59 3
        $first = $this->getPositions()->prevPageExists() ? $this->anotherPage : $this->disabledPage;
60 3
        $this->link->setPageNumber($this->getPositions()->getFirstPage());
61 3
        $pages[] = $first->reset()->setData($this->link, static::PREV_PAGE . static::PREV_PAGE)->render();
62 3
        $this->link->setPageNumber($this->getPositions()->getPrevPage());
63 3
        $pages[] = $first->reset()->setData($this->link, static::PREV_PAGE)->render();
64
65 3
        foreach ($this->getDisplayPages() as $displayPage) {
66 3
            $current = ($this->getPositions()->getPager()->getActualPage() == $displayPage) ? $this->currentPage : $this->anotherPage ;
67 3
            $this->link->setPageNumber($displayPage);
68 3
            $pages[] = $current->reset()->setData($this->link, strval($displayPage))->render();
69
        }
70
71 3
        $last = $this->getPositions()->nextPageExists() ? $this->anotherPage : $this->disabledPage;
72 3
        $this->link->setPageNumber($this->getPositions()->getNextPage());
73 3
        $pages[] = $last->reset()->setData($this->link, static::NEXT_PAGE)->render();
74 3
        $this->link->setPageNumber($this->getPositions()->getLastPage());
75 3
        $pages[] = $last->reset()->setData($this->link, static::NEXT_PAGE . static::NEXT_PAGE)->render();
76
77 3
        $this->pager->setData(
78 3
            implode('', $pages),
79 3
            $showPositions ? $this->getPositions() : null
80
        );
81 3
        return $this->pager->render();
82
    }
83
84 1
    public function getPager(): IPager
85
    {
86 1
        return $this->getPositions()->getPager();
87
    }
88
}
89