PdoReader::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ddeboer\DataImport\Reader;
4
5
/**
6
 * Reads data through PDO
7
 *
8
 * @author Robbie Mackay
9
 */
10
class PdoReader implements CountableReader
11
{
12
    /**
13
     * @var \PDO
14
     */
15
    protected $pdo;
16
17
    /**
18
     * @var string
19
     */
20
    protected $tableName;
21
22
    /**
23
     * @var \PDOStatement
24
     */
25
    protected $statement;
26
27
    /**
28
     * @var array
29
     */
30
    private $data;
31
32
    /**
33
     * @param \PDO   $pdo
34
     * @param string $sql
35
     * @param array  $params
36
     */
37 4
    public function __construct(\PDO $pdo, $sql, array $params = [])
38
    {
39 4
        $this->pdo = $pdo;
40 4
        $this->statement = $this->pdo->prepare($sql);
41
42 4
        foreach ($params as $key => $value) {
43
            $this->statement->bindValue($key, $value);
44 4
        }
45 4
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function getFields()
51
    {
52 1
        if ($this->statement->execute()) {
53
            // Statement executed successfully
54
            // Grab the first row to find keys
55 1
            $row = $this->statement->fetch(\PDO::FETCH_ASSOC);
56
            // Return field keys, or empty array no rows remain
57 1
            return array_keys($row ? $row : []);
58
        } else {
59
            // If the statement errors return empty
60
            return [];
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function current()
68
    {
69 2
        return current($this->data);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function next()
76
    {
77 2
        next($this->data);
78 2
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function key()
84
    {
85
        return key($this->data);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function valid()
92
    {
93 2
        $key = key($this->data);
94
95 2
        return ($key !== null && $key !== false);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    public function rewind()
102
    {
103 2
        $this->loadData();
104
105 2
        reset($this->data);
106 2
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function count()
112
    {
113 1
        $this->loadData();
114
115 1
        return count($this->data);
116
    }
117
118
    /**
119
     * Load data if it hasn't been loaded yet
120
     */
121 3
    protected function loadData()
122
    {
123 3
        if (null === $this->data) {
124 3
            $this->statement->execute();
125 3
            $this->data = $this->statement->fetchAll(\PDO::FETCH_ASSOC);
126 3
        }
127 3
    }
128
}
129