Passed
Pull Request — master (#13)
by Vincent
07:15
created

SchemaManager::truncate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 5
cp 0.6
rs 10
cc 2
nc 2
nop 2
crap 2.2559
1
<?php
2
3
namespace Bdf\Prime\Schema;
4
5
use Bdf\Prime\Exception\DBALException;
6
use Bdf\Prime\Schema\Adapter\Doctrine\DoctrineTable as PrimeTableAdapter;
7
use Bdf\Prime\Schema\Transformer\Doctrine\TableTransformer;
8
use Doctrine\DBAL\DBALException as DoctrineDBALException;
9
use Doctrine\DBAL\Schema\Schema as DoctrineSchema;
10
use Doctrine\DBAL\Schema\SchemaConfig;
11
use Doctrine\DBAL\Schema\SchemaDiff as DoctrineSchemaDiff;
12
use Doctrine\DBAL\Schema\TableDiff as DoctrineTableDiff;
13
use Doctrine\DBAL\Schema\Table as DoctrineTable;
14
15
/**
16
 * SchemaManager using doctrine schemas
17
 */
18
class SchemaManager extends AbstractSchemaManager
19
{
20
    /**
21
     * Queries to execute
22
     * 
23
     * @var array
24
     */
25
    private $queries = [];
26
27
    
28
    /**
29
     * Get the doctrine schema manager
30
     * 
31
     * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
32
     */
33 799
    public function getDoctrineManager()
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
34
    {
35 799
        return $this->connection->getSchemaManager();
0 ignored issues
show
Bug introduced by
The method getSchemaManager() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        return $this->connection->/** @scrutinizer ignore-call */ getSchemaManager();
Loading history...
36
    }
37
    
38
    /**
39
     * Get the queries to execute
40
     * 
41
     * @return array
42
     */
43 5
    public function toSql()
44
    {
45 5
        return $this->queries;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 11
    public function pending()
52
    {
53 11
        return $this->queries;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 801
    public function clear()
60
    {
61 801
        $this->queries = [];
62
        
63 801
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 801
    public function flush()
70
    {
71 801
        $lastResult = false;
72 801
        $queries = $this->queries;
73
        
74 801
        $this->clear();
75
        
76 801
        foreach ($queries as $query) {
77 801
            $lastResult = $this->connection->exec($query);
0 ignored issues
show
Bug introduced by
The method exec() does not exist on Bdf\Prime\Connection\ConnectionInterface. Did you maybe mean execute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            /** @scrutinizer ignore-call */ 
78
            $lastResult = $this->connection->exec($query);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        }
79
        
80 798
        return $lastResult;
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tables should have a doc-comment as per coding-style.
Loading history...
84
     * {@inheritdoc}
85
     */
86 785
    public function schema($tables = [])
87
    {
88 785
        if (!is_array($tables)) {
89 785
            $tables = [$tables];
90
        }
91
92
        $tables = array_map(function ($table) {
93 785
            if ($table instanceof TableInterface) {
94 785
                return (new TableTransformer($table, $this->platform))->toDoctrine();
95
            }
96
97
            return $table;
98 785
        }, $tables);
99
100 785
        $config = new SchemaConfig();
101 785
        $config->setName($this->connection->getDatabase());
102
103 785
        return new DoctrineSchema($tables, [], $config);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 1
    public function loadSchema()
110
    {
111 1
        return $this->getDoctrineManager()->createSchema();
112
    }
113
114
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $database should have a doc-comment as per coding-style.
Loading history...
115
     * {@inheritdoc}
116
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
117
    public function hasDatabase($database)
118
    {
119
        try {
120
            $databases = $this->getDoctrineManager()->listDatabases($database);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Schema\Abs...anager::listDatabases() has too many arguments starting with $database. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
            $databases = $this->getDoctrineManager()->/** @scrutinizer ignore-call */ listDatabases($database);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
121
        } catch (DoctrineDBALException $e) {
122
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
123
        }
124
125
        return in_array(strtolower($database), array_map('strtolower', $databases));
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
131
    public function getDatabases()
132
    {
133
        try {
134
            return $this->getDoctrineManager()->listDatabases();
135
        } catch (DoctrineDBALException $e) {
136
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
137
        }
138
    }
139
140
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $database should have a doc-comment as per coding-style.
Loading history...
141
     * {@inheritdoc}
142
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
143
    public function createDatabase($database)
144
    {
145
        try {
146
            return $this->push(
147
                $this->platform->grammar()->getCreateDatabaseSQL($database)
148
            );
149
        } catch (DoctrineDBALException $e) {
150
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
151
        }
152
    }
153
154
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $database should have a doc-comment as per coding-style.
Loading history...
155
     * {@inheritdoc}
156
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
157
    public function dropDatabase($database)
158
    {
159
        try {
160
            return $this->push(
161
                $this->platform->grammar()->getDropDatabaseSQL($database)
162
            );
163
        } catch (DoctrineDBALException $e) {
164
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
165
        }
166
    }
167
168
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
169
     * {@inheritdoc}
170
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
171 799
    public function hasTable($tableName)
172
    {
173
        try {
174 799
            return $this->getDoctrineManager()->tablesExist($tableName);
175
        } catch (DoctrineDBALException $e) {
176
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
177
        }
178
    }
179
180
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
181
     * {@inheritdoc}
182
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
183 88
    public function loadTable($tableName)
184
    {
185
        try {
186 88
            $manager = $this->getDoctrineManager();
187
188 88
            $foreignKeys = [];
189
190 88
            if ($this->platform->grammar()->supportsForeignKeyConstraints()) {
191
                $foreignKeys = $manager->listTableForeignKeys($tableName);
192
            }
193
194 88
            return new PrimeTableAdapter(
195 88
                new DoctrineTable(
196 88
                    $tableName,
197 88
                    $manager->listTableColumns($tableName),
198 88
                    $manager->listTableIndexes($tableName),
199 88
                    $foreignKeys
200
                ),
201 88
                $this->connection->platform()->types()
202
            );
203
        } catch (DoctrineDBALException $e) {
204
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
205
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end try"
Loading history...
206
    }
207
208
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
209
     * {@inheritdoc}
210
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
211 654
    public function drop($tableName)
212
    {
213
        try {
214 654
            return $this->push(
215 654
                $this->platform->grammar()->getDropTableSQL($tableName)
216
            );
217 25
        } catch (DoctrineDBALException $e) {
218
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
219
        }
220
    }
221
222
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $cascade should have a doc-comment as per coding-style.
Loading history...
223
     * {@inheritdoc}
224
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
225 53
    public function truncate($tableName, $cascade = false)
226
    {
227
        try {
228 53
            return $this->push(
229 53
                $this->platform->grammar()->getTruncateTableSQL($tableName, $cascade)
230
            );
231
        } catch (DoctrineDBALException $e) {
232
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
233
        }
234
    }
235
236
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $newTable should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $oldTable should have a doc-comment as per coding-style.
Loading history...
237
     * {@inheritdoc}
238
     */
239 85
    public function diff(TableInterface $newTable, TableInterface $oldTable)
240
    {
241 85
        $comparator = new Comparator();
0 ignored issues
show
Deprecated Code introduced by
The class Bdf\Prime\Schema\Comparator has been deprecated: since 1.3 Use Prime comparators instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

241
        $comparator = /** @scrutinizer ignore-deprecated */ new Comparator();
Loading history...
242 85
        $comparator->setListDropColumn($this->useDrop);
243
244 85
        return $comparator->compare(
245 85
            $this->schema($oldTable),
246 85
            $this->schema($newTable)
247
        );
248
    }
249
250
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $from should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $to should have a doc-comment as per coding-style.
Loading history...
251
     * {@inheritdoc}
252
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
253 2
    public function rename($from, $to)
254
    {
255 2
        $diff = new DoctrineTableDiff($from);
256 2
        $diff->newName = $to;
257
258
        try {
259 2
            return $this->push(
260 2
                $this->platform->grammar()->getAlterTableSQL($diff)
261
            );
262
        } catch (DoctrineDBALException $e) {
263
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
264
        }
265
    }
266
267
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $queries should have a doc-comment as per coding-style.
Loading history...
268
     * {@inheritdoc}
269
     */
270 801
    public function push($queries)
271
    {
272 801
        if ($queries instanceof DoctrineSchema || $queries instanceof DoctrineSchemaDiff) {
273 785
            $queries = $queries->toSql($this->platform->grammar());
274
        }
275
276 801
        foreach ((array)$queries as $query) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
277 801
            $this->queries[] = $query;
278
        }
279
280 801
        if ($this->autoFlush) {
281 801
            $this->flush();
282
        }
283
284 799
        return $this;
285
    }
286
}
287