QueryExtractor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A extract() 0 8 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
20
        foreach ($values as $value) {
21
            $this->stmt->bindValue(
22
                $value['parameter'],
23
                $value['value'],
24
                $value['data_type'] ?? null
25
            );
26
        }
27
28
        $this->stmt->execute();
29
30
        $this->position = 0;
31
32
        $this->data = [];
33
    }
34
35
    public function extract()
36
    {
37
        if (false !== $this->data[$this->position] = $this->stmt->fetch()) {
38
            $data = $this->current();
39
            $this->next();
40
            return $data;
41
        }
42
    }
43
44
    public function current()
45
    {
46
        return $this->data[$this->position];
47
    }
48
49
    public function key()
50
    {
51
        return $this->position;
52
    }
53
54
    public function next()
55
    {
56
        $this->position += 1;
57
    }
58
59
    public function rewind()
60
    {
61
        $this->position = 0;
62
    }
63
64
    public function valid()
65
    {
66
        return isset($this->data[$this->position]);
67
    }
68
}
69