Completed
Pull Request — master (#46)
by John
03:27
created

Migrator::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nwidart\Modules\Migrations;
4
5
use Nwidart\Modules\Module;
6
7
class Migrator
8
{
9
    /**
10
     * Pingpong Module instance.
11
     *
12
     * @var \Nwidart\Modules\Module
13
     */
14
    protected $module;
15
16
    /**
17
     * Laravel Application instance.
18
     *
19
     * @var \Illuminate\Foundation\Application.
20
     */
21
    protected $laravel;
22
23
    /**
24
     * Create new instance.
25
     *
26
     * @param \Nwidart\Modules\Module $module
27
     */
28 1
    public function __construct(Module $module)
29
    {
30 1
        $this->module = $module;
31 1
        $this->laravel = $module->getLaravel();
0 ignored issues
show
Documentation Bug introduced by
It seems like $module->getLaravel() of type object<Illuminate\Foundation\Application> is incompatible with the declared type object<Illuminate\Foundation\Application.> of property $laravel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32 1
    }
33
34
    /**
35
     * @return Module
36
     */
37 1
    public function getModule()
38
    {
39 1
        return $this->module;
40
    }
41
42
    /**
43
     * Get migration path.
44
     *
45
     * @return string
46
     */
47 1
    public function getPath()
48
    {
49 1
        $config = $this->module->get('migration');
50
51 1
        $path = (is_array($config) && array_key_exists('path', $config)) ? $config['path'] : config('modules.paths.generator.migration');
52
53 1
        return $this->module->getExtraPath($path);
54
    }
55
56
    /**
57
     * Get migration files.
58
     *
59
     * @param boolean $reverse
60
     * @return array
61
     */
62
    public function getMigrations($reverse = false)
63
    {
64
        $files = $this->laravel['files']->glob($this->getPath() . '/*_*.php');
65
66
        // Once we have the array of files in the directory we will just remove the
67
        // extension and take the basename of the file which is all we need when
68
        // finding the migrations that haven't been run against the databases.
69
        if ($files === false) {
70
            return array();
71
        }
72
73
        $files = array_map(function ($file) {
74
            return str_replace('.php', '', basename($file));
75
76
        }, $files);
77
78
        // Once we have all of the formatted file names we will sort them and since
79
        // they all start with a timestamp this should give us the migrations in
80
        // the order they were actually created by the application developers.
81
        sort($files);
82
83
        if ($reverse) {
84
            return array_reverse($files);
85
        }
86
87
        return $files;
88
    }
89
90
    /**
91
     * Rollback migration.
92
     *
93
     * @return array
94
     */
95 View Code Duplication
    public function rollback()
1 ignored issue
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...
96
    {
97
        $migrations = $this->getLast($this->getMigrations(true));
98
99
        $this->requireFiles($migrations->toArray());
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $migrations (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
100
101
        $migrated = [];
102
103
        foreach ($migrations as $migration) {
104
            $data = $this->find($migration);
105
106
            if ($data->count()) {
107
                $migrated[] = $migration;
108
109
                $this->down($migration);
110
111
                $data->delete();
112
            }
113
        }
114
115
        return $migrated;
116
    }
117
118
    /**
119
     * Reset migration.
120
     *
121
     * @return array
122
     */
123 View Code Duplication
    public function reset()
1 ignored issue
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
    {
125
        $migrations = $this->getMigrations(true);
126
127
        $this->requireFiles($migrations);
128
129
        $migrated = [];
130
131
        foreach ($migrations as $migration) {
132
            $data = $this->find($migration);
133
134
            if ($data->count()) {
135
                $migrated[] = $migration;
136
137
                $this->down($migration);
138
139
                $data->delete();
140
            }
141
        }
142
143
        return $migrated;
144
    }
145
146
    /**
147
     * Run down schema from the given migration name.
148
     *
149
     * @param string $migration
150
     */
151
    public function down($migration)
152
    {
153
        $this->resolve($migration)->down();
154
    }
155
156
    /**
157
     * Run up schema from the given migration name.
158
     *
159
     * @param string $migration
160
     */
161
    public function up($migration)
162
    {
163
        $this->resolve($migration)->up();
164
    }
165
166
    /**
167
     * Resolve a migration instance from a file.
168
     *
169
     * @param string $file
170
     *
171
     * @return object
172
     */
173
    public function resolve($file)
174
    {
175
        $file = implode('_', array_slice(explode('_', $file), 4));
176
177
        $class = studly_case($file);
178
179
        return new $class();
180
    }
181
182
    /**
183
     * Require in all the migration files in a given path.
184
     *
185
     * @param array  $files
186
     */
187
    public function requireFiles(array $files)
188
    {
189
        $path = $this->getPath();
190
        foreach ($files as $file) {
191
            $this->laravel['files']->requireOnce($path . '/' . $file . '.php');
192
        }
193
    }
194
195
    /**
196
     * Get table instance.
197
     *
198
     * @return string
199
     */
200
    public function table()
201
    {
202
        return $this->laravel['db']->table(config('database.migrations'));
203
    }
204
205
    /**
206
     * Find migration data from database by given migration name.
207
     *
208
     * @param string $migration
209
     *
210
     * @return object
211
     */
212
    public function find($migration)
213
    {
214
        return $this->table()->whereMigration($migration);
0 ignored issues
show
Bug introduced by
The method whereMigration cannot be called on $this->table() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
215
    }
216
217
    /**
218
     * Save new migration to database.
219
     *
220
     * @param string $migration
221
     *
222
     * @return mixed
223
     */
224
    public function log($migration)
225
    {
226
        return $this->table()->insert([
0 ignored issues
show
Bug introduced by
The method insert cannot be called on $this->table() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
227
            'migration' => $migration,
228
            'batch' => $this->getNextBatchNumber(),
229
        ]);
230
    }
231
232
    /**
233
     * Get the next migration batch number.
234
     *
235
     * @return int
236
     */
237
    public function getNextBatchNumber()
238
    {
239
        return $this->getLastBatchNumber() + 1;
0 ignored issues
show
Bug introduced by
The call to getLastBatchNumber() misses a required argument $migrations.

This check looks for function calls that miss required arguments.

Loading history...
240
    }
241
242
    /**
243
     * Get the last migration batch number.
244
     *
245
     * @param array $migrations
246
     * @return int
247
     */
248
    public function getLastBatchNumber($migrations)
249
    {
250
        return $this->table()
0 ignored issues
show
Bug introduced by
The method whereIn cannot be called on $this->table() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
251
            ->whereIn('migration', $migrations)
252
            ->max('batch');
253
    }
254
255
    /**
256
     * Get the last migration batch.
257
     *
258
     * @param array $migrations
259
     *
260
     * @return array
261
     */
262
    public function getLast($migrations)
263
    {
264
        $query = $this->table()
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->table() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
265
            ->where('batch', $this->getLastBatchNumber($migrations))
266
            ->whereIn('migration', $migrations);
267
268
        $result = $query->orderBy('migration', 'desc')->get();
269
270
        return collect($result)->map(function ($item) {
271
            return (array) $item;
272
        })->lists('migration');
273
    }
274
275
    /**
276
     * Get the ran migrations.
277
     *
278
     * @return array
279
     */
280
    public function getRan()
281
    {
282
        return $this->table()->lists('migration');
0 ignored issues
show
Bug introduced by
The method lists cannot be called on $this->table() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
283
    }
284
}
285