custom/plugins/EnetPickupDate/src/Subscriber/ValidationSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace EnetPickupDate\Subscriber;
  3. use EnetPickupDate\Exception\NoDatePresentError;
  4. use EnetPickupDate\Validator\OpeningTimes;
  5. use EnetPickupDate\Validator\PickupDate;
  6. use EnetPickupDate\Validator\PublicHoliday;
  7. use EnetPickupDate\Validator\PublicHolidayValidator;
  8. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  9. use Shopware\Core\Checkout\Payment\Exception\InvalidOrderException;
  10. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  11. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  12. use Shopware\Core\Framework\Validation\DataValidator;
  13. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  14. use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Shopware\Core\Checkout\Cart\Error\Error;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Validator\Constraints\NotBlank;
  18. class ValidationSubscriber implements EventSubscriberInterface
  19. {
  20.     /** @var DataValidator $validator */
  21.     private $validator;
  22.     /** @var RequestStack $requestStack */
  23.     private $requestStack;
  24.     /** @var CartService $cartService */
  25.     private $cartService;
  26.     public function __construct(DataValidator $validatorRequestStack $requestStackCartService $cartService)
  27.     {
  28.         $this->validator $validator;
  29.         $this->requestStack $requestStack;
  30.         $this->cartService $cartService;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             'framework.validation.order.create' => 'cartValidation'
  36.         ];
  37.     }
  38.     /**
  39.      * Set the PickupDate Validator
  40.      * if LineItems are less then one or if there NOT is only an UserCredit item in cart
  41.      *
  42.      * @param BuildValidationEvent $event
  43.      */
  44.     public function cartValidation(BuildValidationEvent $event)
  45.     {
  46.         $salesChannelContext $this->requestStack->getCurrentRequest()->get('sw-sales-channel-context');
  47.         $cart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  48.         $hasUserCreditProduct false;
  49.         foreach($cart->getLineItems() as $lineItem) {
  50.             $customFields $lineItem->getPayloadValue('customFields');
  51.             if(isset($customFields['enet_credit_product']) && $customFields['enet_credit_product'] === true) {
  52.                 $hasUserCreditProduct true;
  53.             }
  54.         }
  55. //        dump(
  56. //            count($cart->getLineItems()) > 1,
  57. //            $hasUserCreditProduct === false
  58. //        );
  59. //        die();
  60.         if(count($cart->getLineItems()) > || $hasUserCreditProduct === false) {
  61.             $data $this->requestStack->getCurrentRequest()->request;
  62.             $definition = new DataValidationDefinition('order.pickupdate');
  63.             $definition->add('enet_pickup',
  64.                 new PickupDate()
  65.             );
  66.             $violations $this->validator->getViolations($data->all(), $definition);
  67.             if (!$violations->count()) {
  68.                 return;
  69.             }
  70.             throw new ConstraintViolationException($violations$data->all());
  71.         }
  72.     }
  73. }