Completed
Pull Request — master (#758)
by Nicolas
03:37
created

Migrator::getModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Migrations;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use Nwidart\Modules\Module;
9
use Nwidart\Modules\Support\Config\GenerateConfigReader;
10
11
class Migrator
12
{
13
    /**
14
     * Pingpong Module instance.
15
     *
16
     * @var \Nwidart\Modules\Module
17
     */
18
    protected $module;
19
20
    /**
21
     * Laravel Application instance.
22
     *
23
     * @var Application.
24
     */
25
    protected $laravel;
26
27
    /**
28
     * The database connection to be used
29
     *
30
     * @var string
31
     */
32
    protected $database = '';
33
34
    /**
35
     * Create new instance.
36
     *
37
     * @param \Nwidart\Modules\Module $module
38
     */
39 1
    public function __construct(Module $module)
40
    {
41 1
        $this->module = $module;
42 1
        $this->laravel = $module->getLaravel();
43 1
    }
44
45
    /**
46
     * Set the database connection to be used
47
     *
48
     * @param $database
49
     *
50
     * @return $this
51
     */
52
    public function setDatabase($database)
53
    {
54
        if (is_string($database) && $database) {
55
            $this->database = $database;
56
        }
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return Module
63
     */
64 1
    public function getModule()
65
    {
66 1
        return $this->module;
67
    }
68
69
    /**
70
     * Get migration path.
71
     *
72
     * @return string
73
     */
74 1
    public function getPath()
75
    {
76 1
        $config = $this->module->get('migration');
77
78 1
        $migrationPath = GenerateConfigReader::read('migration');
79 1
        $path = (is_array($config) && array_key_exists('path', $config)) ? $config['path'] : $migrationPath->getPath();
80
81 1
        return $this->module->getExtraPath($path);
82
    }
83
84
    /**
85
     * Get migration files.
86
     *
87
     * @param boolean $reverse
88
     * @return array
89
     */
90
    public function getMigrations($reverse = false)
91
    {
92
        $files = $this->laravel['files']->glob($this->getPath() . '/*_*.php');
93
94
        // Once we have the array of files in the directory we will just remove the
95
        // extension and take the basename of the file which is all we need when
96
        // finding the migrations that haven't been run against the databases.
97
        if ($files === false) {
98
            return [];
99
        }
100
101
        $files = array_map(function ($file) {
102
            return str_replace('.php', '', basename($file));
103
        }, $files);
104
105
        // Once we have all of the formatted file names we will sort them and since
106
        // they all start with a timestamp this should give us the migrations in
107
        // the order they were actually created by the application developers.
108
        sort($files);
109
110
        if ($reverse) {
111
            return array_reverse($files);
112
        }
113
114
        return $files;
115
    }
116
117
    /**
118
     * Run down schema from the given migration name.
119
     *
120
     * @param string $migration
121
     */
122
    public function down($migration)
123
    {
124
        $this->resolve($migration)->down();
125
    }
126
127
    /**
128
     * Run up schema from the given migration name.
129
     *
130
     * @param string $migration
131
     */
132
    public function up($migration)
133
    {
134
        $this->resolve($migration)->up();
135
    }
136
137
    /**
138
     * Resolve a migration instance from a file.
139
     *
140
     * @param string $file
141
     *
142
     * @return object
143
     */
144
    public function resolve($file)
145
    {
146
        $file = implode('_', array_slice(explode('_', $file), 4));
147
148
        $class = Str::studly($file);
149
150
        return new $class();
151
    }
152
153
    /**
154
     * Require in all the migration files in a given path.
155
     *
156
     * @param array  $files
157
     */
158
    public function requireFiles(array $files)
159
    {
160
        $path = $this->getPath();
161
        foreach ($files as $file) {
162
            $this->laravel['files']->requireOnce($path . '/' . $file . '.php');
163
        }
164
    }
165
166
    /**
167
     * Get table instance.
168
     *
169
     * @return \Illuminate\Database\Query\Builder
170
     */
171
    public function table()
172
    {
173
        return $this->laravel['db']->connection($this->database ?: null)->table(config('database.migrations'));
174
    }
175
176
    /**
177
     * Find migration data from database by given migration name.
178
     *
179
     * @param string $migration
180
     *
181
     * @return object
182
     */
183
    public function find($migration)
184
    {
185
        return $this->table()->whereMigration($migration);
186
    }
187
188
    /**
189
     * Save new migration to database.
190
     *
191
     * @param string $migration
192
     *
193
     * @return mixed
194
     */
195
    public function log($migration)
196
    {
197
        return $this->table()->insert([
198
            'migration' => $migration,
199
            'batch' => $this->getNextBatchNumber(),
200
        ]);
201
    }
202
203
    /**
204
     * Get the next migration batch number.
205
     *
206
     * @return int
207
     */
208
    public function getNextBatchNumber()
209
    {
210
        return $this->getLastBatchNumber() + 1;
211
    }
212
213
    /**
214
     * Get the last migration batch number.
215
     *
216
     * @param array|null $migrations
217
     * @return int
218
     */
219
    public function getLastBatchNumber($migrations = null)
220
    {
221
        $table = $this->table();
222
223
        if (is_array($migrations)) {
224
            $table = $table->whereIn('migration', $migrations);
225
        }
226
227
        return $table->max('batch');
228
    }
229
230
    /**
231
     * Get the last migration batch.
232
     *
233
     * @param array $migrations
234
     *
235
     * @return Collection
236
     */
237
    public function getLast($migrations)
238
    {
239
        $query = $this->table()
240
            ->where('batch', $this->getLastBatchNumber($migrations))
241
            ->whereIn('migration', $migrations);
242
243
        $result = $query->orderBy('migration', 'desc')->get();
244
245
        return collect($result)->map(function ($item) {
246
            return (array) $item;
247
        })->pluck('migration');
248
    }
249
250
    /**
251
     * Get the ran migrations.
252
     *
253
     * @return Collection
254
     */
255
    public function getRan()
256
    {
257
        return $this->table()->pluck('migration');
258
    }
259
}
260