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

QueryExtractor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A extract() 0 11 2
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
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