Passed
Push — master ( f90e65...c97d58 )
by Yaroslav
04:32
created

LoadDataToDBCommand::importGeonamesData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 38
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
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 LoadDataToDBCommand extends Command
10
{
11
    use HasTablesClassesMap;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'geonames:import:file-to-db
19
        {type : Migration type. }
20
        {file? : Csv file path. }
21
        {--suffix= : Suffix used for specify country if need. }
22
        {--truncate : Truncate table before import. }
23
    ';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Upload data from csv file to DB.';
31
32
    /**
33
     * The filesystem instance.
34
     *
35
     * @var Filesystem
36
     */
37
    protected Filesystem $files;
38
39
    /**
40
     * Create a new command instance.
41
     *
42
     * @param Filesystem $files
43
     *
44
     * @return void
45
     */
46 18
    public function __construct(Filesystem $files)
47
    {
48 18
        parent::__construct();
49
50 18
        $this->files = $files;
51 18
    }
52
53 11
    public function handle()
54
    {
55 11
        $tableClassName = $this->getTableClassNameByType($this->argument('type'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('type') can also be of type array and null; however, parameter $type of LaraGeoData\Console\Comm...tTableClassNameByType() 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

55
        $tableClassName = $this->getTableClassNameByType(/** @scrutinizer ignore-type */ $this->argument('type'));
Loading history...
56
57 10
        (new $tableClassName($this->files))->loadData($this->argument('file'), $this->getSuffix(), (bool) $this->option('truncate'));
58
59 4
        return 0;
60
    }
61
62
63
    /**
64
     * Get formatted suffix.
65
     *
66
     * @return string|null
67
     */
68 10
    protected function getSuffix(): ?string
69
    {
70 10
        $suffix = $this->option('suffix');
71 10
        if ($suffix) {
72 7
            $suffix = Str::snake($suffix);
73
        }
74
75 10
        return $suffix;
76
    }
77
}
78