AuraSqlQueryAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 72
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getNbResults() 0 10 1
A getSlice() 0 13 1
A prepareCountQueryBuilder() 0 7 1
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule\Pagerfanta;
8
9
use Aura\Sql\ExtendedPdo;
10
use Aura\SqlQuery\Common\Select;
11
use Aura\SqlQuery\Common\SelectInterface;
12
use Pagerfanta\Adapter\AdapterInterface;
13
14
class AuraSqlQueryAdapter implements AdapterInterface
15
{
16
    private $pdo;
17
18
    /**
19
     * @var SelectInterface
20
     */
21
    private $select;
22
23
    /**
24
     * @var callable
25
     */
26
    private $countQueryBuilderModifier;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param Select   $select
32
     * @param callable $countQueryBuilderModifier a callable to modifier the query builder to count
33
     */
34
35
    /**
36
     * @param ExtendedPdo     $pdo
37
     * @param SelectInterface $select
38
     * @param callable        $countQueryBuilderModifier
39
     */
40 10
    public function __construct(ExtendedPdo $pdo, SelectInterface $select, callable $countQueryBuilderModifier)
41
    {
42 10
        $this->pdo = $pdo;
43 10
        $this->select = clone $select;
44 10
        $this->countQueryBuilderModifier = $countQueryBuilderModifier;
45 10
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 9
    public function getNbResults()
51
    {
52 9
        $select = $this->prepareCountQueryBuilder();
53 9
        $sql = $select->getStatement();
54 9
        $sth = $this->pdo->prepare($sql);
55 9
        $sth->execute($this->select->getBindValues());
56 9
        $result = $sth->fetchColumn();
57
58 9
        return (int) $result;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 9
    public function getSlice($offset, $length)
65
    {
66 9
        $select = clone $this->select;
67
        $sql = $select
68 9
            ->offset($offset)
69 9
            ->limit($length)
70 9
            ->getStatement();
71 9
        $sth = $this->pdo->prepare($sql);
72 9
        $sth->execute($this->select->getBindValues());
73 9
        $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
74
75 9
        return $result;
76
    }
77
78 9
    private function prepareCountQueryBuilder()
79
    {
80 9
        $select = clone $this->select;
81 9
        \call_user_func($this->countQueryBuilderModifier, $select);
82
83 9
        return $select;
84
    }
85
}
86