Completed
Push — master ( e33650...15989c )
by José
02:42
created

TimedOutputAdapter::changeColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 3
cts 6
cp 0.5
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.5
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2017 Cake Software Foundation
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Db\Adapter
28
 */
29
namespace Phinx\Db\Adapter;
30
31
use Phinx\Db\Table\Column;
32
use Phinx\Db\Table\ForeignKey;
33
use Phinx\Db\Table\Index;
34
use Phinx\Db\Table\Table;
35
use Symfony\Component\Console\Output\OutputInterface;
36
37
/**
38
 * Wraps any adapter to record the time spend executing its commands
39
 */
40
class TimedOutputAdapter extends AdapterWrapper implements DirectActionInterface
41
{
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getAdapterType()
47
    {
48
        return $this->getAdapter()->getAdapterType();
49
    }
50
51
    /**
52
     * Start timing a command.
53
     *
54
     * @return callable A function that is to be called when the command finishes
55
     */
56 5
    public function startCommandTimer()
57
    {
58 5
        $started = microtime(true);
59
60 5
        return function () use ($started) {
61 5
            $end = microtime(true);
62
            if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
63
                $this->getOutput()->writeln('    -> ' . sprintf('%.4fs', $end - $started));
64 5
            }
65
        };
66
    }
67
68
    /**
69
     * Write a Phinx command to the output.
70
     *
71
     * @param string $command Command Name
72
     * @param array  $args    Command Args
73
     * @return void
74 5
     */
75
    public function writeCommand($command, $args = [])
76 5
    {
77 5
        if (OutputInterface::VERBOSITY_VERBOSE > $this->getOutput()->getVerbosity()) {
78
            return;
79
        }
80
81
        if (count($args)) {
82
            $outArr = [];
83
            foreach ($args as $arg) {
84
                if (is_array($arg)) {
85
                    $arg = array_map(
86
                        function ($value) {
87
                            return '\'' . $value . '\'';
88
                        },
89
                        $arg
90
                    );
91
                    $outArr[] = '[' . implode(', ', $arg) . ']';
92
                    continue;
93
                }
94
95
                $outArr[] = '\'' . $arg . '\'';
96
            }
97
            $this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
98
99
            return;
100
        }
101
102
        $this->getOutput()->writeln(' -- ' . $command);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @return void
109
     */
110 View Code Duplication
    public function insert(Table $table, $row)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $end = $this->startCommandTimer();
113
        $this->writeCommand('insert', [$table->getName()]);
114
        parent::insert($table, $row);
115
        $end();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     *
121 2
     * @return void
122
     */
123 2 View Code Duplication
    public function bulkinsert(Table $table, $rows)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124 2
    {
125 2
        $end = $this->startCommandTimer();
126 2
        $this->writeCommand('bulkinsert', [$table->getName()]);
127 2
        parent::bulkinsert($table, $rows);
128
        $end();
129
    }
130
131
    /**
132 3
     * {@inheritdoc}
133
     */
134 3 View Code Duplication
    public function createTable(Table $table, array $columns = [], array $indexes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135 3
    {
136 3
        $end = $this->startCommandTimer();
137 3
        $this->writeCommand('createTable', [$table->getName()]);
138 3
        parent::createTable($table, $columns, $indexes);
139
        $end();
140
    }
141
142
    /**
143 3
     * {@inheritdoc}
144
     */
145 3
    public function changePrimaryKey(Table $table, $newColumns)
146 3
    {
147 3
        $adapter = $this->getAdapter();
148 3
        if (!$adapter instanceof DirectActionInterface) {
149 3
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
150
        }
151
        $end = $this->startCommandTimer();
152
        $this->writeCommand('changePrimaryKey', [$table->getName()]);
153
        $adapter->changePrimaryKey($table, $newColumns);
154 3
        $end();
155
    }
156 3
157 3
    /**
158 3
     * {@inheritdoc}
159 3
     */
160 3
    public function changeComment(Table $table, $newComment)
161
    {
162
        $adapter = $this->getAdapter();
163
        if (!$adapter instanceof DirectActionInterface) {
164
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
165
        }
166
        $end = $this->startCommandTimer();
167
        $this->writeCommand('changeComment', [$table->getName()]);
168
        $adapter->changeComment($table, $newComment);
169
        $end();
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 View Code Duplication
    public function renameTable($tableName, $newTableName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176 3
    {
177
        $adapter = $this->getAdapter();
178 3
        if (!$adapter instanceof DirectActionInterface) {
179 3
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
180 3
        }
181
        $end = $this->startCommandTimer();
182 3
        $this->writeCommand('renameTable', [$tableName, $newTableName]);
183 3
        $adapter->renameTable($tableName, $newTableName);
184 3
        $end();
185 3
    }
186 3
187 3
    /**
188 3
     * {@inheritdoc}
189 3
     */
190
    public function dropTable($tableName)
191
    {
192
        $adapter = $this->getAdapter();
193
        if (!$adapter instanceof DirectActionInterface) {
194 3
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
195
        }
196 3
        $end = $this->startCommandTimer();
197 3
        $this->writeCommand('dropTable', [$tableName]);
198 3
        $adapter->dropTable($tableName);
199 3
        $end();
200 3
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function truncateTable($tableName)
206
    {
207
        $end = $this->startCommandTimer();
208
        $this->writeCommand('truncateTable', [$tableName]);
209
        parent::truncateTable($tableName);
210
        $end();
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 3
    public function addColumn(Table $table, Column $column)
217
    {
218 3
        $adapter = $this->getAdapter();
219 3
        if (!$adapter instanceof DirectActionInterface) {
220 3
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
221 3
        }
222 3
        $end = $this->startCommandTimer();
223
        $this->writeCommand(
224
            'addColumn',
225
            [
226
                $table->getName(),
227
                $column->getName(),
228
                $column->getType()
229
            ]
230
        );
231
        $adapter->addColumn($table, $column);
232
        $end();
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238 View Code Duplication
    public function renameColumn($tableName, $columnName, $newColumnName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
    {
240
        $adapter = $this->getAdapter();
241
        if (!$adapter instanceof DirectActionInterface) {
242
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
243
        }
244
        $end = $this->startCommandTimer();
245
        $this->writeCommand('renameColumn', [$tableName, $columnName, $newColumnName]);
246
        $adapter->renameColumn($tableName, $columnName, $newColumnName);
247
        $end();
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253 View Code Duplication
    public function changeColumn($tableName, $columnName, Column $newColumn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
    {
255
        $adapter = $this->getAdapter();
256
        if (!$adapter instanceof DirectActionInterface) {
257
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
258
        }
259
        $end = $this->startCommandTimer();
260 3
        $this->writeCommand('changeColumn', [$tableName, $columnName, $newColumn->getType()]);
261
        $adapter->changeColumn($tableName, $columnName, $newColumn);
262 3
        $end();
263 3
    }
264 3
265 3
    /**
266 3
     * {@inheritdoc}
267
     */
268 View Code Duplication
    public function dropColumn($tableName, $columnName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
    {
270
        $adapter = $this->getAdapter();
271 3
        if (!$adapter instanceof DirectActionInterface) {
272
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
273 3
        }
274 3
        $end = $this->startCommandTimer();
275 3
        $this->writeCommand('dropColumn', [$tableName, $columnName]);
276 3
        $adapter->dropColumn($tableName, $columnName);
277 3
        $end();
278
    }
279
280
    /**
281
     * {@inheritdoc}
282 5
     */
283
    public function addIndex(Table $table, Index $index)
284 5
    {
285 5
        $adapter = $this->getAdapter();
286 5
        if (!$adapter instanceof DirectActionInterface) {
287 5
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
288 5
        }
289
        $end = $this->startCommandTimer();
290
        $this->writeCommand('addIndex', [$table->getName(), $index->getColumns()]);
291
        $adapter->addIndex($table, $index);
292
        $end();
293 5
    }
294
295 5
    /**
296 5
     * {@inheritdoc}
297 5
     */
298 5 View Code Duplication
    public function dropIndex($tableName, $columns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299 5
    {
300
        $adapter = $this->getAdapter();
301
        if (!$adapter instanceof DirectActionInterface) {
302
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
303
        }
304
        $end = $this->startCommandTimer();
305
        $this->writeCommand('dropIndex', [$tableName, $columns]);
306
        $adapter->dropIndex($tableName, $columns);
307
        $end();
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313 View Code Duplication
    public function dropIndexByName($tableName, $indexName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
314
    {
315
        $adapter = $this->getAdapter();
316
        if (!$adapter instanceof DirectActionInterface) {
317
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
318
        }
319
        $end = $this->startCommandTimer();
320
        $this->writeCommand('dropIndexByName', [$tableName, $indexName]);
321
        $adapter->dropIndexByName($tableName, $indexName);
322
        $end();
323
    }
324
325
    /**
326
     * {@inheritdoc}
327
     */
328
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
329
    {
330
        $adapter = $this->getAdapter();
331
        if (!$adapter instanceof DirectActionInterface) {
332
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
333
        }
334
        $end = $this->startCommandTimer();
335
        $this->writeCommand('addForeignKey', [$table->getName(), $foreignKey->getColumns()]);
336
        $adapter->addForeignKey($table, $foreignKey);
337
        $end();
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343 View Code Duplication
    public function dropForeignKey($tableName, $columns, $constraint = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
344
    {
345
        $adapter = $this->getAdapter();
346
        if (!$adapter instanceof DirectActionInterface) {
347
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
348
        }
349
        $end = $this->startCommandTimer();
350
        $this->writeCommand('dropForeignKey', [$tableName, $columns]);
351
        $adapter->dropForeignKey($tableName, $columns, $constraint);
352
        $end();
353
    }
354
355
    /**
356
     * {@inheritdoc}
357
     */
358
    public function createDatabase($name, $options = [])
359
    {
360
        $end = $this->startCommandTimer();
361
        $this->writeCommand('createDatabase', [$name]);
362
        parent::createDatabase($name, $options);
363
        $end();
364
    }
365
366
    /**
367
     * {@inheritdoc}
368
     */
369
    public function dropDatabase($name)
370
    {
371
        $end = $this->startCommandTimer();
372
        $this->writeCommand('dropDatabase', [$name]);
373
        parent::dropDatabase($name);
374
        $end();
375
    }
376
377
    /**
378
     * {@inheritdoc}
379
     */
380
    public function createSchema($name = 'public')
381
    {
382
        $end = $this->startCommandTimer();
383
        $this->writeCommand('createSchema', [$name]);
384
        parent::createSchema($name);
385
        $end();
386
    }
387
388
    /**
389
     * {@inheritdoc}
390
     */
391
    public function dropSchema($name)
392
    {
393
        $end = $this->startCommandTimer();
394
        $this->writeCommand('dropSchema', [$name]);
395
        parent::dropSchema($name);
396
        $end();
397
    }
398
399
    /**
400
     * {@inheritdoc}
401
     */
402 View Code Duplication
    public function executeActions(Table $table, array $actions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
403
    {
404
        $end = $this->startCommandTimer();
405
        $this->writeCommand(sprintf('Altering table %s', $table->getName()));
406
        parent::executeActions($table, $actions);
407
        $end();
408
    }
409
}
410