Completed
Push — master ( 049347...e0e97b )
by Reen
02:10
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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
        $pageCount = $this->core->getPageCount($count, $limit);
54
55
        if ($page > $pageCount) {
56
            $page = 1;
57
            $results = $adapter->getSlice($this->core->getOffset($page, $limit), $limit);
58
        }
59
60
        return new Pager(
61
            $page,
62
            $limit,
63
            $count,
64
            $pageCount,
65
            $results
66
        );
67
    }
68
}
69