src/Controller/GiftCardController.php line 87

Open in your IDE?
  1. <?php
  2.     namespace App\Controller;
  3.     use App\Service\TessituraBundle\AddNotAvailableException;
  4.     use App\Service\TessituraBundle\CartService;
  5.     use App\Service\TessituraBundle\Entity\CateringItemPerformance;
  6.     use App\Service\TessituraBundle\Item\CartItemGiftCertificate;
  7.     use App\Service\TessituraBundle\PackageService;
  8.     use App\Service\TessituraSDK\TessituraClient;
  9.     use App\Service\TessituraSDK\TessituraClientException;
  10.     use Psr\Cache\CacheItemPoolInterface;
  11.     use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12.     use Symfony\Component\Routing\Annotation\Route;
  13.     use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14.     use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  15.     use Symfony\Component\DependencyInjection\ContainerInterface;
  16.     use Symfony\Component\HttpFoundation\JsonResponse;
  17.     use Symfony\Component\HttpFoundation\Request;
  18.     use Symfony\Component\Serializer\Serializer;
  19.     use Symfony\Component\Serializer\SerializerInterface;
  20.     use Symfony\Contracts\Translation\TranslatorInterface;
  21.     class GiftCardController extends AbstractController
  22.     {
  23.         /**
  24.          * @var TessituraClient
  25.          */
  26.         private $client;
  27.         /**
  28.          * @var Serializer
  29.          */
  30.         private $serializer;
  31.         /**
  32.          * @var CacheItemPoolInterface|TagAwareAdapterInterface
  33.          */
  34.         private $cache;
  35.         /**
  36.          * @var TranslatorInterface
  37.          */
  38.         private $translator;
  39.         /**
  40.          * @var ParameterBagInterface
  41.          */
  42.         private $params;
  43.         /**
  44.          * CartController constructor.
  45.          *
  46.          * @param TessituraClient $client
  47.          * @param SerializerInterface $serializer
  48.          * @param ContainerInterface $container
  49.          * @param TranslatorInterface $translator
  50.          * @param ParameterBagInterface $params
  51.          */
  52.         public function __construct(
  53.             TessituraClient $client,
  54.             SerializerInterface $serializer,
  55.             ContainerInterface $container,
  56.             TranslatorInterface $translator,
  57.             ParameterBagInterface $params
  58.         ) {
  59.             $this->client     $client;
  60.             $this->serializer $serializer;
  61.             $this->container  $container;
  62.             $this->cache      $container->get('cache.app');
  63.             $this->translator $translator;
  64.             $this->params     $params;
  65.         }
  66.         /**
  67.          * @Route(
  68.          *     "/{_locale}/gift-card/",
  69.          *     name="gift-card",
  70.          *     defaults={
  71.          *          "_locale": "fi"
  72.          *     },
  73.          *     requirements={
  74.          *          "_locale": "fi|sv|en"
  75.          *     }
  76.          * )
  77.          * @param Request $request
  78.          * @param PackageService $packageService
  79.          *
  80.          * @return \Symfony\Component\HttpFoundation\Response
  81.          */
  82.         public function giftCardAction(Request $request)
  83.         {
  84.             return $this->render('page/gift-card.html.twig', [
  85.             ]);
  86.         }
  87.         /**
  88.          * @Route(
  89.          *     "/{_locale}/gift-card/api/v1/gift-card-add/",
  90.          *     name="post-api-gift-card-add",
  91.          *     defaults={
  92.          *          "_locale": "fi"
  93.          *     },
  94.          *     requirements={
  95.          *          "_locale": "fi|sv|en"
  96.          *     },
  97.          *     methods={"POST"}
  98.          * )
  99.          */
  100.         public function addToCartAction(Request $requestCartService $cartService)
  101.         {
  102.             $amount $request->get('amount');
  103.             try {
  104.                 $cartService->evaluateFundsInCart(CartItemGiftCertificate::class);
  105.             } catch (AddNotAvailableException $exception) {
  106.                 return new JsonResponse([
  107.                     'success' => false,
  108.                     'message' => $this->translator->trans('fund_in_cart_error')
  109.                 ]);
  110.             }
  111.             try {
  112.                 $cartService->addGiftCard($amount);
  113.             } catch (TessituraClientException $exception) {
  114.                 return new JsonResponse([
  115.                     'success' => false,
  116.                     'message' => $this->translator->trans('giftcard_add_failure')
  117.                 ]);
  118.             }
  119.             return new JsonResponse([
  120.                 'success' => true,
  121.                 'redirect' => $this->generateUrl('cart'),
  122.             ]);
  123.         }
  124.     }