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

Factory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createPager() 0 12 1
B createSmartPager() 0 31 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