ImportTownsToDatabase::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 17
ccs 10
cts 12
cp 0.8333
crap 3.0416
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace UKTowns\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Str;
8
9
class ImportTownsToDatabase extends Command
10
{
11
    protected $signature = 'uk-towns:import
12
    ';
13
14
    protected $description = 'Import towns table to database.';
15
16 2
    public function handle(): int
17
    {
18 2
        $sqlScript = file_get_contents(__DIR__.'/../../storage/uk-towns.sql');
19 2
        if (!$sqlScript) {
20
            $this->error('Script not found.');
21
22
            return 1;
23
        }
24 2
        $sqlScript  = Str::replace('geo_towns', config('uk-towns.tables.towns'), $sqlScript);
25 2
        $sqlScripts = explode('-- statement', $sqlScript);
26 2
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
27 2
        foreach ($sqlScripts as $sqlScript) {
28 2
            DB::statement(trim($sqlScript));
29
        }
30 2
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
31
32 2
        return 0;
33
    }
34
}
35