-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataImporter.php
163 lines (131 loc) · 5.14 KB
/
DataImporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace App\Importers;
use App\Models\City;
use App\Models\CityAlternativeName;
use App\Models\CityProvider;
use App\Models\CityWithoutAssignedCountry;
use App\Models\Country;
use App\Models\ImportInfoDetail;
use App\Models\Service;
use App\Services\MapboxGeocodingService;
use GuzzleHttp\Client;
use Stichoza\GoogleTranslate\GoogleTranslate;
abstract class DataImporter
{
protected bool $stopExecution = false;
protected int $importInfoId;
protected GoogleTranslate $translate;
public function __construct(
protected Client $client,
protected MapboxGeocodingService $mapboxService,
) {
$this->translate = new GoogleTranslate();
}
public function setImportInfo(int $importInfoId): static
{
$this->importInfoId = $importInfoId;
return $this;
}
abstract public function extract(): static;
abstract public function transform(): void;
public function hasStoppedExecution(): bool
{
return $this->stopExecution;
}
public static function getProviderName(): string
{
$parted = explode("\\", static::class);
$parted = str_replace("DataImporter", "", $parted[count($parted) - 1]);
$classNameParts = explode("@", $parted);
return $classNameParts[0];
}
public function translate(string $word, string $language): string
{
return $this->translate->setTarget($language)->translate($word);
}
protected function countryNotFound(string $cityName, string $countryName): void
{
$cityAttributes = [
"city_name" => $cityName,
"country_name" => $countryName,
];
$city = CityWithoutAssignedCountry::query()->withTrashed()->updateOrCreate(
$cityAttributes,
$cityAttributes,
);
if ($city->wasRecentlyCreated) {
$this->createImportInfoDetails("420", self::getProviderName());
}
}
protected function createProvider(int $cityId, string $providerName, array $services): void
{
foreach ($services as $service) {
$service = Service::query()->where("type", $service)->first();
CityProvider::query()->updateOrCreate([
"provider_name" => $providerName,
"city_id" => $cityId,
"created_by" => "scraper",
"service_id" => $service->id,
]);
}
}
protected function deleteMissingProviders(string $providerName, array $existingCityProviders): void
{
$cityProvidersToDelete = CityProvider::query()
->where("provider_name", $providerName)
->whereNotIn("city_id", $existingCityProviders)
->whereNot("created_by", "admin")
->get();
$cityProvidersToDelete->each(fn($cityProvider) => $cityProvider->delete());
}
protected function createImportInfoDetails(string $code, string $providerName): void
{
ImportInfoDetail::query()->updateOrCreate(
[
"provider_name" => $providerName,
"import_info_id" => $this->importInfoId,
"code" => $code,
],
[
"provider_name" => $providerName,
"import_info_id" => $this->importInfoId,
"code" => $code,
],
);
}
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = ["escooter"]): string
{
$country = Country::query()->where("name", $countryName)->orWhere("alternative_name", $countryName)->first();
if ($country) {
$city = City::query()->where("name", $cityName)->where("country_id", $country->id)->first();
$alternativeCityName = CityAlternativeName::query()->where("name", $cityName)->first();
if ($city) {
$cityId = $city->id;
$this->createProvider($cityId, self::getProviderName(), $services);
return strval($cityId);
} elseif ($alternativeCityName) {
$cityId = $alternativeCityName->city_id;
$city = City::query()->where("id", $cityId)->first();
if ($city->country_id === $country->id) {
$this->createProvider($cityId, self::getProviderName(), $services);
}
}
$coordinates = $this->mapboxService->getCoordinatesFromApi($cityName, $countryName);
$countCoordinates = count($coordinates);
if ($countCoordinates === 0 || $coordinates[0] === null || $coordinates[1] === null) {
$this->createImportInfoDetails("419", self::getProviderName());
}
$city = City::query()->create([
"name" => $cityName,
"latitude" => $coordinates[0] ?? null,
"longitude" => $coordinates[1] ?? null,
"country_id" => $country->id,
]);
$this->createProvider($city->id, self::getProviderName(), $services);
return strval($city->id);
}
$this->countryNotFound($cityName, $countryName);
return "";
}
}