<?php
namespace Plugin\Synplgbase\Service\PurchaseFlow\Processor;
use Eccube\Entity\ItemInterface;
use Eccube\Service\PurchaseFlow\InvalidItemException;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\ItemValidator;
use Plugin\Synplgbase\Repository\ConfigRepository;
use Eccube\Service\CartService;
use Eccube\Repository\ProductClassRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DeliveryAdvanceValidator extends ItemValidator
{
/**
* @var ConfigRepository
*/
protected $configRepository;
/**
* @var CartService
*/
protected $cartService;
/**
* @var ProductClassRepository
*/
protected $productClassRepository;
/**
* @var ItemInterface
*/
private $target_item;
public function __construct(ContainerInterface $containerInterface)
{
$this->configRepository = $containerInterface->get(ConfigRepository::class)->get();
$this->cartService = $containerInterface->get(CartService::class);
$this->productClassRepository = $containerInterface->get(ProductClassRepository::class);
}
protected function validate(ItemInterface $item, PurchaseContext $context)
{
if(!$item->isProduct()){
return;
}
if(!$this->configRepository->getDeliveryAdvanceOn()){
return;
}
if(is_null($this->cartService->getCart())){
return;
}
$delivery_advance_value_total = 0;
$arrCartItems = $this->cartService->getCart()->getCartItems();
foreach($arrCartItems as $value){
$delivery_advance_value_total += $value->getProductClass()->getDeliveryAdvanceValue() * $value->getQuantity();
if(!is_null($this->configRepository->getDeliveryAdvanceCart()) && $delivery_advance_value_total > $this->configRepository->getDeliveryAdvanceCart()){
$this->target_item=$value;
throw new InvalidItemException('カートに追加できるのは'.floatval($this->configRepository->getDeliveryAdvanceCart()).$this->configRepository->getDeliveryAdvanceUnit().'までです。この商品はカートに追加されません。');
}
}
}
protected function handle(ItemInterface $item, PurchaseContext $context)
{
$qantity = $this->target_item->getQuantity();
$this->target_item->setQuantity($qantity-1);
}
}