Completed
Pull Request — master (#1175)
by
unknown
01:55
created

ProxyAdapter::invertAddCustomColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
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;
32
use Phinx\Db\Table\Column;
33
use Phinx\Db\Table\Index;
34
use Phinx\Db\Table\ForeignKey;
35
use Phinx\Migration\IrreversibleMigrationException;
36
37
/**
38
 * Phinx Proxy Adapter.
39
 *
40
 * Used for recording migration commands to automatically reverse them.
41
 *
42
 * @author Rob Morgan <[email protected]>
43
 */
44
class ProxyAdapter extends AdapterWrapper
45
{
46
    /**
47
     * @var array
48
     */
49
    protected $commands;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getAdapterType()
55
    {
56
        return 'ProxyAdapter';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    public function createTable(Table $table)
63
    {
64 4
        $this->recordCommand('createTable', [$table->getName()]);
65 4
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 4
    public function renameTable($tableName, $newTableName)
71
    {
72 4
        $this->recordCommand('renameTable', [$tableName, $newTableName]);
73 4
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function dropTable($tableName)
79
    {
80
        $this->recordCommand('dropTable', [$tableName]);
81
    }
82
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function truncateTable($tableName)
88
    {
89
        $this->recordCommand('truncateTable', [$tableName]);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 4
    public function addColumn(Table $table, Column $column)
96
    {
97 4
        $this->recordCommand('addColumn', [$table, $column]);
98 4
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 4
    public function renameColumn($tableName, $columnName, $newColumnName)
104
    {
105 4
        $this->recordCommand('renameColumn', [$tableName, $columnName, $newColumnName]);
106 4
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function changeColumn($tableName, $columnName, Column $newColumn)
112
    {
113
        $this->recordCommand('changeColumn', [$tableName, $columnName, $newColumn]);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function dropColumn($tableName, $columnName)
120
    {
121
        $this->recordCommand('dropColumn', [$tableName, $columnName]);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 1
    public function addIndex(Table $table, Index $index)
128
    {
129 1
        $this->recordCommand('addIndex', [$table, $index]);
130 1
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function dropIndex($tableName, $columns, $options = [])
136
    {
137
        $this->recordCommand('dropIndex', [$tableName, $columns, $options]);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function dropIndexByName($tableName, $indexName)
144
    {
145
        $this->recordCommand('dropIndexByName', [$tableName, $indexName]);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 4
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
152
    {
153 4
        $this->recordCommand('addForeignKey', [$table, $foreignKey]);
154 4
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function dropForeignKey($tableName, $columns, $constraint = null)
160
    {
161
        $this->recordCommand('dropForeignKey', [$columns, $constraint]);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function createDatabase($name, $options = [])
168
    {
169
        $this->recordCommand('createDatabase', [$name, $options]);
170
    }
171
172
    /**
173
     * Record a command for execution later.
174
     *
175
     * @param string $name Command Name
176
     * @param array $arguments Command Arguments
177
     * @return void
178
     */
179 10
    public function recordCommand($name, $arguments)
180
    {
181 10
        $this->commands[] = [
182 10
            'name'      => $name,
183
            'arguments' => $arguments
184 10
        ];
185 10
    }
186
187
    /**
188
     * Sets an array of recorded commands.
189
     *
190
     * @param array $commands Commands
191
     * @return ProxyAdapter
192
     */
193
    public function setCommands($commands)
194
    {
195
        $this->commands = $commands;
196
        return $this;
197
    }
198
199
    /**
200
     * Gets an array of the recorded commands.
201
     *
202
     * @return array
203
     */
204 11
    public function getCommands()
205
    {
206 11
        return $this->commands;
207
    }
208
209
    /**
210
     * Gets an array of the recorded commands in reverse.
211
     *
212
     * @throws IrreversibleMigrationException if a command cannot be reversed.
213
     * @return array
214
     */
215 11
    public function getInvertedCommands()
216
    {
217 11
        if ($this->getCommands() === null) {
218 1
            return [];
219
        }
220
221 10
        $invCommands = [];
222
        $supportedCommands = [
223 10
            'createTable', 'renameTable', 'addColumn',
224 10
            'renameColumn', 'addIndex', 'addForeignKey'
225 10
        ];
226 10
        foreach (array_reverse($this->getCommands()) as $command) {
227 10
            if (!in_array($command['name'], $supportedCommands)) {
228 1
                throw new IrreversibleMigrationException(sprintf(
229 1
                    'Cannot reverse a "%s" command',
230 1
                    $command['name']
231 1
                ));
232
            }
233 9
            $invertMethod = 'invert' . ucfirst($command['name']);
234 9
            $invertedCommand = $this->$invertMethod($command['arguments']);
235 9
            $invCommands[] = [
236 9
                'name'      => $invertedCommand['name'],
237 9
                'arguments' => $invertedCommand['arguments']
238 9
            ];
239 9
        }
240
241 9
        return $invCommands;
242
    }
243
244
    /**
245
     * Execute the recorded commands.
246
     *
247
     * @return void
248
     */
249 View Code Duplication
    public function executeCommands()
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...
250
    {
251
        $commands = $this->getCommands();
252
        foreach ($commands as $command) {
253
            call_user_func_array([$this->getAdapter(), $command['name']], $command['arguments']);
254
        }
255
    }
256
257
    /**
258
     * Execute the recorded commands in reverse.
259
     *
260
     * @return void
261
     */
262 4 View Code Duplication
    public function executeInvertedCommands()
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...
263
    {
264 4
        $commands = $this->getInvertedCommands();
265 4
        foreach ($commands as $command) {
266 3
            call_user_func_array([$this->getAdapter(), $command['name']], $command['arguments']);
267 4
        }
268 4
    }
269
270
    /**
271
     * Returns the reverse of a createTable command.
272
     *
273
     * @param array $args Method Arguments
274
     * @return array
275
     */
276 4
    public function invertCreateTable($args)
277
    {
278 4
        return ['name' => 'dropTable', 'arguments' => [$args[0]]];
279
    }
280
281
    /**
282
     * Returns the reverse of a renameTable command.
283
     *
284
     * @param array $args Method Arguments
285
     * @return array
286
     */
287 4
    public function invertRenameTable($args)
288
    {
289 4
        return ['name' => 'renameTable', 'arguments' => [$args[1], $args[0]]];
290
    }
291
292
    /**
293
     * Returns the reverse of a addColumn command.
294
     *
295
     * @param array $args Method Arguments
296
     * @return array
297
     */
298 4
    public function invertAddColumn($args)
299
    {
300 4
        return ['name' => 'dropColumn', 'arguments' => [$args[0]->getName(), $args[1]->getName()]];
301
    }
302
303
    /**
304
     * Returns the reverse of a addCustomColumn command.
305
     *
306
     * @param array $args Method Arguments
307
     * @return array
308
     */
309 4
    public function invertAddCustomColumn($args)
310
    {
311 4
        return $this->invertAddColumn($args);
312
    }
313
314
    /**
315
     * Returns the reverse of a renameColumn command.
316
     *
317
     * @param array $args Method Arguments
318
     * @return array
319
     */
320 1
    public function invertRenameColumn($args)
321
    {
322 1
        return ['name' => 'renameColumn', 'arguments' => [$args[0], $args[2], $args[1]]];
323
    }
324
325
    /**
326
     * Returns the reverse of a addIndex command.
327
     *
328
     * @param array $args Method Arguments
329
     * @return array
330
     */
331 4
    public function invertAddIndex($args)
332
    {
333 4
        return ['name' => 'dropIndex', 'arguments' => [$args[0]->getName(), $args[1]->getColumns()]];
334
    }
335
336
    /**
337
     * Returns the reverse of a addForeignKey command.
338
     *
339
     * @param array $args Method Arguments
340
     * @return array
341
     */
342
    public function invertAddForeignKey($args)
343
    {
344
        return ['name' => 'dropForeignKey', 'arguments' => [$args[0]->getName(), $args[1]->getColumns()]];
345
    }
346
}
347