Completed
Push — master ( a4e58c...d4ecfc )
by Peter
09:22
created

Builder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

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