src/Service/DataService.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Contracts\HttpClient\HttpClientInterface;
  4. class DataService
  5. {
  6.     public function __construct(HttpClientInterface $client)
  7.     {
  8.         $this->client $client;
  9.     }
  10.     public function fetchPrestations(): array
  11.     {
  12.         $response $this->client->request('GET'"{$_ENV['BASE_API']}prestations");
  13.         return $response->toArray()["prestations"];
  14.     }
  15.     public function fetchSpecialites(): array
  16.     {
  17.         $response $this->client->request('GET'"{$_ENV['BASE_API']}specialites");
  18.         return $response->toArray()["specialites"];
  19.     }
  20.     public function fetchDiplomes(): array
  21.     {
  22.         $response $this->client->request('GET'"{$_ENV['BASE_API']}diplomes");
  23.         return $response->toArray()["diplomes"];
  24.     }
  25.    
  26.     public function fetchMutuelles(): array
  27.     {
  28.         $response $this->client->request(
  29.             'GET',
  30.             "{$_ENV['BASE_API']}mutuelles"
  31.         );
  32.         return $response->toArray()["mutuelles"];
  33.     }
  34.     public function fetchStations(): array
  35.     {
  36.          $response $this->client->request(
  37.             'GET',
  38.             "{$_ENV['BASE_API']}stations/city"
  39.         );
  40.         return $response->toArray()["stations"];
  41.     }
  42.     public function fetchCity(): array
  43.     {
  44.         $response $this->client->request(
  45.             'GET',
  46.             "{$_ENV['BASE_API']}villes"
  47.         );
  48.         $data $response->toArray();
  49.         if (isset($data['villes'])) {
  50.             $collator = new \Collator('fr_FR');
  51.             
  52.             usort($data['villes'], function ($a$b) use ($collator) {
  53.                 preg_match('/^([^\d]+)\s*(\d*)$/u'$a['nom'], $matchesA);
  54.                 preg_match('/^([^\d]+)\s*(\d*)$/u'$b['nom'], $matchesB);
  55.                 $nameCmp $collator->compare(trim($matchesA[1]), trim($matchesB[1]));
  56.                 
  57.                 if ($nameCmp !== 0) {
  58.                     return $nameCmp;
  59.                 }
  60.                 $numA = isset($matchesA[2]) && $matchesA[2] !== '' ? (int)$matchesA[2] : 0;
  61.                 $numB = isset($matchesB[2]) && $matchesB[2] !== '' ? (int)$matchesB[2] : 0;
  62.                 return $numA <=> $numB;
  63.             });
  64.             return $data['villes'];
  65.         }
  66.         return [];
  67.     }
  68.     public function fetchData(): array
  69.     {
  70.         $prestations $this->fetchPrestations();
  71.         $specialites $this->fetchSpecialites();
  72.         $diplomes $this->fetchDiplomes();
  73.         $mutuelles $this->fetchMutuelles();
  74.         $stations $this->fetchStations();
  75.         return [
  76.             "prestations" => $prestations,
  77.             "specialites" => $specialites,
  78.             "diplomes" => $diplomes,
  79.             "mutuelles" => $mutuelles,
  80.             "stations" => $stations,
  81.         ];
  82.     }
  83. }