src/Controller/FundController.php line 91

Open in your IDE?
  1. <?php
  2.     namespace App\Controller;
  3.     use App\Form\FundForm;
  4.     use App\Service\TessituraBundle\AddNotAvailableException;
  5.     use App\Service\TessituraBundle\CartService;
  6.     use App\Service\TessituraBundle\Entity\CateringItemPerformance;
  7.     use App\Service\TessituraBundle\FundService;
  8.     use App\Service\TessituraBundle\Item\CartItemContribution;
  9.     use App\Service\TessituraBundle\PackageService;
  10.     use App\Service\TessituraSDK\TessituraClient;
  11.     use App\Service\TessituraSDK\TessituraClientException;
  12.     use Psr\Cache\CacheItemPoolInterface;
  13.     use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14.     use Symfony\Component\Routing\Annotation\Route;
  15.     use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16.     use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  17.     use Symfony\Component\DependencyInjection\ContainerInterface;
  18.     use Symfony\Component\Form\FormError;
  19.     use Symfony\Component\HttpFoundation\JsonResponse;
  20.     use Symfony\Component\HttpFoundation\Request;
  21.     use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  22.     use Symfony\Component\Serializer\Serializer;
  23.     use Symfony\Component\Serializer\SerializerInterface;
  24.     use Symfony\Contracts\Translation\TranslatorInterface;
  25.     class FundController extends AbstractController
  26.     {
  27.         /**
  28.          * @var TessituraClient
  29.          */
  30.         private $client;
  31.         /**
  32.          * @var Serializer
  33.          */
  34.         private $serializer;
  35.         /**
  36.          * @var CacheItemPoolInterface|TagAwareAdapterInterface
  37.          */
  38.         private $cache;
  39.         /**
  40.          * @var TranslatorInterface
  41.          */
  42.         private $translator;
  43.         /**
  44.          * @var ParameterBagInterface
  45.          */
  46.         private $params;
  47.         /**
  48.          * CartController constructor.
  49.          *
  50.          * @param TessituraClient $client
  51.          * @param SerializerInterface $serializer
  52.          * @param ContainerInterface $container
  53.          * @param TranslatorInterface $translator
  54.          * @param ParameterBagInterface $params
  55.          */
  56.         public function __construct(
  57.             TessituraClient $client,
  58.             SerializerInterface $serializer,
  59.             ContainerInterface $container,
  60.             TranslatorInterface $translator,
  61.             ParameterBagInterface $params
  62.         ) {
  63.             $this->client     $client;
  64.             $this->serializer $serializer;
  65.             $this->container  $container;
  66.             $this->cache      $container->get('cache.app');
  67.             $this->translator $translator;
  68.             $this->params     $params;
  69.         }
  70.         /**
  71.          * @Route(
  72.          *     "/{_locale}/fund/",
  73.          *     name="fund",
  74.          *     defaults={
  75.          *          "_locale": "fi"
  76.          *     },
  77.          *     requirements={
  78.          *          "_locale": "fi|sv|en"
  79.          *     }
  80.          * )
  81.          * @param Request $request
  82.          * @param CartService $cartService
  83.          *
  84.          * @return \Symfony\Component\HttpFoundation\Response
  85.          */
  86.         public function fundAction(Request $requestFundService $fundServiceCartService $cartService)
  87.         {
  88.             $items $fundService->getAvailable();
  89.             $items array_filter($items, function ($item) {
  90.                 // remove opera beyond (15)
  91.                 // remove ballet slippers fund (16)
  92.                 $removedFunds = [1516];
  93.                 if (!in_array($item->getId(), $removedFunds)) {
  94.                     return $item;
  95.                 }
  96.             });
  97.             $orderedItems $fundService->orderItems($items);
  98.             $defaultDonationValues $this->params->get('app.default_donation_amounts');
  99.             $formViews $this->createFundForms($orderedItems);
  100.             return $this->render('page/fund.html.twig', [
  101.                 'ordered_items' => $orderedItems,
  102.                 'items'         => $items,
  103.                 'amounts'       => $defaultDonationValues,
  104.                 'forms'         => $formViews
  105.             ]);
  106.         }
  107.         /**
  108.          * @param array $items
  109.          * @return array
  110.          */
  111.         public function createFundForms(array $items)
  112.         {
  113.             $forms = [];
  114.             foreach ($items as $i => $array) {
  115.                 $forms[] = array_map(function ($item) {
  116.                     return $this->createForm(FundForm::class, [$item->getId()])->createView();
  117.                 }, $array);
  118.             }
  119.             return $forms;
  120.         }
  121.         /**
  122.          * @Route(
  123.          *     "/{_locale}/fund/api/v1/fund-add/{fundId}/",
  124.          *     name="post-api-fund-add",
  125.          *     defaults={
  126.          *          "_locale": "fi"
  127.          *     },
  128.          *     requirements={
  129.          *          "_locale": "fi|sv|en"
  130.          *     },
  131.          *     methods={"POST"}
  132.          * )
  133.          * @param Request $request
  134.          * @param CartService $cartService
  135.          * @param $fundId
  136.          *
  137.          * @return JsonResponse
  138.          * @throws \Psr\Cache\InvalidArgumentException
  139.          */
  140.         public function addToCartAction(Request $requestCartService $cartService$fundId)
  141.         {
  142.             $amount $request->get('amount');
  143.             $token $request->get('token');
  144.             $fundId = (int) $fundId;
  145.             if (!$this->isCsrfTokenValid($fundId$token)) {
  146.                 throw new InvalidCsrfTokenException('Invalid CSRF token.');
  147.             }
  148.             if ($amount $this->params->get('tessitura.donation_max_value')) {
  149.                 return new JsonResponse([
  150.                     'success' => false,
  151.                     'message' => $this->translator->trans('fund_add_failure')
  152.                 ]);
  153.             }
  154.             try {
  155.                 $cartService->evaluateFundsInCart(CartItemContribution::class, $fundId);
  156.             } catch (AddNotAvailableException $exception) {
  157.                 if ($fundId == $this->params->get('tessitura.donation_fund')) {
  158.                     return new JsonResponse([
  159.                         'success' => false,
  160.                         'message' => $this->translator->trans('fund_in_cart_error')
  161.                     ]);
  162.                 } else {
  163.                     return new JsonResponse([
  164.                         'success' => false,
  165.                         'message' => $this->translator->trans('fund_other_in_cart_error')
  166.                     ]);
  167.                 }
  168.             }
  169.             try {
  170.                 $cartService->addContribution($amount$fundId);
  171.             } catch (TessituraClientException $exception) {
  172.                 return new JsonResponse([
  173.                     'success' => false,
  174.                     'message' => $this->translator->trans('fund_add_failure')
  175.                 ]);
  176.             }
  177.             return new JsonResponse([
  178.                 'success' => true,
  179.                 'redirect' => $this->generateUrl('cart'),
  180.             ]);
  181.         }
  182.     }