<?php
namespace App\Controller\Operation;
use App\Controller\APIController;
use App\Model\Operation\Odr;
use App\Service\APIKiwi;
use Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* Class OdrController.
*
* @Route("/offre-de-remboursement")
*/
class OdrController extends APIController
{
/**
* @Route("/", name="kiwi_operation_odr")
*
* @throws Exception|TransportExceptionInterface
*/
public function index(APIKiwi $APIKiwi)
{
$user = $this->getUser();
if ($user) {
$odrResp = $APIKiwi->getKiwi(['path' => '/odrs'.'?nbParticipations=true', 'token' => $user->getToken()]);
$odrs = $this->batchDeserialize(json_decode($odrResp->getContent(), true)['hydra:member'], Odr::class, 'get_odrs');
} else {
$odrResp = $this->APIKiwi->getPublicKiwi('odrs', [
'nbParticipations' => true,
'online' => true,
'startDate[strictly_before]' => date('Y-m-d H:i:s'),
'subscriptionEndDate[strictly_after]' => date('Y-m-d H:i:s'),
]);
$odrs = $this->batchDeserialize(json_decode($odrResp->getContent(), true)['hydra:member'], Odr::class, 'get_odrs');
}
if (1 === count($odrs)) {
return $this->redirectToRoute('kiwi_operation_odr_show', ['odrId' => $odrs[0]->getId()]);
}
return $this->render('operation/odr/index.html.twig', [
'odrs' => $odrs,
'target_path' => 'kiwi_operation_odr',
'register_path' => 'kiwi_operation_odr_register_user',
]);
}
/**
* @Route("/odr/{odrId}", name="kiwi_operation_odr_show")
*/
public function show($odrId): Response
{
$odrResp = $this->APIKiwi->getPublicKiwi('odr/'.$odrId);
$odr = $this->deserialize($odrResp->getContent(), Odr::class, 'get_odr');
$registerRoute = 'kiwi_operation_odr_register_user';
$user = null;
$userInterface = $this->getUser();
if ($userInterface) {
$user = $this->APIKiwi->getUserData($userInterface);
}
$this->requestStack->getSession()->set('kiwi_operation_odr_id', $odr->getId());
$this->requestStack->getSession()->set('_target_path', 'kiwi_operation_odr_form_participation_user');
$this->requestStack->getSession()->set('_target_path_params', ['odrId' => $odrId]);
return $this->render('operation/odr/details.html.twig', [
'odr' => $odr,
'register_path' => $registerRoute,
'target_path' => 'kiwi_operation_odr',
'user' => $user,
]);
}
}