/** * Check if a coupon is currently active. * * @param CouponInterface $coupon Coupon to check activeness * @param DateTime $now * * @return bool */ private function isActive(CouponInterface $coupon, \DateTime $now = null) { if (!$coupon->isEnabled()) { return false; } $now = $now ?: $this->dateTimeFactory->create(); if ($coupon->getValidFrom() > $now) { return false; } $validTo = $coupon->getValidTo(); if ($validTo && $now > $validTo) { return false; } return true; }
/** * Creates a new coupon instance, given an existing Coupon as reference * * You can specify a DateTime new coupon will be valid from. * If not specified, current DateTime will be used * * If given coupon is valid forever, new coupon will also be * Otherwise, this method will add to validFrom, the same interval than given Coupon * * Also can be specified how new Coupon name must be defined. * If none, automatic generator will add to existing name, 10 random digits. * * Given Coupon name: FOO * New Coupon name: FOO_a67b6786a6 * * Coupons are only generated, and are not persisted in Manager not Flushed * * @param CouponInterface $coupon Reference coupon * @param DateTime $dateFrom Date From. If null, takes actual dateTime * * @return CouponInterface Coupon generated */ public function duplicateCoupon(CouponInterface $coupon, DateTime $dateFrom = null) { /** * Creates a valid date interval given the referent Coupon */ if (!$dateFrom instanceof DateTime) { $dateFrom = new DateTime(); } $dateTo = null; if ($coupon->getValidTo() instanceof DateTime) { $interval = $coupon->getValidFrom()->diff($coupon->getValidTo()); $dateTo = clone $dateFrom; $dateTo->add($interval); } /** * @var CouponInterface $couponGenerated */ $couponGenerated = $this->couponFactory->create(); $couponGenerated->setCode($this->couponCodeGenerator->generate())->setName($coupon->getName())->setType($coupon->getType())->setPrice($coupon->getPrice())->setDiscount($coupon->getDiscount())->setCount($coupon->getCount())->setPriority($coupon->getPriority())->setMinimumPurchase($coupon->getMinimumPurchase())->setValidFrom($dateFrom)->setValidTo($dateTo)->setEnabled(true); return $couponGenerated; }