Passed
Push — master ( 231a12...f06e64 )
by Jay
03:35
created

FetchIterator::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Db;
3
4
/**
5
 * Class FetchIterator
6
 * @package FMUP\Db
7
 * @author [email protected]
8
 * @author [email protected]
9
 */
10
class FetchIterator implements \Iterator, \ArrayAccess
11
{
12
    /**
13
     * @var mixed
14
     */
15
    private $statement;
16
17
    /**
18
     * @var DbInterface
19
     */
20
    private $dbInterface;
21
22
    /**
23
     * @var array
24
     */
25
    private $current;
26
    /**
27
     * @var array
28
     */
29
    private $params = array();
30
31
    /**
32
     * @var int
33
     */
34
    private $row = 0;
35
36
    /**
37
     * @param mixed $statement
38
     * @param DbInterface $dbInterface
39
     * @param array $params
40
     */
41 8
    public function __construct($statement, DbInterface $dbInterface, array $params = array())
42
    {
43 8
        $this->setStatement($statement)->setDbInterface($dbInterface)->setParams($params);
44 8
    }
45
46
    /**
47
     * Define params for the prepared statement
48
     * @param array $params
49
     * @return $this
50
     */
51 8
    public function setParams(array $params = array())
52
    {
53 8
        $this->params = $params;
54 8
        return $this;
55
    }
56
57
    /**
58
     * @param mixed $statement
59
     * @return $this
60
     */
61 8
    public function setStatement($statement)
62
    {
63 8
        $this->statement = $statement;
64 8
        return $this;
65
    }
66
67
    /**
68
     * @return mixed|\PDOStatement
69
     */
70 5
    public function getStatement()
71
    {
72 5
        return $this->statement;
73
    }
74
75
    /**
76
     * @return DbInterface
77
     */
78 5
    public function getDbInterface()
79
    {
80 5
        return $this->dbInterface;
81
    }
82
83
    /**
84
     * @param DbInterface $dbInterface
85
     * @return $this
86
     */
87 8
    public function setDbInterface(DbInterface $dbInterface)
88
    {
89 8
        $this->dbInterface = $dbInterface;
90 8
        return $this;
91
    }
92
93
    /**
94
     * Return format depends on Fetch Mode
95
     * @return array|object
96
     */
97 4
    public function current()
98
    {
99 4
        return $this->current;
100
    }
101
102
    /**
103
     * @return void
104
     */
105 1
    public function next()
106
    {
107 1
        $this->current = $this->getDbInterface()->fetchRow($this->getStatement());
108 1
        $this->row++;
109 1
    }
110
111
    /**
112
     * @return bool
113
     */
114 3
    public function valid()
115
    {
116 3
        return (bool)($this->current());
117
    }
118
119 2
    public function rewind()
120
    {
121 2
        $this->row = 0;
122 2
        $this->getDbInterface()->execute($this->getStatement(), $this->params);
123 2
        $this->current = $this->getDbInterface()->fetchRow($this->getStatement(), DbInterface::CURSOR_FIRST);
124 2
    }
125
126
    /**
127
     * @return int
128
     */
129 2
    public function key()
130
    {
131 2
        return (int)$this->row;
132
    }
133
134
    /**
135
     * @param mixed $offset
136
     * @return bool
137
     */
138 1
    public function offsetExists($offset)
139
    {
140 1
        $this->seek($offset);
141 1
        return $this->valid();
142
    }
143
144
    /**
145
     * @param mixed $offset
146
     * @return array|object
147
     */
148 1
    public function offsetGet($offset)
149
    {
150 1
        return $this->offsetExists($offset) ? $this->current() : null;
151
    }
152
153
    /**
154
     * @param mixed $offset
155
     * @param mixed $value
156
     * @throws Exception
157
     */
158 1
    public function offsetSet($offset, $value)
159
    {
160 1
        throw new Exception("Unable to set offset $offset to value $value on iterator");
161
    }
162
163
    /**
164
     * @param mixed $offset
165
     * @throws Exception
166
     */
167 1
    public function offsetUnset($offset)
168
    {
169 1
        throw new Exception("Unable to unset offset $offset on iterator");
170
    }
171
172
    /**
173
     * @param int $offset
174
     */
175 2
    protected function seek($offset)
176
    {
177 2
        $this->row = (int)$offset;
178 2
        $this->current = $this->getDbInterface()->fetchRow(
179 2
            $this->getStatement(),
180 2
            DbInterface::CURSOR_FIRST,
181 2
            $this->row
182
        );
183 2
        if ($this->current === false) {
184 2
            $this->getStatement()->execute();
185 2
            $this->current = $this->getDbInterface()->fetchRow(
186 2
                $this->getStatement(),
187 2
                DbInterface::CURSOR_FIRST,
188 2
                $this->row
189
            );
190
        }
191 2
    }
192
}
193