Passed
Push — master ( 39e5a8...610dd1 )
by Yaroslav
03:43
created

MakeMigrationCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 76
ccs 21
cts 22
cp 0.9545
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A makeGeonamesMigration() 0 18 5
A handle() 0 9 2
1
<?php
2
3
namespace LaraGeoData\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
9
class MakeMigrationCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'geonames:make:migration
17
        {type : Migration type. }
18
        {--suffix= : Suffix used for specify country if need. }
19
    ';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Publish specific migrations.';
27
28
    /**
29
     * Path where stored all templates.
30
     *
31
     * @var string
32
     */
33
    protected string $templatesPath = __DIR__ . '/../../../database/migrations/';
34
35
    /**
36
     * The filesystem instance.
37
     *
38
     * @var Filesystem
39
     */
40
    protected Filesystem $files;
41
42
    /**
43
     * Create a new command instance.
44
     *
45
     * @param Filesystem $files
46
     *
47
     * @return void
48
     */
49 7
    public function __construct(Filesystem $files)
50
    {
51 7
        parent::__construct();
52
53 7
        $this->files = $files;
54 7
    }
55
56 3
    public function handle()
57
    {
58 3
        $type       = $this->argument('type');
59 3
        $methodName = 'make' . Str::ucfirst(Str::camel($type)) . 'Migration';
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type array; however, parameter $value of Illuminate\Support\Str::camel() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $methodName = 'make' . Str::ucfirst(Str::camel(/** @scrutinizer ignore-type */ $type)) . 'Migration';
Loading history...
60 3
        if (!method_exists($this, $methodName)) {
61
            throw new \Exception("Migration type [{$type}] not supported");
62
        }
63
64 3
        return $this->{$methodName}();
65
    }
66
67 3
    protected function makeGeonamesMigration(): int
68
    {
69 3
        $suffix = $this->option('suffix');
70 3
        if ($suffix) {
71 2
            $suffix = Str::snake($suffix);
72
        }
73
74 3
        $template = $this->templatesPath . 'create_geonames_table.php.stub';
75 3
        $saveTo   = database_path('migrations/' . date('Y_m_d_His', time()) . '_create_geonames_table' . ($suffix ? "_{$suffix}" : '') . '.php');
76
77 3
        $this->files->copy($template, $saveTo);
78
79 3
        $text = $this->files->get($saveTo);
80 3
        $text = str_replace('/** class_suffix **/', $suffix ? Str::ucfirst(Str::camel($suffix)) : '', $text);
81 3
        $text = str_replace('/** table_suffix **/', $suffix ? " . '_{$suffix}'" : '', $text);
82 3
        $this->files->put($saveTo, $text);
83
84 3
        return 0;
85
    }
86
}
87