Passed
Push — master ( 2681c1...326c0d )
by Denis
10:20
created

ParseCityAddressRu   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A download() 0 5 1
A __construct() 0 5 1
B handle() 0 69 5
1
<?php
2
3
namespace Ngtfkx\Laradeck\AddressGenerator\Commands;
4
5
use Illuminate\Console\Command;
1 ignored issue
show
Bug introduced by
The type Illuminate\Console\Command 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...
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Storage;
8
use Symfony\Component\DomCrawler\Crawler;
1 ignored issue
show
Bug introduced by
The type Symfony\Component\DomCrawler\Crawler 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...
9
10
class ParseCityAddressRu extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'address:city-address-ru {city} {url} {--limit=0}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Parse streets and building numbers from http://www.city-address.ru';
25
26
    /**
27
     * @var Collection
28
     */
29
    protected $streets;
30
31
    public function __construct()
32
    {
33
        $this->streets = new Collection();
34
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $cityId = $this->argument('city');
46
47
        $url = $this->argument('url');
48
49
        $limit = (int)$this->option('limit');
50
51
        /**
52
         * Получаем список всех страниц с улицами
53
         */
54
        $firstPage = $this->download($url);
55
56
        $allPages = (int)(new Crawler($firstPage))->filter('.uk-pagination > li')->last()->text();
57
58
        $k = 0;
59
        for ($i = 1; $i <= $allPages; $i++) {
60
            /**
61
             * Получам список улиц на текущей странице
62
             */
63
            $page = $this->download($url . '/page-' . $i . '/');
64
65
            $this->line('Downloading streets and building numbers from page #' . $i . ' from ' . $allPages);
66
67
            $streets = (new Crawler($page))->filter('.c4 > a');
68
69
            $streets->each(function ($node) {
70
                /**
71
                 * Получаем список домов
72
                 */
73
                $url = 'http://www.city-address.ru' . $node->attr('href');
74
75
                $page = $this->download($url);
76
77
                /**
78
                 * Обрабатываем список домов
79
                 */
80
                $numbers = collect((new Crawler($page))->filter('.c6 > div > a')->extract('_text'))
81
                    ->reject(function ($item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

81
                    ->reject(function ($item, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
                        return strpos($item, 'дом ') !== 0;
83
                    })->map(function ($item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

83
                    })->map(function ($item, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
                        return str_replace('дом № ', '', $item);
85
                    })->toArray();
86
87
                $this->streets->put($node->text(), $numbers);
88
            });
89
90
            if (++$k == $limit) {
91
                $this->warn('Interrupted by the limit');
92
                break;
93
            }
94
        }
95
96
        /**
97
         * Генерируем контент для файла с данными
98
         */
99
        $output = '<?php' . PHP_EOL . PHP_EOL;
100
        $output .= 'return [' . PHP_EOL;
101
        foreach ($this->streets as $street => $numbers) {
102
            $glueNumbers = implode('", "', $numbers);
103
            if (!empty($glueNumbers)) {
104
                $output .= ' "' . trim($street, ";") . '" => ["' . $glueNumbers . '"],' . PHP_EOL;
105
            }
106
        }
107
        $output .= '];' . PHP_EOL;
108
109
        Storage::put($cityId . '.php', $output);
110
111
        $this->info('Data was saved');
112
    }
113
114
    protected function download(string $url)
115
    {
116
        $content = file_get_contents($url);
117
118
        return $content;
119
    }
120
}
121