public function buildCouponFromCode($couponCode)
    {
        /** @var Coupon $couponModel */
        $couponModel = $this->facade->findOneCouponByCode($couponCode);
        if ($couponModel === null) {
            return false;
        }

        // check if coupon is enabled
        if (!$couponModel->getIsEnabled()) {
            throw new InactiveCouponException($couponCode);
        }

        $nowDateTime = new \DateTime();

        // Check coupon start date
        if ($couponModel->getStartDate() !== null && $couponModel->getStartDate() > $nowDateTime) {
            throw new CouponNotReleaseException($couponCode);
        }

        // Check coupon expiration date
        if ($couponModel->getExpirationDate() < $nowDateTime) {
            throw new CouponExpiredException($couponCode);
        }

        // Check coupon usage count
        if (! $couponModel->isUsageUnlimited()) {
            if (null === $customer = $this->facade->getCustomer()) {
                throw new UnmatchableConditionException($couponCode);
            }

            if ($couponModel->getUsagesLeft($customer->getId()) <= 0) {
                throw new CouponNoUsageLeftException($couponCode);
            }
        }

        /** @var CouponInterface $couponInterface */
        $couponInterface = $this->buildCouponFromModel($couponModel);

        if ($couponInterface && $couponInterface->getConditions()->count() == 0) {
            throw new InvalidConditionException(
                \get_class($couponInterface)
            );
        }

        return $couponInterface;
    }
public function buildCouponFromCode($couponCode)
    {
        /** @var Coupon $couponModel */
        $couponModel = $this->facade->findOneCouponByCode($couponCode);
        if ($couponModel === null) {
            return false;
        }

        // check if coupon is enabled
        if (!$couponModel->getIsEnabled()) {
            throw new InactiveCouponException($couponCode);
        }

        $nowDateTime = new \DateTime();

        // Check coupon start date
        if ($couponModel->getStartDate() !== null && $couponModel->getStartDate() > $nowDateTime) {
            throw new CouponNotReleaseException($couponCode);
        }

        // Check coupon expiration date
        if ($couponModel->getExpirationDate() < $nowDateTime) {
            throw new CouponExpiredException($couponCode);
        }

        // Check coupon usage count
        if (! $couponModel->isUsageUnlimited()) {
            if (null === $customer = $this->facade->getCustomer()) {
                throw new UnmatchableConditionException($couponCode);
            }

            if ($couponModel->getUsagesLeft($customer->getId()) <= 0) {
                throw new CouponNoUsageLeftException($couponCode);
            }
        }

        /** @var CouponInterface $couponInterface */
        $couponInterface = $this->buildCouponFromModel($couponModel);

        if ($couponInterface && $couponInterface->getConditions()->count() == 0) {
            throw new InvalidConditionException(
                \get_class($couponInterface)
            );
        }

        return $couponInterface;
    }
public function buildCouponFromCode ( $ couponCode ) { /** @var Coupon $couponModel */ $ couponModel = $ this -> facade -> findOneCouponByCode ( $ couponCode ) ; if ( $ couponModel === null ) { return false ; } // check if coupon is enabled if ( ! $ couponModel -> getIsEnabled ( ) ) { throw new InactiveCouponException ( $ couponCode ) ; } $ nowDateTime = new \ DateTime ( ) ; // Check coupon start date if ( $ couponModel -> getStartDate ( ) !== null && $ couponModel -> getStartDate ( ) > $ nowDateTime ) { throw new CouponNotReleaseException ( $ couponCode ) ; } // Check coupon expiration date if ( $ couponModel -> getExpirationDate ( ) < $ nowDateTime ) { throw new CouponExpiredException ( $ couponCode ) ; } // Check coupon usage count if ( ! $ couponModel -> isUsageUnlimited ( ) ) { if ( null === $ customer = $ this -> facade -> getCustomer ( ) ) { throw new UnmatchableConditionException ( $ couponCode ) ; } if ( $ couponModel -> getUsagesLeft ( $ customer -> getId ( ) ) <= 0 ) { throw new CouponNoUsageLeftException ( $ couponCode ) ; } } /** @var CouponInterface $couponInterface */ $ couponInterface = $ this -> buildCouponFromModel ( $ couponModel ) ; if ( $ couponInterface && $ couponInterface -> getConditions ( ) -> count ( ) == 0 ) { throw new InvalidConditionException ( \ get_class ( $ couponInterface ) ) ; } return $ couponInterface ; }
Build a CouponInterface from its database data

@param string $couponCode Coupon code ex: XMAS
@return CouponInterface
@throws CouponExpiredException
@throws CouponNoUsageLeftException
@throws CouponNotReleaseException
Build a CouponInterface from its database data