|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Importers; |
| 6 | + |
| 7 | +use App\Enums\ServicesEnum; |
| 8 | +use GuzzleHttp\Exception\GuzzleException; |
| 9 | +use Symfony\Component\DomCrawler\Crawler; |
| 10 | + |
| 11 | +class GoSharingDataImporter extends DataImporter |
| 12 | +{ |
| 13 | + protected Crawler $sections; |
| 14 | + |
| 15 | + public function extract(): static |
| 16 | + { |
| 17 | + try { |
| 18 | + $response = $this->client->get("https://nl.go-sharing.com/en/locations/"); |
| 19 | + $html = $response->getBody()->getContents(); |
| 20 | + } catch (GuzzleException) { |
| 21 | + $this->createImportInfoDetails("400", self::getProviderName()); |
| 22 | + |
| 23 | + $this->stopExecution = true; |
| 24 | + |
| 25 | + return $this; |
| 26 | + } |
| 27 | + |
| 28 | + $crawler = new Crawler($html); |
| 29 | + $this->sections = $crawler->filter('section[id="show-cities"]'); |
| 30 | + |
| 31 | + if (count($this->sections) === 0) { |
| 32 | + $this->createImportInfoDetails("204", self::getProviderName()); |
| 33 | + $this->stopExecution = true; |
| 34 | + } |
| 35 | + |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + public function transform(): void |
| 40 | + { |
| 41 | + if ($this->stopExecution) { |
| 42 | + return; |
| 43 | + } |
| 44 | + $existingCityProviders = []; |
| 45 | + $countryNames = []; |
| 46 | + |
| 47 | + foreach ($this->sections as $section) { |
| 48 | + $sectionCrawler = new Crawler($section); |
| 49 | + $countries = $sectionCrawler->filter("h3"); |
| 50 | + |
| 51 | + foreach ($countries as $country) { |
| 52 | + $countryNames[] = $country->textContent; |
| 53 | + } |
| 54 | + |
| 55 | + $countryCitiesRows = $sectionCrawler->filter('div[class="row location-row"]'); |
| 56 | + $countryCitiesArray = []; |
| 57 | + |
| 58 | + foreach ($countryCitiesRows as $countryCities) { |
| 59 | + $locationsCrawler = new Crawler($countryCities); |
| 60 | + $locations = $locationsCrawler->filter('div[class="location"]'); |
| 61 | + |
| 62 | + $citiesArray = []; |
| 63 | + |
| 64 | + foreach ($locations as $location) { |
| 65 | + $cityCrawler = new Crawler($location); |
| 66 | + $cityName = $cityCrawler->text(); |
| 67 | + |
| 68 | + $activeServicesArray = []; |
| 69 | + $activeServices = $cityCrawler->filter('div[class="location__icons__icon active"]'); |
| 70 | + |
| 71 | + foreach ($activeServices as $activeService) { |
| 72 | + $serviceCrawler = new Crawler($activeService); |
| 73 | + $serviceType = $serviceCrawler->filter("img")->attr("alt"); |
| 74 | + |
| 75 | + if ($serviceType === "E-Scooter") { |
| 76 | + $activeServicesArray[] = ServicesEnum::Escooter; |
| 77 | + } |
| 78 | + |
| 79 | + if ($serviceType === "E-Bike") { |
| 80 | + $activeServicesArray[] = ServicesEnum::Bike; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $citiesArray[] = [ |
| 85 | + "cityName" => $cityName, |
| 86 | + "services" => $activeServicesArray, |
| 87 | + ]; |
| 88 | + } |
| 89 | + $countryCitiesArray[] = $citiesArray; |
| 90 | + } |
| 91 | + |
| 92 | + $countryCount = count($countryNames); |
| 93 | + |
| 94 | + for ($i = 0; $i < $countryCount; $i++) { |
| 95 | + $countryName = $countryNames[$i]; |
| 96 | + $cities = $countryCitiesArray[$i]; |
| 97 | + |
| 98 | + foreach ($cities as $city) { |
| 99 | + $cityName = $city["cityName"]; |
| 100 | + $services = $city["services"]; |
| 101 | + |
| 102 | + $provider = $this->load($cityName, $countryName, $lat = "", $long = "", $services); |
| 103 | + if ($provider !== "") { |
| 104 | + $existingCityProviders[] = $provider; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + $this->deleteMissingProviders(self::getProviderName(), $existingCityProviders); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments