src/Infra/Services/ApiService.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Infra\Services;
  3. use App\Domain\Entity\Apimo;
  4. use App\Domain\Repository\ApimoRepository;
  5. use App\Domain\Repository\PropertyRepository;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. class ApiService
  9. {
  10.     private $httpClient;
  11.     private $apimoRepository;
  12.     private $propertyRepository;
  13.     
  14.     public function __construct(
  15.          HttpClientInterface $httpClient,
  16.          ApimoRepository $apimoRepository,
  17.          PropertyRepository $propertyRepository
  18.     )
  19.     {
  20.         $this->httpClient $httpClient;
  21.         $this->apimoRepository $apimoRepository;
  22.         $this->propertyRepository $propertyRepository;
  23.     }
  24.     public function getAllProperties()
  25.     {
  26.         $apimos $this->apimoRepository->findAll();
  27.         $properties = [];
  28.         $db_response = [];
  29.         if($apimos){
  30.             foreach($apimos as $apimo){
  31.                 $properties[] = $this->setLocalImages($apimo);
  32.             }
  33.             $db_response = ["total_items" => count($apimos), "properties" => $properties];
  34.             return $db_response;
  35.         }
  36.         else{
  37.             $response $this->httpClient->request(
  38.                 'GET',
  39.                 'https://api.apimo.pro/agencies/20169/properties', [
  40.                     'auth_basic' => [
  41.                         'user' => '2450:0e7a10ed20e96147ecb47c083644ffa023b4155e',
  42.                     ],
  43.                     'timeout' => 3
  44.                 ],
  45.             );
  46.             return $response->getStatusCode() !== 200 ? ['properties' => []] : $response->toArray();
  47.         }
  48.     }
  49.     public function getPropertyById(string $id)
  50.     {
  51.         $apimo = ($this->apimoRepository->findOneBy(["apimo_id" => $id]));
  52.         if($apimo) return $this->setLocalImages($apimo);
  53.         try{
  54.             $response $this->httpClient->request(
  55.                 'GET',
  56.                 'https://api.apimo.pro/agencies/20169/properties/'.$id, [
  57.                     'auth_basic' => [
  58.                         'user' => '2450:0e7a10ed20e96147ecb47c083644ffa023b4155e',
  59.                     ],
  60.                     'timeout' => 3
  61.                 ]
  62.             );
  63.             return $response->getStatusCode() !== 200 ? [
  64.                 'bedrooms' => null,
  65.                 'type' => null,
  66.                 'reference' => null,
  67.                 'pictures' => null,
  68.                 'area' => ['value' => null],
  69.                 'areas' => null,
  70.                 'construction' => ['construction_year' => null],
  71.                 'available_at' => null,
  72.                 'city' => ["name" => null],
  73.                 'regulations' => null,
  74.                 'price' => ["value" => null],
  75.                 'comments' => [["comment" => "erreur de connexion avec l'API, merci de réessayer plus tard"]],
  76.                 'category' => null,
  77.             ] : $response->toArray();
  78.         }
  79.         catch(\Exception $e){
  80.             return [
  81.                 'bedrooms' => null,
  82.                 'type' => null,
  83.                 'reference' => null,
  84.                 'pictures' => null,
  85.                 'area' => ['value' => null],
  86.                 'areas' => null,
  87.                 'construction' => ['construction_year' => null],
  88.                 'available_at' => null,
  89.                 'city' => ["name" => null],
  90.                 'regulations' => null,
  91.                 'price' => ["value" => null],
  92.                 'comments' => [["comment" => "erreur de connexion avec l'API, merci de réessayer plus tard"]],
  93.                 'category' => null,
  94.             ];
  95.         }
  96.     }
  97.     /**
  98.      * Return Apimo json content in array with replacement of images url
  99.      *
  100.      * @param Apimo $apimo
  101.      * @return array
  102.      */
  103.     private function setLocalImages(Apimo $apimo): array{
  104.         $property $this->propertyRepository->findOneBy(["apimo" => $apimo]);
  105.         $datas json_decode($apimo->getJson(), true);
  106.         $i 0;
  107.         foreach($datas["pictures"] as $picture){
  108.             if($property->getImages()[$i]){
  109.                 $datas["pictures"][$i]["url"] = $property->getImages()[$i]->getUrl();
  110.             }
  111.             $i++;
  112.         }
  113.         return $datas;
  114.     }
  115. }