Issues (52)

src/Commands/SyncCoordinateCommand.php (5 issues)

Labels
1
<?php
2
3
namespace Turahe\Master\Commands;
4
5
use GuzzleHttp\Exception\ServerException;
6
use Illuminate\Console\Command;
7
use Turahe\Master\Models\City;
8
use Turahe\Master\Models\District;
9
use Turahe\Master\Models\Province;
10
use Turahe\Master\Models\Village;
11
12
class SyncCoordinateCommand extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'turahe:master:sync-coordinate';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Synchronize latitude longitude data in database directly using Google\'s geocoding service';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $count = Province::count();
46
        $this->info("Processing {$count} Province");
47
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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

47
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
48
        $bar->start();
49
        Province::cursor()->each(function ($item) use ($bar) {
50
            $meta = $item->meta;
51
            $geocoding = \Geocoder::getCoordinatesForAddress($item->address);
0 ignored issues
show
The type Geocoder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
            $meta['lat'] = $geocoding['lat'] ?? null;
53
            $meta['long'] = $geocoding['lng'] ?? null;
54
            $item->meta = $meta;
55
            $item->save();
56
            $bar->advance();
57
        });
58
59
        $this->line('');
60
61
        $count = City::count();
62
        $this->info("Processing {$count} City");
63
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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

63
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
64
        $bar->start();
65
        City::with('provinsi')->cursor()->each(function ($item) use ($bar) {
66
            $meta = $item->meta;
67
            $geocoding = \Geocoder::getCoordinatesForAddress($item->address);
68
            $meta['lat'] = $geocoding['lat'] ?? null;
69
            $meta['long'] = $geocoding['lng'] ?? null;
70
            $item->meta = $meta;
71
            $item->save();
72
            $bar->advance();
73
        });
74
75
        $this->line('');
76
77
        $count = District::count();
78
        $this->info("Processing {$count} District");
79
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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

79
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
80
        $bar->start();
81
        District::with('kabupaten.provinsi')->cursor()->each(function ($item) use ($bar) {
82
            $meta = $item->meta;
83
            $geocoding = \Geocoder::getCoordinatesForAddress($item->address);
84
            $meta['lat'] = $geocoding['lat'] ?? null;
85
            $meta['long'] = $geocoding['lng'] ?? null;
86
            $item->meta = $meta;
87
            $item->save();
88
            $bar->advance();
89
        });
90
91
        $this->line('');
92
93
        $count = Village::whereNull('meta')->count();
94
        $this->info("Processing {$count} Village");
95
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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

95
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
96
        $bar->start();
97
        Village::whereNull('meta')->cursor()->each(function ($item) use ($bar) {
98
            $meta = $item->meta;
99
            if (!$meta) {
100
                try {
101
                    $geocoding = \Geocoder::getCoordinatesForAddress($item->address);
102
                    $meta['lat'] = $geocoding['lat'] ?? null;
103
                    $meta['long'] = $geocoding['lng'] ?? null;
104
                    $item->meta = $meta;
105
                    $item->save();
106
                } catch (ServerException $e) {
107
                    $this->error($e->getMessage());
108
                    sleep(1);
109
                }
110
            }
111
            $bar->advance();
112
        });
113
    }
114
}
115