Completed
Pull Request — master (#7)
by Asif
08:58
created

MigrationCommand::fire()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 0
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel MultiLang package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\LaravelMultiLang\Console;
12
13
use Illuminate\Console\Command;
14
use Illuminate\Support\Facades\Config;
15
16
class MigrationCommand extends Command
17
{
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'multilang:migration';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Creates a migration following the multilang specifications.';
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return void
36
     */
37
    public function handle()
38
    {
39
        $table = Config::get('multilang.db.texts_table');
40
41
        if ('' == $table) {
42
            $this->error('Couldn\'t create migration.' . PHP_EOL . 'Table name can\'t be empty. Check your configuration.');
43
44
            return;
45
        }
46
47
        $this->line('');
48
        $this->info('Tables: ' . $table);
49
50
        $message = 'A migration that creates "' . $table . '" tables will be created in database/migrations directory';
51
52
        $this->comment($message);
53
        $this->line('');
54
55
        if ($this->confirm('Proceed with the migration creation? [Yes|no]', true)) {
56
            $this->line('');
57
58
            $this->info('Creating migration...');
59
            if ($this->createMigration($table)) {
60
                $this->info('Migration successfully created!');
61
            } else {
62
                $this->error(
63
                    'Couldn\'t create migration.' . PHP_EOL . ' Check the write permissions
64
                    within the database/migrations directory.'
65
                );
66
            }
67
68
            $this->line('');
69
        }
70
    }
71
72
    /**
73
     * Create the migration.
74
     *
75
     * @param  string $table
76
     * @return bool
77
     */
78
    protected function createMigration($table)
79
    {
80
        $migrationFile = base_path("database/migrations") . "/" . date('Y_m_d_His') . "_create_multi_lang_texts_table.php";
81
82
        if (file_exists($migrationFile)) {
83
            return false;
84
        }
85
86
        $stubPath = __DIR__ . '/../../stubs/migrations/texts.stub';
87
        $content = file_get_contents($stubPath);
88
        if (empty($content)) {
89
            return false;
90
        }
91
92
        $data = str_replace('{{TEXTS_TABLE}}', $table, $content);
93
94
        if (! file_put_contents($migrationFile, $data)) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
}
101