src/Twig/DiscountCodeExtension.php line 125

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\Main\Cart;
  4. use App\Entity\Main\DiscountCode;
  5. use App\Entity\Main\User;
  6. use App\Exceptions\Main\DiscountCode\DiscountCodeAlreadyUsedException;
  7. use App\Exceptions\Main\DiscountCode\DiscountCodeNotValidRegistrationException;
  8. use App\Exceptions\Main\DiscountCode\DiscountCodeWrongUserException;
  9. use App\Services\CartManager;
  10. use App\Services\TrackingManager;
  11. use App\Services\ABTestManager;
  12. use App\Services\DiscountCodeManager;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\ORM\NonUniqueResultException;
  15. use Twig\Extension\AbstractExtension;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Twig\TwigFunction;
  18. class DiscountCodeExtension extends AbstractExtension
  19. {
  20.     /**
  21.      * @var DiscountCodeManager
  22.      */
  23.     private $discountCodeManager;
  24.     /**
  25.      * @var ABTestManager
  26.      */
  27.     private $ABTestManager;
  28.     /**
  29.      * @var TrackingManager
  30.      */
  31.     private $trackingFactory;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     private $entityManager;
  36.     /**
  37.      * @var CartManager
  38.      */
  39.     private $cartFactory;
  40.     /**
  41.      * DiscountCodeExtension constructor.
  42.      * @param DiscountCodeManager $discountCodeManager
  43.      * @param ABTestManager $ABTestManager
  44.      * @param TrackingManager $trackingFactory
  45.      * @param EntityManagerInterface $entityManager
  46.      * @param CartManager $cartFactory
  47.      */
  48.     public function __construct(
  49.         DiscountCodeManager $discountCodeManager,
  50.         ABTestManager $ABTestManager,
  51.         TrackingManager $trackingFactory,
  52.         EntityManagerInterface $entityManager,
  53.         CartManager $cartFactory
  54.     ) {
  55.         $this->discountCodeManager $discountCodeManager;
  56.         $this->ABTestManager $ABTestManager;
  57.         $this->trackingFactory $trackingFactory;
  58.         $this->entityManager $entityManager;
  59.         $this->cartFactory $cartFactory;
  60.     }
  61.     /**
  62.      * @return array|TwigFunction[]
  63.      */
  64.     public function getFunctions(): array
  65.     {
  66.         return [
  67.             new TwigFunction('getPromoCodesOfDay', [$this'getPromoCodesOfDay']),
  68.             new TwigFunction('isPromoCodeExpired', [$this'isPromoCodeExpired']),
  69.             new TwigFunction('getPromoCodeValueText', [$this'getPromoCodeValueText']),
  70.             new TwigFunction('canShowPromoCodeAlertFromGetParam', [$this'canShowPromoCodeAlertFromGetParam'])
  71.         ];
  72.     }
  73.     /**
  74.      * @return array|null
  75.      */
  76.     public function getPromoCodesOfDay()
  77.     {
  78.         return $this->discountCodeManager->getPromoCodesOfDay();
  79.     }
  80.     /**
  81.      * @param DiscountCode $discountCode
  82.      * @return bool
  83.      */
  84.     public function isPromoCodeExpired(DiscountCode $discountCode)
  85.     {
  86.         return $this->discountCodeManager->isPromoCodeExpired($discountCode);
  87.     }
  88.     /**
  89.      * @param string $discountCodeName
  90.      * @return string|null
  91.      * @throws NonUniqueResultException
  92.      */
  93.     public function getPromoCodeValueText(string $discountCodeName)
  94.     {
  95.         $discountCode =  $this->discountCodeManager->getActivePromoCodeByName($discountCodeName);
  96.         $text null;
  97.         if (DiscountCode::PERCENTAGE_RATE == $discountCode->getDiscountCodeType()) {
  98.             $text $discountCode->getDiscountCodeValue() . '%';
  99.         } elseif (DiscountCode::FIXED_AMOUNT == $discountCode->getDiscountCodeType()) {
  100.             $text $discountCode->getDiscountCodeValue() / 100 '€';
  101.         }
  102.         return $text;
  103.     }
  104.     /**
  105.      * @param Request $request
  106.      * @param $user
  107.      * @return bool
  108.      * @throws NonUniqueResultException
  109.      */
  110.     public function canShowPromoCodeAlertFromGetParam(Request $request$user)
  111.     {
  112.         if ($request->getSession()) {
  113.             $dcodeParam $request->getSession()->get('dcode');
  114.         }
  115.         // Purchase is over, remove the code to prevent having it flying around ...
  116.         if ('brulafine_over' === $request->get('_route')) {
  117.             $request->getSession()->set('dcode'null);
  118.             return false;
  119.         }
  120.         // check if dcode exists.
  121.         if (empty($dcodeParam)) {
  122.             return false;
  123.         }
  124.         // check if the code has been already applied for the current session.
  125.         if ($request->getSession()->get(DiscountCode::DISCOUNT_CODE_HIDE_ALERT)) {
  126.             return false;
  127.         }
  128.         $cart $this->cartFactory->resolveCurrentCart();
  129.         $discountCode $this->discountCodeManager->getActivePromoCodeByName($dcodeParam);
  130.         if (!$discountCode instanceof DiscountCode) {
  131.             return false;
  132.         }
  133.         if ($this->discountCodeManager->isDiscountCodeExpired($discountCode)) {
  134.             return false;
  135.         }
  136.         // if has cart and user is logged in.
  137.         if ($cart instanceof Cart && $cart->getUser() instanceof User) {
  138.             // check if this code is applied in the cart.
  139.             if ($cart->getDiscountCode() instanceof DiscountCode && $cart->getDiscountCode()->getDiscountCodeName() == $dcodeParam) {
  140.                 $request->getSession()->set(DiscountCode::DISCOUNT_CODE_HIDE_ALERTtrue);
  141.                 return false;
  142.             } else {
  143.                 // check if this code can be applied in the cart.
  144.                 try {
  145.                     $this->cartFactory->checkCanApplyCodeToCart($discountCode$cart);
  146.                 } catch (DiscountCodeAlreadyUsedException $e) {
  147.                     // check if discount code already used.
  148.                     return false;
  149.                 } catch (DiscountCodeWrongUserException $e) {
  150.                     // check if discount code is for this user.
  151.                     return false;
  152.                 } catch (\Exception $e) {
  153.                     $request->getSession()->set(DiscountCode::DISCOUNT_CODE_HIDE_ALERTtrue);
  154.                     return false;
  155.                 }
  156.             }
  157.             // if has no cart and user is logged in
  158.         } elseif ($user instanceof User) {
  159.             // check if can apply SINGLE_USAGE and SINGLE_CLIENT_USAGE
  160.             if (!$this->discountCodeManager->canApplySingleUsageCode($discountCode)) {
  161.                 return false;
  162.             }
  163.             try {
  164.                 $this->discountCodeManager->canCodeBeUsedByUser($user$discountCode);
  165.                 // check if discount code already used by this user.
  166.             } catch (DiscountCodeAlreadyUsedException $e) {
  167.                 return false;
  168.             } catch (DiscountCodeNotValidRegistrationException $e) {
  169.                 return false;
  170.             } catch (\Exception $e) {
  171.                 $request->getSession()->set(DiscountCode::DISCOUNT_CODE_HIDE_ALERTtrue);
  172.                 return false;
  173.             }
  174.         }
  175.         return true;
  176.     }
  177.     /**
  178.      * @return string
  179.      */
  180.     public function getName()
  181.     {
  182.         return 'discount_code_extension';
  183.     }
  184. }