Passed
Push — master ( 0cc661...15cf8e )
by Gaetano
09:51
created

TableStorage::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
rs 10
c 2
b 0
f 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\StorageHandler\Database;
4
5
use Doctrine\DBAL\Schema\Table;
6
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
7
use Kaliop\eZMigrationBundle\API\ConfigResolverInterface;
8
9
abstract class TableStorage
10
{
11
    /**
12
     * Name of the database table where data is stored.
13
     * @var string $tableName
14
     *
15
     * @todo add setter/getter, as we need to clear versionTableExists when switching this
16
     */
17
    protected $tableName;
18
19
    /**
20
     * @var DatabaseHandler $dbHandler
21
     */
22
    protected $dbHandler;
23
24
    /**
25
     * Flag to indicate that the migration table has been created
26
     *
27
     * @var boolean $tableExists
28
     */
29
    protected $tableExists = false;
30
31
    /** @var array $tableCreationOptions */
32
    protected $tableCreationOptions;
33
34
    /**
35
     * @param DatabaseHandler $dbHandler
36 96
     * @param string $tableNameParameter name of table when $configResolver is null, name of parameter otherwise
37
     * @param ConfigResolverInterface $configResolver
38 96
     * @param array $tableCreationOptions
39 96
     * @throws \Exception
40 96
     */
41
    public function __construct(DatabaseHandler $dbHandler, $tableNameParameter, ConfigResolverInterface $configResolver = null, $tableCreationOptions = array())
42
    {
43
        $this->dbHandler = $dbHandler;
44
        $this->tableName = $configResolver ? $configResolver->getParameter($tableNameParameter) : $tableNameParameter;
45
        $this->tableCreationOptions = $tableCreationOptions;
46
    }
47 63
48
    abstract function createTable();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49 63
50
    /**
51
     * To be called from createTable to add to table definitions the common options (storage engine, charset, etc...)
52
     * @param Table $table
53
     */
54
    protected function injectTableCreationOptions(Table $table)
55
    {
56
        foreach($this->tableCreationOptions as $key => $value) {
57
            $table->addOption($key, $value);
58 96
        }
59
    }
60
61 96
    /**
62 96
     * @return mixed
63 96
     */
64 96
    protected function getConnection()
65
    {
66
        return $this->dbHandler->getConnection();
67
    }
68 2
69
    /**
70
     * Check if a table exists in the database
71
     *
72
     * @param string $tableName
73
     * @return bool
74
     */
75
    protected function tableExist($tableName)
76
    {
77
        /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $sm */
78
        $sm = $this->dbHandler->getConnection()->getSchemaManager();
79 96
        foreach ($sm->listTables() as $table) {
80
            if ($table->getName() == $tableName) {
81 96
                return true;
82 62
            }
83
        }
84
85 96
        return false;
86 95
    }
87 95
88
    /**
89
     * Check if the version db table exists and create it if not.
90 2
     *
91
     * @return bool true if table has been created, false if it was already there
92 2
     *
93 2
     * @todo add a 'force' flag to force table re-creation
94
     * @todo manage changes to table definition
95
     */
96
    protected function createTableIfNeeded()
97
    {
98
        if ($this->tableExists) {
99
            return false;
100
        }
101
102
        if ($this->tableExist($this->tableName)) {
103
            $this->tableExists = true;
104
            return false;
105
        }
106
107
        $this->createTable();
108
109
        $this->tableExists = true;
110
        return true;
111
    }
112
113
    /**
114
     * Removes all data from storage as well as removing the tables itself
115
     */
116
    protected function drop()
117
    {
118
        if ($this->tableExist($this->tableName)) {
119
            $this->dbHandler->exec('DROP TABLE ' . $this->tableName);
120
        }
121
    }
122
123
    /**
124
     * Removes all data from storage
125
     */
126
    public function truncate()
127
    {
128
        if ($this->tableExist($this->tableName)) {
129
            $this->dbHandler->exec('TRUNCATE ' . $this->tableName);
130
        }
131
    }
132
}
133