Builder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 52
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A paginate() 0 5 1
A paginateQuery() 0 15 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace AnimeDb\Bundle\PaginationBundle\Service;
11
12
use Doctrine\ORM\QueryBuilder;
13
14
class Builder
15
{
16
    /**
17
     * The number of pages displayed in the navigation.
18
     *
19
     * @var int
20
     */
21
    protected $max_navigate = Configuration::DEFAULT_LIST_LENGTH;
22
23
    /**
24
     * @param int $max_navigate
25
     */
26 4
    public function __construct($max_navigate)
27
    {
28 4
        $this->max_navigate = $max_navigate;
29 4
    }
30
31
    /**
32
     * @param int $total_pages
33
     * @param int $current_page
34
     *
35
     * @return Configuration
36
     */
37 2
    public function paginate($total_pages = 0, $current_page = 1)
38
    {
39 2
        return (new Configuration($total_pages, $current_page))
40 2
            ->setMaxNavigate($this->max_navigate);
41
    }
42
43
    /**
44
     * @param QueryBuilder $query
45
     * @param int $per_page
46
     * @param int $current_page
47
     *
48
     * @return Configuration
49
     */
50 2
    public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1)
51
    {
52 2
        $counter = clone $query;
53
        $total = $counter
54 2
            ->select(sprintf('COUNT(%s)', current($query->getRootAliases())))
55 2
            ->getQuery()
56 2
            ->getSingleScalarResult();
57
58
        $query
59 2
            ->setFirstResult(($current_page - 1) * $per_page)
60 2
            ->setMaxResults($per_page);
61
62 2
        return (new Configuration(ceil($total / $per_page), $current_page))
63 2
            ->setMaxNavigate($this->max_navigate);
64
    }
65
}
66