Completed
Push — master ( 87ea4b...6fe47d )
by Саша
8s
created

QueryExtractor::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Extraload\Extractor\Doctrine;
4
5
use Doctrine\DBAL\Connection;
6
use Extraload\Extractor\ExtractorInterface;
7
8
class QueryExtractor implements ExtractorInterface
9
{
10
    protected $stmt;
11
12
    protected $position;
13
14
    protected $data;
15
16
    public function __construct(Connection $conn, string $sql, array $values = [])
17
    {
18
        $this->stmt = $conn->prepare($sql);
19
        foreach ($values as $value) {
20
            $this->stmt->bindValue(
21
                $value['parameter'],
22
                $value['value'],
23
                $value['data_type'] ?? null
24
            );
25
        }
26
        $this->stmt->execute();
27
28
        $this->position = 0;
29
30
        $this->data = [];
31
    }
32
33
    public function extract()
34
    {
35
        if (count($this->data) >= $this->stmt->rowCount()) {
36
            return;
37
        }
38
        $this->data[$this->position] = $this->stmt->fetch();
39
        $data = $this->current();
40
        $this->next();
41
42
        return $data;
43
    }
44
45
    public function current()
46
    {
47
        return $this->data[$this->position];
48
    }
49
50
    public function key()
51
    {
52
        return $this->position;
53
    }
54
55
    public function next()
56
    {
57
        $this->position += 1;
58
    }
59
60
    public function rewind()
61
    {
62
        $this->position = 0;
63
    }
64
65
    public function valid()
66
    {
67
        return isset($this->data[$this->position]);
68
    }
69
}
70