Completed
Push — 1.x ( 5c5c78...93e31d )
by Akihito
13s queued 10s
created

ExtendedPdoAdapter   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 123
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getNbResults() 0 23 6
A getSlice() 0 7 2
B getLimitClause() 0 18 7
B rewriteCountQuery() 0 26 6
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\Sql\ExtendedPdoInterface;
11
use Pagerfanta\Adapter\AdapterInterface;
12
13
class ExtendedPdoAdapter implements AdapterInterface
14
{
15
    /**
16
     * @var ExtendedPdoInterface
17
     */
18
    private $pdo;
19
20
    /**
21
     * @var string
22
     */
23
    private $sql;
24
25
    /**
26
     * @var array
27
     */
28
    private $params;
29
30 14
    public function __construct(ExtendedPdoInterface $pdo, string $sql, array $params)
31
    {
32 14
        $this->pdo = $pdo;
33 14
        $this->sql = $sql;
34 14
        $this->params = $params;
35 14
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 9
    public function getNbResults()
41
    {
42
        // be smart and try to guess the total number of records
43 9
        $countQuery = $this->rewriteCountQuery($this->sql);
44 9
        if (! $countQuery) {
45
            // GROUP BY => fetch the whole result set and count the rows returned
46 1
            $result = $this->pdo->query($this->sql)->fetchAll();
47 1
48
            return ! $result ? 0 : \count($result);
49 1
        }
50
        if ($this->params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51 8
            /** @var ExtendedPdo $pdo */
52
            $pdo = $this->pdo;
53 2
            $sth = $pdo->prepareWithValues($this->sql, $this->params);
54 2
            $sth->execute();
55 2
            $result = $sth->fetchAll();
56 2
57
            return ! $result ? 0 : \count($result);
58 2
        }
59
        $count = $this->pdo->query($countQuery)->fetchColumn();
60 6
61
        return ! $count ? 0 : (int) $count;
62 6
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getSlice($offset, $length)
68 6
    {
69
        $sql = $this->sql . $this->getLimitClause($offset, $length);
70 6
        $result = $this->pdo->perform($sql, $this->params)->fetchAll(\PDO::FETCH_ASSOC);
71 6
72
        return ! $result ? [] : $result;
73 6
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getLimitClause($offset, $length)
79 9
    {
80
        $hasLimit = $offset || $length;
81 9
        if ($offset && $length) {
82 9
            $clause = PHP_EOL . "LIMIT {$length}";
83 5
            if ($offset) {
84 5
                $clause .= " OFFSET {$offset}";
85 5
            }
86
87
            return $clause;
88 5
        }
89
90
        if ($hasLimit && $length) {
91 4
            return PHP_EOL . "LIMIT {$length}";
92 3
        }
93
94
        return '';
95 1
    }
96
97
    /**
98
     * Return count query
99
     *
100
     * @param string $query
101
     *
102
     * @return string
103
     *
104
     * @see https://github.com/pear/Pager/blob/master/examples/Pager_Wrapper.php
105
     * Taken from pear/pager and modified.
106
     * tested at https://github.com/pear/Pager/blob/80c0e31c8b94f913cfbdeccbe83b63822f42a2f8/tests/pager_wrapper_test.php#L19
107
     * @codeCoverageIgnore
108
     */
109
    public function rewriteCountQuery($query)
110
    {
111
        if (\is_int(\strpos(\strtolower($query), 'union'))) {
112
            return '';
113
        }
114
        if (\preg_match('/^\s*SELECT\s+\bDISTINCT\b/is', $query) || \preg_match('/\s+GROUP\s+BY\s+/is', $query)) {
115
            return '';
116
        }
117
        $openParenthesis = '(?:\()';
118
        $closeParenthesis = '(?:\))';
119
        $subQueryInSelect = $openParenthesis . '.*\bFROM\b.*' . $closeParenthesis;
120
        $pattern = '/(?:.*' . $subQueryInSelect . '.*)\bFROM\b\s+/Uims';
121
        if (\preg_match($pattern, $query)) {
122
            return '';
123
        }
124
        $subQueryWithLimitOrder = $openParenthesis . '.*\b(LIMIT|ORDER)\b.*' . $closeParenthesis;
125
        $pattern = '/.*\bFROM\b.*(?:.*' . $subQueryWithLimitOrder . '.*).*/Uims';
126
        if (\preg_match($pattern, $query)) {
127
            return '';
128
        }
129
        $queryCount = \preg_replace('/(?:.*)\bFROM\b\s+/Uims', 'SELECT COUNT(*) FROM ', $query, 1);
130
        list($queryCount) = \preg_split('/\s+ORDER\s+BY\s+/is', (string) $queryCount);
131
        list($queryCount) = \preg_split('/\bLIMIT\b/is', (string) $queryCount);
132
133
        return \trim((string) $queryCount);
134
    }
135
}
136