Skip to content

Commit a9f38ad

Browse files
JakubKermesAleksandraKozubalEwelinaSkrzypacz
authored
#214 - restructure database for different types of vehicles (#224)
* Add new types for mobility * update service assigning * Add example importer (Beam) * csf * Fix ImporterTest.php * Fix ImporterTest.php * Add enum for services * change seeder to work with enum * csf * fix example importer(beam) and rewrite loading to use enum * fix example importer(beam) and rewrite loading to use enum * fix example importer(beam) and fix messed commit * lint * Update database/seeders/ServiceSeeder.php Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com> --------- Co-authored-by: Aleksandra Kozubal <104600942+AleksandraKozubal@users.noreply.github.com> Co-authored-by: Aleksandra Kozubal <aleksandra.kozubal@studenci.collegiumwitelona.pl> Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com>
1 parent f84ecc8 commit a9f38ad

17 files changed

+253
-119
lines changed

app/Enums/ServicesEnum.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Enums;
6+
7+
enum ServicesEnum: string
8+
{
9+
case Bike = "bike";
10+
case Cargo = "cargo";
11+
case Emoped = "emoped";
12+
case Escooter = "escooter";
13+
case Motorscooter = "motorscooter";
14+
}

app/Exceptions/ExceptionHandler.php

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function register(): void
2727
});
2828
}
2929

30+
/**
31+
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
32+
*/
3033
public function render($request, Throwable $exception)
3134
{
3235
if ($exception instanceof ValidationException) {

app/Http/Controllers/Api/CityProviderController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function index(): JsonResponse
3131
->sortByDesc(fn(City $city): int => $city->cityProviders->count()),
3232
);
3333

34-
$providers = ProviderResource::collection(Provider::all()->sortBy("name"));
34+
$providers = ProviderResource::collection(Provider::all());
3535
$countries = Country::whereHas("cities.cityProviders")
3636
->with(["cities.cityAlternativeNames", "cities.cityProviders"])
3737
->get()

app/Importers/BeamDataImporter.php

+36-45
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Importers;
66

7+
use App\Enums\ServicesEnum;
78
use GuzzleHttp\Exception\GuzzleException;
89
use Symfony\Component\DomCrawler\Crawler;
910

@@ -18,12 +19,10 @@ public function extract(): static
1819
$html = $response->getBody()->getContents();
1920
} catch (GuzzleException) {
2021
$this->createImportInfoDetails("400", self::getProviderName());
21-
2222
$this->stopExecution = true;
2323

2424
return $this;
2525
}
26-
2726
$crawler = new Crawler($html);
2827
$this->sections = $crawler->filter("div.find-beam-box");
2928

@@ -37,58 +36,50 @@ public function extract(): static
3736

3837
public function transform(): void
3938
{
40-
$url = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b60da91e7d71298637_map-vehicle-saturn.png";
39+
$escooterImageUrl = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b60da91e7d71298637_map-vehicle-saturn.png";
40+
$bikeImageUrl = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b7f06e4a42de02241b_map-vehicle-saturn-apollo.png";
4141

4242
if ($this->stopExecution) {
4343
return;
4444
}
45-
4645
$existingCityProviders = [];
4746

4847
foreach ($this->sections as $section) {
49-
foreach ($section->childNodes as $node) {
50-
if ($node->nodeName === "h4") {
51-
$countryName = $node->nodeValue;
48+
$crawler = new Crawler($section);
49+
$countryName = $crawler->filter('h4[class="find-beam-title-map"]')->text();
50+
51+
$columns = $crawler->filter('div[class="beam-col"], div[class="beam-col-main-box"]');
52+
53+
foreach ($columns as $column) {
54+
$columnCrawler = new Crawler($column);
55+
$services = [];
56+
$images = $columnCrawler->filter("img");
57+
58+
foreach ($images as $image) {
59+
if ($image->getAttribute("src") === $escooterImageUrl) {
60+
$services[] = ServicesEnum::Escooter;
61+
} else if ($image->getAttribute("src") === $bikeImageUrl) {
62+
$services[] = ServicesEnum::Bike;
63+
}
5264
}
5365

54-
if ($node->nodeName === "div") {
55-
foreach ($node->childNodes as $div) {
56-
if ($div->nodeName === "div") {
57-
foreach ($div->childNodes as $city) {
58-
if ($city->nodeName === "img" && $city->getAttribute("src") === $url) {
59-
$hasEscooters = true;
60-
} elseif ($city->nodeName === "img" && $city->getAttribute("src") !== $url) {
61-
$hasEscooters = false;
62-
}
63-
64-
if ($city->nodeName === "p" && $hasEscooters === true) {
65-
$search = ["\u{00A0}", "\u{200D}", "Prefecture"];
66-
$valueToDelete = "Selangor";
67-
$cityName = preg_replace('/[\p{Hiragana}\p{Katakana}\p{Han}]+/u', "", $city->nodeValue);
68-
$cityName = str_replace($search, "", $cityName);
69-
$cityName = preg_replace('/(?<=[^\s_\-])(?=[A-Z])/', " ", $city->nodeValue);
70-
$arrayOfCitiesNames = explode(" ", $cityName);
71-
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($value) => $value !== $valueToDelete);
72-
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strlen($record) > 1);
73-
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strpos($record, "") === false);
74-
75-
foreach ($arrayOfCitiesNames as $cityName) {
76-
if ($cityName !== "Selangor") {
77-
$cityName = trim($cityName);
78-
79-
if ($countryName === "Korea") {
80-
$countryName = "South Korea";
81-
}
82-
$provider = $this->load($cityName, $countryName);
83-
84-
if ($provider !== "") {
85-
$existingCityProviders[] = $provider;
86-
}
87-
}
88-
}
89-
}
90-
}
91-
}
66+
$cityNamesHtml = $columnCrawler->filter("p")->html();
67+
$cityNamesArray = explode("<br>", $cityNamesHtml);
68+
69+
foreach ($cityNamesArray as $cityName) {
70+
$cityName = strip_tags($cityName);
71+
$search = ["&nbsp;•", "&nbsp;"];
72+
$cityName = str_replace($search, "", $cityName);
73+
$cityName = trim($cityName);
74+
75+
if ($countryName === "Korea") {
76+
$countryName = "South Korea";
77+
}
78+
79+
$provider = $this->load($cityName, $countryName, $lat = "", $long = "", $services);
80+
81+
if ($provider) {
82+
$existingCityProviders[] = $provider;
9283
}
9384
}
9485
}

app/Importers/BerylDataImporter.php

+61-49
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Importers;
66

7+
use App\Enums\ServicesEnum;
78
use GuzzleHttp\Exception\GuzzleException;
89
use Symfony\Component\DomCrawler\Crawler;
910

@@ -27,7 +28,7 @@ public function extract(): static
2728
}
2829

2930
$crawler = new Crawler($html);
30-
$this->sections = $crawler->filter("div > section > div .inner");
31+
$this->sections = $crawler->filter('div[class="view-content col-xs-12 col-sm-12"]')->filter("div[class='views-row']");
3132

3233
if (count($this->sections) === 0) {
3334
$this->createImportInfoDetails("204", self::getProviderName());
@@ -46,58 +47,69 @@ public function transform(): void
4647
$existingCityProviders = [];
4748

4849
foreach ($this->sections as $section) {
49-
foreach ($section->childNodes as $node) {
50-
if ($node->nodeName === "div") {
51-
foreach ($node->childNodes as $city) {
52-
if ($city->nodeName === "h3") {
53-
$cityName = $city->nodeValue;
54-
}
55-
56-
$eScootersFound = false;
57-
58-
if ($city->nodeValue === "e-Scooters") {
59-
$eScootersFound = true;
60-
}
61-
62-
if ($eScootersFound) {
63-
$cityName = str_replace(["E-scooters", "and ", ","], "", $cityName);
64-
$cityName = trim($cityName);
65-
66-
if ($cityName === "Bournemouth Christchurch Poole") {
67-
$arrayOfCityNames = explode(" ", $cityName);
68-
69-
foreach ($arrayOfCityNames as $cityName) {
70-
$provider = $this->load($cityName, self::COUNTRY_NAME);
71-
72-
if ($provider !== "") {
73-
$existingCityProviders[] = $provider;
74-
}
75-
}
76-
} elseif ($cityName === "West Midlands") {
77-
$cityName = "Birmingham";
78-
}
79-
80-
if ($cityName === "Isle of Wight") {
81-
$arrayOfCityNames = ["Cowes", "East Cowes", "Newport", "Ryde", "Sandown", "Shanklin"];
82-
83-
foreach ($arrayOfCityNames as $cityName) {
84-
$provider = $this->load($cityName, self::COUNTRY_NAME);
85-
86-
if ($provider !== "") {
87-
$existingCityProviders[] = $provider;
88-
}
89-
}
90-
}
91-
$provider = $this->load($cityName, self::COUNTRY_NAME);
92-
93-
if ($provider !== "") {
94-
$existingCityProviders[] = $provider;
95-
}
96-
}
50+
$crawler = new Crawler($section);
51+
$cityName = $crawler->filter("h3")->text();
52+
$this->data[] = $cityName;
53+
54+
$servicesNodes = $crawler->filter('div[class="field--label"]');
55+
$availableServices = [];
56+
57+
foreach ($servicesNodes as $serviceNode) {
58+
$serviceCrawler = new Crawler($serviceNode);
59+
$service = $serviceCrawler->text();
60+
61+
if ($service === "e-Bikes") {
62+
$availableServices[] = ServicesEnum::Bike;
63+
}
64+
65+
if ($service === "e-Scooters") {
66+
$availableServices[] = ServicesEnum::Escooter;
67+
}
68+
69+
if ($service === "Cargo") {
70+
$availableServices[] = ServicesEnum::Cargo;
71+
}
72+
}
73+
74+
if ($cityName === "Bournemouth Christchurch Poole") {
75+
$arrayOfCityNames = explode(" ", $cityName);
76+
77+
foreach ($arrayOfCityNames as $cityName) {
78+
$provider = $this->load($cityName, self::COUNTRY_NAME, $lat = "", $long = "", $availableServices);
79+
80+
if ($provider !== "") {
81+
$existingCityProviders[] = $provider;
9782
}
9883
}
84+
} elseif ($cityName === "West Midlands") {
85+
$cityName = "Birmingham";
86+
} elseif ($cityName === "Isle of Wight") {
87+
$arrayOfCityNames = ["Cowes", "East Cowes", "Newport", "Ryde", "Sandown", "Shanklin"];
88+
89+
foreach ($arrayOfCityNames as $cityName) {
90+
$provider = $this->load($cityName, self::COUNTRY_NAME, $lat = "", $long = "", $availableServices);
91+
92+
if ($provider !== "") {
93+
$existingCityProviders[] = $provider;
94+
}
95+
}
96+
}
97+
$provider = $this->load($cityName, self::COUNTRY_NAME, $lat = "", $long = "", $availableServices);
98+
99+
if ($provider !== "") {
100+
$existingCityProviders[] = $provider;
99101
}
100102
}
103+
101104
$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
102105
}
106+
107+
public function test()
108+
{
109+
$this->extract()->transform();
110+
$sectionCount = count($this->sections);
111+
$this->data[] = ["sections" => "$sectionCount"];
112+
113+
return $this->data;
114+
}
103115
}

app/Importers/BirdDataImporter.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Importers;
66

7+
use App\Enums\ServicesEnum;
78
use App\Exceptions\MapboxGeocodingServiceException;
89
use App\Models\City;
910
use App\Models\CityAlternativeName;
@@ -57,7 +58,7 @@ public function transform(): void
5758

5859
protected function parseData(string $html): array
5960
{
60-
$pattern = '/let features = \[([\s\S]*?)\];/';
61+
$pattern = '/let features = \[([\s\S]*?)];/';
6162

6263
if (preg_match($pattern, $html, $matches)) {
6364
$fetchedData = $matches[1];
@@ -75,15 +76,15 @@ protected function parseData(string $html): array
7576
return array_map("trim", $coordinates);
7677
}
7778

78-
protected function load(string $cityName, string $countryName, string $lat = "", string $long = ""): string
79+
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = [ServicesEnum::Escooter]): string
7980
{
8081
$city = City::query()->where("name", $cityName)->first();
8182
$alternativeCityName = CityAlternativeName::query()->where("name", $cityName)->first();
8283

8384
if ($city || $alternativeCityName) {
8485
$cityId = $city ? $city->id : $alternativeCityName->city_id;
8586

86-
$this->createProvider($cityId, self::getProviderName());
87+
$this->createProvider($cityId, self::getProviderName(), $services);
8788

8889
return strval($cityId);
8990
}
@@ -97,7 +98,7 @@ protected function load(string $cityName, string $countryName, string $lat = "",
9798
"country_id" => $country->id,
9899
]);
99100

100-
$this->createProvider($city->id, self::getProviderName());
101+
$this->createProvider($city->id, self::getProviderName(), $services);
101102

102103
return strval($city->id);
103104
}

0 commit comments

Comments
 (0)