src/Service/APIDigitalCouponHTTP.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Document\Emission;
  4. use App\Exception\DigitalCouponException;
  5. use App\Exception\Manager\DigitalCouponExceptionManager as DCM;
  6. use Symfony\Component\HttpClient\CachingHttpClient;
  7. use Symfony\Component\HttpClient\HttpClient;
  8. use Symfony\Component\HttpKernel\HttpCache\Store;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. class APIDigitalCouponHTTP
  11. {
  12.     const  PATH_COUPONS_MEDIA 'coupons/listCouponsByMediaGET';
  13.     const  PATH_EMISSION 'coupons/getCoupons';
  14.     const  PATH_EMISSION_WITH_ID 'coupons/getCouponsWithEmissionId';
  15.     /**
  16.      * @var Client
  17.      */
  18.     private $client;
  19.     /**
  20.      * @var string
  21.      */
  22.     private $url;
  23.     /**
  24.      * @var string
  25.      */
  26.     private $codeDemandeur;
  27.     /**
  28.      * @var ApiSerializer
  29.      */
  30.     private $apiSerializer;
  31.     /**
  32.      * @var DCM
  33.      */
  34.     private $exceptionManager;
  35.     public function __construct($urlApiSerializer $apiSerializerDCM $exceptionManagerParameterBagInterface $params)
  36.     {
  37.         $this->url $url "/";
  38.         $store = new Store($params->get("kernel.cache_dir") . "/HttpClientSore/");
  39.         $this->client HttpClient::create();
  40.         $this->client = new CachingHttpClient($this->client$store);
  41.         $this->apiSerializer $apiSerializer;
  42.         $this->exceptionManager $exceptionManager;
  43.     }
  44.     public function callApiCoupon($path$data$method "GET")
  45.     {
  46.         $response $this->client->request(
  47.             $method,
  48.             $this->url $path,
  49.             [
  50.                 'body' => $data,
  51.                 'headers' => [
  52.                     'Content-Type' => 'application/xml',
  53.                 ],
  54.                 'timeout' => 5
  55.             ]
  56.         );
  57.         return $response;
  58.     }
  59.     public function getCoupponByMedia()
  60.     {
  61.         if (!empty($this->getCodeDemandeur())) {
  62.             $response $this->client->request(
  63.                 'GET',
  64.                 $this->url self::PATH_COUPONS_MEDIA "/" $this->getCodeDemandeur(),
  65.                 [
  66.                     'timeout' => 5
  67.                 ]
  68.             );
  69.             if (200 == $response->getStatusCode()) {
  70.                 $jsonResponse $this->apiSerializer->serializeApiData($response->getContent());
  71.                 return $jsonResponse;
  72.             }
  73.             throw new DigitalCouponException($response->getContent());
  74.         } else {
  75.             throw new \Exception('Code demandeur is missing');
  76.         }
  77.     }
  78.     public function postEmission(Emission $emission)
  79.     {
  80.         $xmlOperationCode $this->createOperationCode($emission);
  81.         $xml '<coupons>
  82.                     <demandeur>'.$emission->getEmmeteur().'</demandeur>
  83.                     <format>html</format>
  84.                     <barcodeValue>'.$emission->getConsumer()->getShortId().'</barcodeValue>
  85.                     <customerInfo>'.$emission->getConsumer()->getCustomerInfo().'</customerInfo>
  86.                     <email>'.$emission->getConsumer()->getEmail().'</email>
  87.                     '.$xmlOperationCode.'
  88.                 </coupons>';
  89.         $response $this->callApiCoupon(self::PATH_EMISSION_WITH_ID$xml"POST");
  90.         $digitalcouponResponse json_decode($response->getContent());
  91.         $emission->setPrintUrl($digitalcouponResponse->url ?? null);
  92.         $emission->setDigitacouponEmissionId($digitalcouponResponse->ids ?? null);
  93.         return $emission;
  94.     }
  95.     public function getCodeDemandeur()
  96.     {
  97.         return $this->codeDemandeur;
  98.     }
  99.     public function setCodeDemandeur($codeDemandeur)
  100.     {
  101.         $this->codeDemandeur $codeDemandeur;
  102.     }
  103.     private function createOperationCode(Emission $emission)
  104.     {
  105.         $xml '';
  106.         foreach ($emission->getCoupons() as $coupon) {
  107.             $xml .= '<operationCode>'.$coupon->getOperationCode().'</operationCode>';
  108.         }
  109.         return $xml;
  110.     }
  111. }