Passed
Push — next ( a87628...835774 )
by Bas
02:38
created

AranguentConvertMigrationsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 2
b 0
f 0
dl 0
loc 72
ccs 15
cts 16
cp 0.9375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 12 3
A convertMigrationFile() 0 12 1
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Console\Migrations;
4
5
use Illuminate\Console\ConfirmableTrait;
6
use Illuminate\Database\Console\Migrations\BaseCommand;
7
use Illuminate\Database\Migrations\Migrator;
8
9
class AranguentConvertMigrationsCommand extends BaseCommand
10
{
11
    use ConfirmableTrait;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'aranguent:convert-migrations
19
                {--path= : The path to the migrations files to be converted}
20
                {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}';
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Convert existing migrations to Aranguent migrations';
27
28
    /**
29
     * The migrator instance.
30
     *
31
     * @var \Illuminate\Database\Migrations\Migrator
32
     */
33
    protected $migrator;
34
35
    /**
36
     * Create a new migration command instance.
37
     *
38
     * @param \Illuminate\Database\Migrations\Migrator $migrator
39
     *
40
     * @return void
41
     */
42 209
    public function __construct(Migrator $migrator)
43
    {
44 209
        parent::__construct();
45
46 209
        $this->migrator = $migrator;
47 209
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return void
53
     */
54 209
    public function handle()
55
    {
56 209
        if (!$this->confirmToProceed()) {
57
            return;
58
        }
59
60
        // Next, we will check to see if a path option has been defined. If it has
61
        // we will use the path relative to the root of this installation folder
62
        // so that migrations may be run for any path within the applications.
63 209
        $files = $this->migrator->getMigrationFiles($this->getMigrationPaths());
64 209
        foreach ($files as $file) {
65 209
            $this->convertMigrationFile($file);
66
        }
67 209
    }
68
69 209
    public function convertMigrationFile($filePath)
70
    {
71
        // Check for additional information to make sure we
72
        // partial matches aren't accidentally replaced.
73 209
        $replacements = [
74
            'Illuminate\Support\Facades\Schema'    => 'LaravelFreelancerNL\Aranguent\Facades\Schema',
75
            'Illuminate\Database\Schema\Blueprint' => 'LaravelFreelancerNL\Aranguent\Schema\Blueprint',
76
        ];
77
78 209
        $content = file_get_contents($filePath);
79 209
        $content = str_replace(array_keys($replacements), $replacements, $content);
80 209
        file_put_contents($filePath, $content);
81 209
    }
82
}
83