Test Failed
Push — master ( 0c6ae7...5a133e )
by Yaroslav
02:43
created

MakeMigrationCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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 6
    public function __construct(Filesystem $files)
50
    {
51 6
        parent::__construct();
52
53 6
        $this->files = $files;
54 6
    }
55
56 2
    public function handle()
57
    {
58 2
        $type       = $this->argument('type');
59 2
        $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 2
        if (!method_exists($this, $methodName)) {
61
            throw new \Exception("Migration type [{$type}] not supported");
62
        }
63
64 2
        return $this->{$methodName}();
65
    }
66
67 2
    protected function makeGeonamesMigration(): int
68
    {
69 2
        $suffix = $this->option('suffix');
70 2
        if ($suffix) {
71 1
            $suffix = Str::snake($suffix);
72
        }
73
74 2
        $template = $this->templatesPath . 'create_geonames_table.php.stub';
75 2
        $saveTo   = database_path('migrations/' . date('Y_m_d_His', time()) . '_create_geonames_table' . ($suffix ? "_{$suffix}" : '') . '.php');
76
77 2
        $this->files->copy($template, $saveTo);
78
79 2
        $text = $this->files->get($saveTo);
80 2
        $text = str_replace('/** class_suffix **/', $suffix ? Str::ucfirst(Str::camel($suffix)) : '', $text);
81 2
        $text = str_replace('/** table_suffix **/', $suffix ? " . '_{$suffix}'" : '', $text);
82 2
        $this->files->put($saveTo, $text);
83
84 2
        return 0;
85
    }
86
}
87