Completed
Push — master ( aeaf5f...306089 )
by Reen
01:36
created

Factory::createPager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
namespace ReenExeCubeTime\LightPaginator;
4
5
use ReenExeCubeTime\LightPaginator\Adapter\AdapterInterface;
6
7
class Factory
8
{
9
    /**
10
     * @var Core
11
     */
12
    private $core;
13
14
    /**
15
     * @param Core $core
16
     */
17
    public function __construct(Core $core)
18
    {
19
        $this->core = $core;
20
    }
21
22
    /**
23
     * @param AdapterInterface $adapter
24
     * @param $page
25
     * @param $limit
26
     * @return PagerInterface
27
     */
28
    public function createPager(AdapterInterface $adapter, $page, $limit)
29
    {
30
        $count = $adapter->getCount();
31
32
        return new Pager(
33
            $page,
34
            $limit,
35
            $count,
36
            $this->core->getPageCount($count, $limit),
37
            $adapter->getSlice($this->core->getOffset($page, $limit), $limit)
38
        );
39
    }
40
41
    /**
42
     * @param AdapterInterface $adapter
43
     * @param $page
44
     * @param $limit
45
     * @return PagerInterface
46
     */
47
    public function createSmartPager(AdapterInterface $adapter, $page, $limit)
48
    {
49
        $results = $adapter->getSlice($this->core->getOffset($page, $limit), $limit);
50
51
        $count = $adapter->getCount();
52
53
        if ($count === 0) {
54
            return new Pager(
55
                1,
56
                $limit,
57
                $count,
58
                0,
59
                []
60
            );
61
        }
62
63
        $pageCount = $this->core->getPageCount($count, $limit);
64
65
        if ($page > $pageCount) {
66
            $page = 1;
67
            $results = $adapter->getSlice($this->core->getOffset($page, $limit), $limit);
68
        }
69
70
        return new Pager(
71
            $page,
72
            $limit,
73
            $count,
74
            $pageCount,
75
            $results
76
        );
77
    }
78
}
79