DbalReader   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 201
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setRowCountCalculated() 0 4 1
A isRowCountCalculated() 0 4 1
A setSql() 0 6 1
A setSqlParameters() 0 7 1
A current() 0 8 2
A next() 0 5 1
A key() 0 4 1
A valid() 0 8 2
A rewind() 0 11 3
A count() 0 15 4
A doCalcRowCount() 0 7 1
A prepare() 0 9 2
1
<?php
2
3
namespace Port\Dbal;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Statement;
7
use Port\Reader\CountableReader;
8
9
/**
10
 * Reads data through the Doctrine DBAL
11
 */
12
class DbalReader implements CountableReader
13
{
14
    /**
15
     * @var Connection
16
     */
17
    private $connection;
18
19
    /**
20
     * @var array
21
     */
22
    private $data;
23
24
    /**
25
     * @var Statement
26
     */
27
    private $stmt;
28
29
    /**
30
     * @var string
31
     */
32
    private $sql;
33
34
    /**
35
     * @var array
36
     */
37
    private $params;
38
39
    /**
40
     * @var integer
41
     */
42
    private $rowCount;
43
44
    /**
45
     * @var boolean
46
     */
47
    private $rowCountCalculated = true;
48
49
    /**
50
     * @var string
51
     */
52
    private $key;
53
54
    /**
55
     * @param Connection $connection
56
     * @param string     $sql
57
     * @param array      $params
58
     */
59
    public function __construct(Connection $connection, $sql, array $params = [])
60
    {
61
        $this->connection = $connection;
62
63
        $this->setSql($sql, $params);
64
    }
65
66
    /**
67
     * Do calculate row count?
68
     *
69
     * @param boolean $calculate
70
     */
71
    public function setRowCountCalculated($calculate = true)
72
    {
73
        $this->rowCountCalculated = (bool) $calculate;
74
    }
75
76
    /**
77
     * Is row count calculated?
78
     *
79
     * @return boolean
80
     */
81
    public function isRowCountCalculated()
82
    {
83
        return $this->rowCountCalculated;
84
    }
85
86
    /**
87
     * Set Query string with Parameters
88
     *
89
     * @param string $sql
90
     * @param array  $params
91
     */
92
    public function setSql($sql, array $params = [])
93
    {
94
        $this->sql = (string) $sql;
95
96
        $this->setSqlParameters($params);
97
    }
98
99
    /**
100
     * Set SQL parameters
101
     *
102
     * @param array $params
103
     */
104
    public function setSqlParameters(array $params)
105
    {
106
        $this->params = $params;
107
108
        $this->stmt = null;
109
        $this->rowCount = null;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function current()
116
    {
117
        if (null === $this->data) {
118
            $this->rewind();
119
        }
120
121
        return $this->data;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function next()
128
    {
129
        $this->key++;
130
        $this->data = $this->stmt->fetch(\PDO::FETCH_ASSOC);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->stmt->fetch(\PDO::FETCH_ASSOC) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The method Doctrine\DBAL\Statement::fetch() has been deprecated with message: Use fetchNumeric(), fetchAssociative() or fetchOne() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function key()
137
    {
138
        return $this->key;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function valid()
145
    {
146
        if (null === $this->data) {
147
            $this->rewind();
148
        }
149
150
        return (false !== $this->data);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function rewind()
157
    {
158
        if (null === $this->stmt) {
159
            $this->stmt = $this->prepare($this->sql, $this->params);
160
        }
161
        if (0 !== $this->key) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of 0 (integer) and $this->key (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
162
            $this->stmt->execute();
163
            $this->data = $this->stmt->fetch(\PDO::FETCH_ASSOC);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->stmt->fetch(\PDO::FETCH_ASSOC) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The method Doctrine\DBAL\Statement::fetch() has been deprecated with message: Use fetchNumeric(), fetchAssociative() or fetchOne() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
164
            $this->key = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $key was declared of type string, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
165
        }
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function count()
172
    {
173
        if (null === $this->rowCount) {
174
            if ($this->rowCountCalculated) {
175
                $this->doCalcRowCount();
176
            } else {
177
                if (null === $this->stmt) {
178
                    $this->rewind();
179
                }
180
                $this->rowCount = $this->stmt->rowCount();
181
            }
182
        }
183
184
        return $this->rowCount;
185
    }
186
187
    private function doCalcRowCount()
188
    {
189
        $statement = $this->prepare(sprintf('SELECT COUNT(*) FROM (%s) AS port_cnt', $this->sql), $this->params);
190
        $statement->execute();
191
192
        $this->rowCount = (int) $statement->fetchColumn(0);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Statement::fetchColumn() has been deprecated with message: Use fetchOne() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
193
    }
194
195
    /**
196
     * Prepare given statement
197
     *
198
     * @param string $sql
199
     * @param array  $params
200
     *
201
     * @return Statement
202
     */
203
    private function prepare($sql, array $params)
204
    {
205
        $statement = $this->connection->prepare($sql);
206
        foreach ($params as $key => $value) {
207
            $statement->bindValue($key, $value);
208
        }
209
210
        return $statement;
211
    }
212
}
213