Completed
Pull Request — master (#1357)
by José
01:58
created

AbstractMigration::getQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Migration
28
 */
29
namespace Phinx\Migration;
30
31
use Phinx\Db\Adapter\AdapterInterface;
32
use Phinx\Db\Table;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
/**
37
 * Abstract Migration Class.
38
 *
39
 * It is expected that the migrations you write extend from this class.
40
 *
41
 * This abstract class proxies the various database methods to your specified
42
 * adapter.
43
 *
44
 * @author Rob Morgan <[email protected]>
45
 */
46
abstract class AbstractMigration implements MigrationInterface
47
{
48
    /**
49
     * @var string
50
     */
51
    protected $environment;
52
    /**
53
     * @var float
54
     */
55
    protected $version;
56
57
    /**
58
     * @var \Phinx\Db\Adapter\AdapterInterface
59
     */
60
    protected $adapter;
61
62
    /**
63
     * @var \Symfony\Component\Console\Output\OutputInterface
64
     */
65
    protected $output;
66
67
    /**
68
     * @var \Symfony\Component\Console\Input\InputInterface
69
     */
70
    protected $input;
71
72
    /**
73
     * Whether this migration is being applied or reverted
74
     *
75
     * @var bool
76
     */
77
    protected $isMigratingUp = true;
78
79
    /**
80
     * Class Constructor.
81
     *
82 406
     * @param string $environment Environment Detected
83
     * @param int $version Migration Version
84 406
     * @param \Symfony\Component\Console\Input\InputInterface|null $input
85 406
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
86 384
     */
87 384
    final public function __construct($environment, $version, InputInterface $input = null, OutputInterface $output = null)
88 406
    {
89 384
        $this->environment = $environment;
90 384
        $this->version = $version;
0 ignored issues
show
Documentation Bug introduced by
The property $version was declared of type double, but $version is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
91
92 406
        if (!is_null($input)) {
93 406
            $this->setInput($input);
94
        }
95
96
        if (!is_null($output)) {
97
            $this->setOutput($output);
98
        }
99
100 406
        $this->init();
101
    }
102 406
103
    /**
104
     * Initialize method.
105
     *
106
     * @return void
107 1
     */
108
    protected function init()
109 1
    {
110
    }
111
112
    /**
113
     * {@inheritdoc}
114 1
     */
115
    public function up()
116 1
    {
117
    }
118
119
    /**
120
     * {@inheritdoc}
121 21
     */
122
    public function down()
123 21
    {
124 21
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function setAdapter(AdapterInterface $adapter)
130 13
    {
131
        $this->adapter = $adapter;
132 13
133
        return $this;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138 384
     */
139
    public function getAdapter()
140 384
    {
141 384
        return $this->adapter;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 2
    public function setInput(InputInterface $input)
148
    {
149 2
        $this->input = $input;
150
151
        return $this;
152
    }
153
154
    /**
155 385
     * {@inheritdoc}
156
     */
157 385
    public function getInput()
158 385
    {
159
        return $this->input;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164 3
     */
165
    public function setOutput(OutputInterface $output)
166 3
    {
167
        $this->output = $output;
168
169
        return $this;
170
    }
171
172 140
    /**
173
     * {@inheritdoc}
174 140
     */
175
    public function getOutput()
176
    {
177
        return $this->output;
178
    }
179
180 1
    /**
181
     * {@inheritdoc}
182 1
     */
183 1
    public function getName()
184
    {
185
        return get_class($this);
186
    }
187
188
    /**
189 279
     * {@inheritdoc}
190
     */
191 279
    public function getEnvironment()
192
    {
193
        return $this->environment;
194
    }
195
196
    /**
197 10
     * {@inheritdoc}
198
     */
199 10
    public function setVersion($version)
200 10
    {
201
        $this->version = $version;
202
203
        return $this;
204
    }
205
206 2
    /**
207
     * {@inheritdoc}
208 2
     */
209
    public function getVersion()
210
    {
211
        return $this->version;
212
    }
213
214 3
    /**
215
     * {@inheritdoc}
216 3
     */
217
    public function setMigratingUp($isMigratingUp)
218
    {
219
        $this->isMigratingUp = $isMigratingUp;
220
221
        return $this;
222 1
    }
223
224 1
    /**
225
     * {@inheritdoc}
226
     */
227
    public function isMigratingUp()
228
    {
229
        return $this->isMigratingUp;
230 1
    }
231
232 1
    /**
233
     * {@inheritdoc}
234
     */
235
    public function execute($sql)
236
    {
237
        return $this->getAdapter()->execute($sql);
238 1
    }
239
240 1
    /**
241
     * {@inheritdoc}
242
     */
243
    public function query($sql)
244
    {
245
        return $this->getAdapter()->query($sql);
246 3
    }
247
248
    /**
249 3
     * {@inheritdoc}
250 2
     */
251 2
    public function getQueryBuilder()
252 3
    {
253 3
        return $this->getAdapter()->getQueryBuilder();
254
    }
255
256
    /**
257
     * {@inheritdoc}
258 1
     */
259
    public function fetchRow($sql)
260 1
    {
261 1
        return $this->getAdapter()->fetchRow($sql);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266 1
     */
267
    public function fetchAll($sql)
268 1
    {
269 1
        return $this->getAdapter()->fetchAll($sql);
270
    }
271
272
    /**
273
     * {@inheritdoc}
274 1
     */
275 View Code Duplication
    public function insert($table, $data)
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...
276 1
    {
277
        // convert to table object
278
        if (is_string($table)) {
279
            $table = new Table($table, [], $this->getAdapter());
280
        }
281
        $table->insert($data)->save();
282 5
    }
283
284 5
    /**
285
     * {@inheritdoc}
286
     */
287
    public function createDatabase($name, $options)
288
    {
289
        $this->getAdapter()->createDatabase($name, $options);
290
    }
291
292
    /**
293 1
     * {@inheritdoc}
294
     */
295 1
    public function dropDatabase($name)
296 1
    {
297
        $this->getAdapter()->dropDatabase($name);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303
    public function hasTable($tableName)
304
    {
305
        return $this->getAdapter()->hasTable($tableName);
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    public function table($tableName, $options = [])
312
    {
313
        return new Table($tableName, $options, $this->getAdapter());
314
    }
315
316
    /**
317
     * A short-hand method to drop the given database table.
318
     *
319
     * @param string $tableName Table Name
320
     * @return void
321
     */
322
    public function dropTable($tableName)
323
    {
324
        $this->table($tableName)->drop();
325
    }
326
}
327