File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Discounts/Conditions/CartSubtotalHandler.php
<?php declare(strict_types=1);
namespace App\Discounts\Conditions;
use App\Cart\ShoppingCart;
use App\Discounts\DiscountableInterface;
use App\Discounts\DiscountService;
use App\Vat\VatService;
class CartSubtotalHandler extends CountConditionHandler
{
protected ?int $type = DiscountConditionTypes::CART_SUBTOTAL;
public function canBeApplied(DiscountableInterface $discountable, string $operator, string $param): bool
{
if(!is_a($discountable, ShoppingCart::class)) return false;
$discountService = new DiscountService();
$vatService = new VatService();
//Calculate the total inc vat price of the shopping cart
$subtotalInc = 0;
foreach($discountable->getItems() as $item) {
$discountService->applyDiscountsTo($item);
$vatService->calculateVatForModelWithVatScenarioEnum($item);
$subtotalInc += $item->getPriceInc();
}
$result = false;
if($operator === '<') {
if($subtotalInc < intval($param)) $result = true;
} else if($operator === '<=') {
if($subtotalInc <= intval($param)) $result = true;
} else if($operator === '==') {
if($subtotalInc == intval($param)) $result = true;
} else if($operator === '!=') {
if($subtotalInc != intval($param)) $result = true;
} else if($operator >= '>=') {
if($subtotalInc >= intval($param)) $result = true;
} else if($operator > '>') {
if($subtotalInc > intval($param)) $result = true;
} else if($operator === 'in') {
if(in_array($subtotalInc, $this->dissectParam($param), true)) $result = true;
} else if($operator === 'not_in') {
if(!in_array($subtotalInc, $this->dissectParam($param), true)) $result = true;
}
return $result;
}
private function dissectParam(string $param) {
return array_map('trim', explode(',', $param));
}
}