Completed
Push — master ( 9704d2...c47828 )
by Albert
03:44
created

Paginator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 1
cbo 2
dl 0
loc 64
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A render() 0 8 1
A getCurrentPage() 0 4 1
A getPerPage() 0 4 1
A getCriteria() 0 4 1
1
<?php
2
3
namespace Albert221\Blog\Pagination;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Twig_Environment;
7
8
class Paginator
9
{
10
    private $currentPage;
11
    private $perPage;
12
    private $count;
13
    private $criteria;
14
    private $twig;
15
16
    /**
17
     * Paginator constructor.
18
     *
19
     * @param $page
20
     * @param $perPage
21
     * @param $count
22
     * @param Twig_Environment $twig
23
     */
24 9
    public function __construct($page, $perPage, $count, Twig_Environment $twig)
25
    {
26 9
        $this->currentPage = $page;
27 9
        $this->perPage = $perPage;
28 9
        $this->count = $count;
29 9
        $this->twig = $twig;
30
        
31 9
        $this->criteria = Criteria::create()
32 9
            ->setFirstResult(($page - 1) * $perPage)
33 9
            ->setMaxResults($count);
34 9
    }
35
36
    /**
37
     * @return string
38
     */
39 3
    public function render()
40
    {
41 3
        return $this->twig->render('components/pagination.twig', [
42 3
            'currentPage' => $this->currentPage,
43 3
            'perPage' => $this->perPage,
44 3
            'pages' => ceil($this->count / $this->perPage)
45 3
        ]);
46
    }
47
48
    /**
49
     * @return int
50
     */
51 3
    public function getCurrentPage()
52
    {
53 3
        return $this->currentPage;
54
    }
55
56
    /**
57
     * @return int
58
     */
59 3
    public function getPerPage()
60
    {
61 3
        return $this->perPage;
62
    }
63
64
    /**
65
     * @return Criteria
66
     */
67 3
    public function getCriteria()
68
    {
69 3
        return $this->criteria;
70
    }
71
}
72