AddressSeeder::run()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 70

Duplication

Lines 12
Ratio 17.14 %

Importance

Changes 0
Metric Value
dl 12
loc 70
rs 8.0323
c 0
b 0
f 0
cc 6
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Illuminate\Database\Seeder;
4
use Yajra\Address\Entities\City;
5
use Yajra\Address\Entities\Region;
6
use Yajra\Address\Entities\Barangay;
7
use Yajra\Address\Entities\Province;
8
use Rap2hpoutre\FastExcel\FastExcel;
9
10
class AddressSeeder extends Seeder
11
{
12
    /**
13
     * Run the database seeds.
14
     *
15
     * @return void
16
     * @throws \Box\Spout\Common\Exception\IOException
17
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
18
     * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
19
     */
20
    public function run()
21
    {
22
        $publication = config('address.publication.path', __DIR__ . '/publication/PSGC_Publication_Dec2019.xlsx');
23
        $sheet       = config('address.publication.sheet', 4);
24
25
        $regions   = [];
26
        $provinces = [];
27
        $cities    = [];
28
        $barangays = [];
29
30
        $this->command->info(sprintf('Parsing PSA official PSGC publication (%s).', $publication));
31
        (new FastExcel)
32
            ->sheet($sheet)
33
            ->import($publication, function ($line) use (&$regions, &$provinces, &$cities, &$barangays) {
34
                $attributes              = [];
35
                $attributes['code']      = $line['Code'];
36
                $attributes['name']      = $line['Name'];
37
                $attributes['region_id'] = substr($attributes['code'], 0, 2);
38
39
                switch ($line['Geographic Level']) {
40
                    case 'Reg':
41
                        $regions[] = $attributes;
42
                        break;
43
44
                    case 'Dist':
45
                    case 'Prov':
46
                    case '':
47
                        $attributes['province_id'] = substr($attributes['code'], 0, 4);
48
49
                        $provinces[] = $attributes;
50
                        break;
51
52 View Code Duplication
                    case 'Bgy':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                        $attributes['province_id'] = substr($attributes['code'], 0, 4);
54
                        $attributes['city_id']     = substr($attributes['code'], 0, 6);
55
56
                        $barangays[] = $attributes;
57
                        break;
58
59 View Code Duplication
                    default: // City, SubMun, Mun
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                        $attributes['province_id'] = substr($attributes['code'], 0, 4);
61
                        $attributes['city_id']     = substr($attributes['code'], 0, 6);
62
63
                        $cities[] = $attributes;
64
                        break;
65
                }
66
            });
67
68
        $this->command->info(sprintf('Seeding %s regions.', count($regions)));
69
        $region = config('address.models.region', Region::class);
70
        $region::query()->insert($regions);
71
72
        $this->command->info(sprintf('Seeding %s provinces.', count($provinces)));
73
        $province = config('address.models.province', Province::class);
74
        collect($provinces)->chunk(100)->each(function ($chunk) use ($province) {
75
            $province::query()->insert($chunk->toArray());
76
        });
77
78
        $city = config('address.models.city', City::class);
79
        $this->command->info(sprintf('Seeding %s cities & municipalities.', count($cities)));
80
        collect($cities)->chunk(100)->each(function ($chunk) use ($city) {
81
            $city::query()->insert($chunk->toArray());
82
        });
83
84
        $this->command->info(sprintf('Seeding %s barangays.', count($barangays)));
85
        $barangay = config('address.models.barangay', Barangay::class);
86
        collect($barangays)->chunk(100)->each(function ($chunk) use ($barangay) {
87
            $barangay::query()->insert($chunk->toArray());
88
        });
89
    }
90
}
91