TimedOutputAdapter::renameColumn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Db\Adapter;
9
10
use BadMethodCallException;
11
use Phinx\Db\Table\Column;
12
use Phinx\Db\Table\ForeignKey;
13
use Phinx\Db\Table\Index;
14
use Phinx\Db\Table\Table;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Wraps any adapter to record the time spend executing its commands
19
 */
20
class TimedOutputAdapter extends AdapterWrapper implements DirectActionInterface
21
{
22
    /**
23
     * @inheritDoc
24
     */
25
    public function getAdapterType()
26
    {
27
        return $this->getAdapter()->getAdapterType();
28
    }
29
30
    /**
31
     * Start timing a command.
32
     *
33
     * @return callable A function that is to be called when the command finishes
34
     */
35
    public function startCommandTimer()
36
    {
37
        $started = microtime(true);
38
39
        return function () use ($started) {
40
            $end = microtime(true);
41
            if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
42
                $this->getOutput()->writeln('    -> ' . sprintf('%.4fs', $end - $started));
43
            }
44
        };
45
    }
46
47
    /**
48
     * Write a Phinx command to the output.
49
     *
50
     * @param string $command Command Name
51
     * @param array $args Command Args
52
     * @return void
53
     */
54
    public function writeCommand($command, $args = [])
55
    {
56 5
        if (OutputInterface::VERBOSITY_VERBOSE > $this->getOutput()->getVerbosity()) {
57
            return;
58 5
        }
59
60 5
        if (count($args)) {
61 5
            $outArr = [];
62
            foreach ($args as $arg) {
63
                if (is_array($arg)) {
64 5
                    $arg = array_map(
65
                        function ($value) {
66
                            return '\'' . $value . '\'';
67
                        },
68
                        $arg
69
                    );
70
                    $outArr[] = '[' . implode(', ', $arg) . ']';
71
                    continue;
72
                }
73
74 5
                $outArr[] = '\'' . $arg . '\'';
75
            }
76 5
            $this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
77 5
78
            return;
79
        }
80
81
        $this->getOutput()->writeln(' -- ' . $command);
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function insert(Table $table, $row)
88
    {
89
        $end = $this->startCommandTimer();
90
        $this->writeCommand('insert', [$table->getName()]);
91
        parent::insert($table, $row);
92
        $end();
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function bulkinsert(Table $table, $rows)
99
    {
100
        $end = $this->startCommandTimer();
101
        $this->writeCommand('bulkinsert', [$table->getName()]);
102
        parent::bulkinsert($table, $rows);
103
        $end();
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function createTable(Table $table, array $columns = [], array $indexes = [])
110
    {
111
        $end = $this->startCommandTimer();
112
        $this->writeCommand('createTable', [$table->getName()]);
113
        parent::createTable($table, $columns, $indexes);
114
        $end();
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     *
120
     * @throws \BadMethodCallException
121 2
     * @return void
122
     */
123 2
    public function changePrimaryKey(Table $table, $newColumns)
124 2
    {
125 2
        $adapter = $this->getAdapter();
126 2
        if (!$adapter instanceof DirectActionInterface) {
127 2
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
128
        }
129
        $end = $this->startCommandTimer();
130
        $this->writeCommand('changePrimaryKey', [$table->getName()]);
131
        $adapter->changePrimaryKey($table, $newColumns);
132 3
        $end();
133
    }
134 3
135 3
    /**
136 3
     * {@inheritDoc}
137 3
     *
138 3
     * @throws \BadMethodCallException
139
     * @return void
140
     */
141
    public function changeComment(Table $table, $newComment)
142
    {
143 3
        $adapter = $this->getAdapter();
144
        if (!$adapter instanceof DirectActionInterface) {
145 3
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
146 3
        }
147 3
        $end = $this->startCommandTimer();
148 3
        $this->writeCommand('changeComment', [$table->getName()]);
149 3
        $adapter->changeComment($table, $newComment);
150
        $end();
151
    }
152
153
    /**
154 3
     * {@inheritDoc}
155
     *
156 3
     * @throws \BadMethodCallException
157 3
     * @return void
158 3
     */
159 3
    public function renameTable($tableName, $newTableName)
160 3
    {
161
        $adapter = $this->getAdapter();
162
        if (!$adapter instanceof DirectActionInterface) {
163
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
164
        }
165
        $end = $this->startCommandTimer();
166
        $this->writeCommand('renameTable', [$tableName, $newTableName]);
167
        $adapter->renameTable($tableName, $newTableName);
168
        $end();
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     *
174
     * @throws \BadMethodCallException
175
     * @return void
176 3
     */
177
    public function dropTable($tableName)
178 3
    {
179 3
        $adapter = $this->getAdapter();
180 3
        if (!$adapter instanceof DirectActionInterface) {
181
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
182 3
        }
183 3
        $end = $this->startCommandTimer();
184 3
        $this->writeCommand('dropTable', [$tableName]);
185 3
        $adapter->dropTable($tableName);
186 3
        $end();
187 3
    }
188 3
189 3
    /**
190
     * @inheritDoc
191
     */
192
    public function truncateTable($tableName)
193
    {
194 3
        $end = $this->startCommandTimer();
195
        $this->writeCommand('truncateTable', [$tableName]);
196 3
        parent::truncateTable($tableName);
197 3
        $end();
198 3
    }
199 3
200 3
    /**
201
     * {@inheritDoc}
202
     *
203
     * @throws \BadMethodCallException
204
     * @return void
205
     */
206
    public function addColumn(Table $table, Column $column)
207
    {
208
        $adapter = $this->getAdapter();
209
        if (!$adapter instanceof DirectActionInterface) {
210
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
211
        }
212
        $end = $this->startCommandTimer();
213
        $this->writeCommand(
214
            'addColumn',
215
            [
216 3
                $table->getName(),
217
                $column->getName(),
218 3
                $column->getType(),
219 3
            ]
220 3
        );
221 3
        $adapter->addColumn($table, $column);
222 3
        $end();
223
    }
224
225
    /**
226
     * {@inheritDoc}
227
     *
228
     * @throws \BadMethodCallException
229
     * @return void
230
     */
231
    public function renameColumn($tableName, $columnName, $newColumnName)
232
    {
233
        $adapter = $this->getAdapter();
234
        if (!$adapter instanceof DirectActionInterface) {
235
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
236
        }
237
        $end = $this->startCommandTimer();
238
        $this->writeCommand('renameColumn', [$tableName, $columnName, $newColumnName]);
239
        $adapter->renameColumn($tableName, $columnName, $newColumnName);
240
        $end();
241
    }
242
243
    /**
244
     * {@inheritDoc}
245
     *
246
     * @throws \BadMethodCallException
247
     * @return void
248
     */
249
    public function changeColumn($tableName, $columnName, Column $newColumn)
250
    {
251
        $adapter = $this->getAdapter();
252
        if (!$adapter instanceof DirectActionInterface) {
253
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
254
        }
255
        $end = $this->startCommandTimer();
256
        $this->writeCommand('changeColumn', [$tableName, $columnName, $newColumn->getType()]);
257
        $adapter->changeColumn($tableName, $columnName, $newColumn);
258
        $end();
259
    }
260 3
261
    /**
262 3
     * {@inheritDoc}
263 3
     *
264 3
     * @throws \BadMethodCallException
265 3
     * @return void
266 3
     */
267
    public function dropColumn($tableName, $columnName)
268
    {
269
        $adapter = $this->getAdapter();
270
        if (!$adapter instanceof DirectActionInterface) {
271 3
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
272
        }
273 3
        $end = $this->startCommandTimer();
274 3
        $this->writeCommand('dropColumn', [$tableName, $columnName]);
275 3
        $adapter->dropColumn($tableName, $columnName);
276 3
        $end();
277 3
    }
278
279
    /**
280
     * {@inheritDoc}
281
     *
282 5
     * @throws \BadMethodCallException
283
     * @return void
284 5
     */
285 5
    public function addIndex(Table $table, Index $index)
286 5
    {
287 5
        $adapter = $this->getAdapter();
288 5
        if (!$adapter instanceof DirectActionInterface) {
289
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
290
        }
291
        $end = $this->startCommandTimer();
292
        $this->writeCommand('addIndex', [$table->getName(), $index->getColumns()]);
293 5
        $adapter->addIndex($table, $index);
294
        $end();
295 5
    }
296 5
297 5
    /**
298 5
     * {@inheritDoc}
299 5
     *
300
     * @throws \BadMethodCallException
301
     * @return void
302
     */
303
    public function dropIndex($tableName, $columns)
304
    {
305
        $adapter = $this->getAdapter();
306
        if (!$adapter instanceof DirectActionInterface) {
307
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
308
        }
309
        $end = $this->startCommandTimer();
310
        $this->writeCommand('dropIndex', [$tableName, $columns]);
311
        $adapter->dropIndex($tableName, $columns);
312
        $end();
313
    }
314
315
    /**
316
     * {@inheritDoc}
317
     *
318
     * @throws \BadMethodCallException
319
     * @return void
320
     */
321
    public function dropIndexByName($tableName, $indexName)
322
    {
323
        $adapter = $this->getAdapter();
324
        if (!$adapter instanceof DirectActionInterface) {
325
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
326
        }
327
        $end = $this->startCommandTimer();
328
        $this->writeCommand('dropIndexByName', [$tableName, $indexName]);
329
        $adapter->dropIndexByName($tableName, $indexName);
330
        $end();
331
    }
332
333
    /**
334
     * {@inheritDoc}
335
     *
336
     * @throws \BadMethodCallException
337
     * @return void
338
     */
339
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
340
    {
341
        $adapter = $this->getAdapter();
342
        if (!$adapter instanceof DirectActionInterface) {
343
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
344
        }
345
        $end = $this->startCommandTimer();
346
        $this->writeCommand('addForeignKey', [$table->getName(), $foreignKey->getColumns()]);
347
        $adapter->addForeignKey($table, $foreignKey);
348
        $end();
349
    }
350
351
    /**
352
     * {@inheritDoc}
353
     *
354
     * @throws \BadMethodCallException
355
     * @return void
356
     */
357
    public function dropForeignKey($tableName, $columns, $constraint = null)
358
    {
359
        $adapter = $this->getAdapter();
360
        if (!$adapter instanceof DirectActionInterface) {
361
            throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
362
        }
363
        $end = $this->startCommandTimer();
364
        $this->writeCommand('dropForeignKey', [$tableName, $columns]);
365
        $adapter->dropForeignKey($tableName, $columns, $constraint);
366
        $end();
367
    }
368
369
    /**
370
     * @inheritDoc
371
     */
372
    public function createDatabase($name, $options = [])
373
    {
374
        $end = $this->startCommandTimer();
375
        $this->writeCommand('createDatabase', [$name]);
376
        parent::createDatabase($name, $options);
377
        $end();
378
    }
379
380
    /**
381
     * @inheritDoc
382
     */
383
    public function dropDatabase($name)
384
    {
385
        $end = $this->startCommandTimer();
386
        $this->writeCommand('dropDatabase', [$name]);
387
        parent::dropDatabase($name);
388
        $end();
389
    }
390
391
    /**
392
     * @inheritDoc
393
     */
394
    public function createSchema($name = 'public')
395
    {
396
        $end = $this->startCommandTimer();
397
        $this->writeCommand('createSchema', [$name]);
398
        parent::createSchema($name);
399
        $end();
400
    }
401
402
    /**
403
     * @inheritDoc
404
     */
405
    public function dropSchema($name)
406
    {
407
        $end = $this->startCommandTimer();
408
        $this->writeCommand('dropSchema', [$name]);
409
        parent::dropSchema($name);
410
        $end();
411
    }
412
413
    /**
414
     * @inheritDoc
415
     */
416
    public function executeActions(Table $table, array $actions)
417
    {
418
        $end = $this->startCommandTimer();
419
        $this->writeCommand(sprintf('Altering table %s', $table->getName()));
420
        parent::executeActions($table, $actions);
421
        $end();
422
    }
423
}
424