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

Paginator::getCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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