HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/brameda/brameda.nl/app/Komma/Shop/Discounts/Conditions/CouponDiscountCondition.php
<?php


namespace App\Komma\Shop\Discounts\Conditions;

use App\Komma\Shop\Cart\HasCouponCodesInterface;
use App\Komma\Shop\Discounts\DiscountableInterface;
use App\Komma\Shop\Discounts\DiscountTypes;


/**
 * Helps applying a discount to a specific discountable
 *
 * @package App\Komma\Shop\Discounts
 */
class CouponDiscountCondition extends AbstractDiscountCondition
{
    private $couponCode;

    /**
     * AbsoluteDiscountCondition constructor.
     * @param string $couponCode
     */
    public function __construct($couponCode = '')
    {
        $conditionSettings = $couponCode;

        parent::__construct($conditionSettings);
    }

    /**
     * Whether or not the discount that this condition belongs to can be applied according to this rule
     *
     * @param DiscountableInterface $discountable
     * @return bool
     */
    public function canBeApplied(DiscountableInterface $discountable): bool
    {
        if(!is_a($discountable, HasCouponCodesInterface::class)) return false;

        /** @var DiscountableInterface|HasCouponCodesInterface $discountable */

        //Return false if the coupon code already is used in the discountable
        $discounts = $discountable->getDiscounts();
        foreach($discounts as $discount)
        {
            if($discount->type == DiscountTypes::Coupon && $discount->hasDiscountCondition() && $discount->getDiscountCondition()->getValue() == $this->couponCode) return false;
        }

        if(!in_array($this->couponCode, $discountable->getCouponCodes())) return false;

        return true;
    }

    /**
     * Check that the given coupon code is valid
     *
     * @param string $couponCode
     * @return bool
     */
    public function couponCodeIsValid(string $couponCode):bool
    {
        return ($couponCode == $this->couponCode);
    }

    /**
     * Dissect a value that comes from a discount into something that the DiscountCondition understands
     *
     * @param string $discountValue
     * @return bool
     */
    protected function dissectDiscountValue(string $discountValue): bool
    {
        $discountValueParts = $this->returnDissectedValueIfValid($discountValue);
        $this->couponCode = $discountValueParts[0];

        return false;
    }

    /**
     * Create a new instance from the settings from a discount model
     *
     * @param string $discountConditionSettings
     * @return AbstractDiscountCondition
     */
    public static function newInstanceFromDiscountSettings(string $discountConditionSettings): AbstractDiscountCondition
    {
        $settings = CouponDiscountCondition::returnDissectedValueIfValid($discountConditionSettings);
        return new self($settings[0]);
    }


    /**
     * Checks if the value delimited string is correctly defined and returns the separate values
     *
     * @param $discountValue
     * @return array
     */
    private static function returnDissectedValueIfValid($discountValue):array
    {
        $discountValueParts = explode(',', $discountValue);
        if(!count($discountValueParts) == 1) throw new \InvalidArgumentException('The value for an coupon discount must be specified like this: <couponcode>. Got: '.$discountValue);

        return $discountValueParts;
    }
}