<?php
namespace App\Service;
use App\Document\Emission;
use App\Exception\DigitalCouponException;
use App\Exception\Manager\DigitalCouponExceptionManager as DCM;
use Symfony\Component\HttpClient\CachingHttpClient;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class APIDigitalCouponHTTP
{
const PATH_COUPONS_MEDIA = 'coupons/listCouponsByMediaGET';
const PATH_EMISSION = 'coupons/getCoupons';
const PATH_EMISSION_WITH_ID = 'coupons/getCouponsWithEmissionId';
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $codeDemandeur;
/**
* @var ApiSerializer
*/
private $apiSerializer;
/**
* @var DCM
*/
private $exceptionManager;
public function __construct($url, ApiSerializer $apiSerializer, DCM $exceptionManager, ParameterBagInterface $params)
{
$this->url = $url . "/";
$store = new Store($params->get("kernel.cache_dir") . "/HttpClientSore/");
$this->client = HttpClient::create();
$this->client = new CachingHttpClient($this->client, $store);
$this->apiSerializer = $apiSerializer;
$this->exceptionManager = $exceptionManager;
}
public function callApiCoupon($path, $data, $method = "GET")
{
$response = $this->client->request(
$method,
$this->url . $path,
[
'body' => $data,
'headers' => [
'Content-Type' => 'application/xml',
],
'timeout' => 5
]
);
return $response;
}
public function getCoupponByMedia()
{
if (!empty($this->getCodeDemandeur())) {
$response = $this->client->request(
'GET',
$this->url . self::PATH_COUPONS_MEDIA . "/" . $this->getCodeDemandeur(),
[
'timeout' => 5
]
);
if (200 == $response->getStatusCode()) {
$jsonResponse = $this->apiSerializer->serializeApiData($response->getContent());
return $jsonResponse;
}
throw new DigitalCouponException($response->getContent());
} else {
throw new \Exception('Code demandeur is missing');
}
}
public function postEmission(Emission $emission)
{
$xmlOperationCode = $this->createOperationCode($emission);
$xml = '<coupons>
<demandeur>'.$emission->getEmmeteur().'</demandeur>
<format>html</format>
<barcodeValue>'.$emission->getConsumer()->getShortId().'</barcodeValue>
<customerInfo>'.$emission->getConsumer()->getCustomerInfo().'</customerInfo>
<email>'.$emission->getConsumer()->getEmail().'</email>
'.$xmlOperationCode.'
</coupons>';
$response = $this->callApiCoupon(self::PATH_EMISSION_WITH_ID, $xml, "POST");
$digitalcouponResponse = json_decode($response->getContent());
$emission->setPrintUrl($digitalcouponResponse->url ?? null);
$emission->setDigitacouponEmissionId($digitalcouponResponse->ids ?? null);
return $emission;
}
public function getCodeDemandeur()
{
return $this->codeDemandeur;
}
public function setCodeDemandeur($codeDemandeur)
{
$this->codeDemandeur = $codeDemandeur;
}
private function createOperationCode(Emission $emission)
{
$xml = '';
foreach ($emission->getCoupons() as $coupon) {
$xml .= '<operationCode>'.$coupon->getOperationCode().'</operationCode>';
}
return $xml;
}
}