Completed
Push — master ( 378c5b...5052d2 )
by Markus
08:56
created

DbalStorage::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Mathielen\ImportEngine\Storage;
4
5
use Mathielen\DataImport\Reader\DbalReader;
6
use Doctrine\DBAL\Connection;
7
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
8
9
class DbalStorage implements StorageInterface
10
{
11
    /**
12
     * @var Connection
13
     */
14
    private $connection;
15
16
    private $tableName;
17
18
    private $query;
19
20
    private $info;
21
22
    public function __construct(Connection $connection, $tableName = null, $query = null)
23
    {
24
        $this->connection = $connection;
25
        $this->tableName = $tableName;
26
        $this->query = $query;
27
    }
28
29
    public function getConnection()
30
    {
31
        return $this->connection;
32
    }
33
34
    /**
35
     */
36
    public function getQuery()
37
    {
38
        return $this->query;
39
    }
40
41
    /**
42
     * @param null $query
43
     */
44
    public function setQuery($query)
45
    {
46
        $this->query = $query;
47
    }
48
49
    /**
50
     */
51
    public function getTableName()
52
    {
53
        return $this->tableName;
54
    }
55
56
    /**
57
     * @param null $tableName
58
     */
59
    public function setTableName($tableName)
60
    {
61
        $this->tableName = $tableName;
62
    }
63
64
    /**
65
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer().
66
     */
67
    public function writer()
68
    {
69
        if (empty($this->tableName)) {
70
            throw new InvalidConfigurationException('Can only use pdo for writing if tableName is given.');
71
        }
72
73
        //TODO implement me
74
75
        //return $writer;
76
    }
77
78
    /**
79
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info().
80
     */
81
    public function info()
82
    {
83
        if (!$this->info) {
84
            $this->info = new StorageInfo(array(
85
                'name' => $this->query,
86
                'type' => 'SQL Query',
87
                'count' => count($this->reader()),
88
            ));
89
        }
90
91
        return $this->info;
92
    }
93
94
    /**
95
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader().
96
     */
97
    public function reader()
98
    {
99
        return new DbalReader($this->connection, $this->query);
100
    }
101
102
    /**
103
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields().
104
     */
105
    public function getFields()
106
    {
107
        return $this->reader()->getFields();
108
    }
109
}
110