Completed
Push — master ( 05902f...9e6322 )
by mark
01:41 queued 11s
created

TextWrapper::getMigrate()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 18

Duplication

Lines 17
Ratio 94.44 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 17
loc 18
ccs 0
cts 11
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 24
nop 2
crap 56
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\Wrapper;
9
10
use Phinx\Console\PhinxApplication;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Output\StreamOutput;
13
14
/**
15
 * Phinx text wrapper: a way to run `status`, `migrate`, and `rollback` commands
16
 * and get the output of the command back as plain text.
17
 *
18
 * @author Woody Gilk <[email protected]>
19
 */
20
class TextWrapper
21
{
22
    /**
23
     * @var \Phinx\Console\PhinxApplication
24
     */
25
    protected $app;
26
27
    /**
28
     * @var array
29
     */
30
    protected $options;
31
32
    /**
33
     * @var int
34
     */
35
    protected $exitCode;
36
37
    /**
38
     * @param \Phinx\Console\PhinxApplication $app Application
39
     * @param array $options Options
40
     */
41
    public function __construct(PhinxApplication $app, array $options = [])
42
    {
43
        $this->app = $app;
44
        $this->options = $options;
45
    }
46
47
    /**
48
     * Get the application instance.
49
     *
50
     * @return \Phinx\Console\PhinxApplication
51
     */
52
    public function getApp()
53
    {
54
        return $this->app;
55
    }
56
57
    /**
58
     * Returns the exit code from the last run command.
59
     *
60
     * @return int
61
     */
62
    public function getExitCode()
63
    {
64
        return $this->exitCode;
65
    }
66
67
    /**
68
     * Returns the output from running the "status" command.
69
     *
70
     * @param string|null $env environment name (optional)
71
     *
72
     * @return string
73
     */
74
    public function getStatus($env = null)
75
    {
76
        $command = ['status'];
77
        if ($env ?: $this->hasOption('environment')) {
78
            $command += ['-e' => $env ?: $this->getOption('environment')];
79
        }
80
        if ($this->hasOption('configuration')) {
81
            $command += ['-c' => $this->getOption('configuration')];
82
        }
83
        if ($this->hasOption('parser')) {
84
            $command += ['-p' => $this->getOption('parser')];
85
        }
86
        if ($this->hasOption('format')) {
87
            $command += ['-f' => $this->getOption('format')];
88
        }
89
90
        return $this->executeRun($command);
91
    }
92
93
    /**
94
     * Returns the output from running the "migrate" command.
95
     *
96
     * @param string|null $env environment name (optional)
97
     * @param string|null $target target version (optional)
98
     *
99
     * @return string
100
     */
101 View Code Duplication
    public function getMigrate($env = null, $target = 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...
102
    {
103
        $command = ['migrate'];
104
        if ($env ?: $this->hasOption('environment')) {
105
            $command += ['-e' => $env ?: $this->getOption('environment')];
106
        }
107
        if ($this->hasOption('configuration')) {
108
            $command += ['-c' => $this->getOption('configuration')];
109
        }
110
        if ($this->hasOption('parser')) {
111
            $command += ['-p' => $this->getOption('parser')];
112
        }
113
        if ($target) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $target of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
114
            $command += ['-t' => $target];
115
        }
116
117
        return $this->executeRun($command);
118
    }
119
120
    /**
121
     * Returns the output from running the "seed:run" command.
122
     *
123
     * @param string|null $env Environment name
124
     * @param string|null $target Target version
125
     * @param string[]|string|null $seed Array of seed names or seed name
126
     *
127
     * @return string
128
     */
129
    public function getSeed($env = null, $target = null, $seed = null)
130
    {
131
        $command = ['seed:run'];
132
        if ($env ?: $this->hasOption('environment')) {
133
            $command += ['-e' => $env ?: $this->getOption('environment')];
134
        }
135
        if ($this->hasOption('configuration')) {
136
            $command += ['-c' => $this->getOption('configuration')];
137
        }
138
        if ($this->hasOption('parser')) {
139
            $command += ['-p' => $this->getOption('parser')];
140
        }
141
        if ($target) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $target of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
142
            $command += ['-t' => $target];
143
        }
144
        if ($seed) {
145
            $seed = (array)$seed;
146
            $command += ['-s' => $seed];
147
        }
148
149
        return $this->executeRun($command);
150
    }
151
152
    /**
153
     * Returns the output from running the "rollback" command.
154
     *
155
     * @param string|null $env Environment name (optional)
156
     * @param mixed $target Target version, or 0 (zero) fully revert (optional)
157
     *
158
     * @return string
159
     */
160 View Code Duplication
    public function getRollback($env = null, $target = 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...
161
    {
162
        $command = ['rollback'];
163
        if ($env ?: $this->hasOption('environment')) {
164
            $command += ['-e' => $env ?: $this->getOption('environment')];
165
        }
166
        if ($this->hasOption('configuration')) {
167
            $command += ['-c' => $this->getOption('configuration')];
168
        }
169
        if ($this->hasOption('parser')) {
170
            $command += ['-p' => $this->getOption('parser')];
171
        }
172
        if (isset($target)) {
173
            // Need to use isset() with rollback, because -t0 is a valid option!
174
            // See https://book.cakephp.org/phinx/0/en/commands.html#the-rollback-command
175
            $command += ['-t' => $target];
176
        }
177
178
        return $this->executeRun($command);
179
    }
180
181
    /**
182
     * Check option from options array
183
     *
184
     * @param string $key Key
185
     *
186
     * @return bool
187
     */
188
    protected function hasOption($key)
189
    {
190
        return isset($this->options[$key]);
191
    }
192
193
    /**
194
     * Get option from options array
195
     *
196
     * @param string $key Key
197
     *
198
     * @return string|null
199
     */
200
    protected function getOption($key)
201
    {
202
        if (!isset($this->options[$key])) {
203
            return null;
204
        }
205
206
        return $this->options[$key];
207
    }
208
209
    /**
210
     * Set option in options array
211
     *
212
     * @param string $key Key
213
     * @param string $value Value
214
     *
215
     * @return $this
216
     */
217
    public function setOption($key, $value)
218
    {
219
        $this->options[$key] = $value;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Execute a command, capturing output and storing the exit code.
226
     *
227
     * @param array $command Command
228
     *
229
     * @return string
230
     */
231
    protected function executeRun(array $command)
232
    {
233
        // Output will be written to a temporary stream, so that it can be
234
        // collected after running the command.
235
        $stream = fopen('php://temp', 'w+');
236
237
        // Execute the command, capturing the output in the temporary stream
238
        // and storing the exit code for debugging purposes.
239
        $this->exitCode = $this->app->doRun(new ArrayInput($command), new StreamOutput($stream));
240
241
        // Get the output of the command and close the stream, which will
242
        // destroy the temporary file.
243
        $result = stream_get_contents($stream, -1, 0);
244
        fclose($stream);
245
246
        return $result;
247
    }
248
}
249