custom/plugins/EnetCredit/src/Subscriber/ValidationSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace EnetCredit\Subscriber;
  3. use EnetCredit\Service\UserCreditPayment;
  4. use EnetCredit\Validator\UserCredit;
  5. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  6. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  7. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ValidationSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var \Shopware\Core\Framework\Validation\DataValidator $validator */
  12.     private $validator;
  13.     /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
  14.     private $requestStack;
  15.     /** @var \Shopware\Core\Checkout\Cart\SalesChannel\CartService $cartService */
  16.     private $cartService;
  17.     public function __construct($validator$requestStack$cartService)
  18.     {
  19.         $this->validator $validator;
  20.         $this->requestStack $requestStack;
  21.         $this->cartService $cartService;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             'framework.validation.order.create' => 'userCreditValidation'
  27.         ];
  28.     }
  29.     public function userCreditValidation(BuildValidationEvent $event)
  30.     {
  31.         $data $this->requestStack->getCurrentRequest()->request;
  32.         /** @var \Shopware\Core\System\SalesChannel\SalesChannelContext $salesChannelContext */
  33.         $salesChannelContext $this->requestStack->getCurrentRequest()->get('sw-sales-channel-context');
  34.         $customer $salesChannelContext->getCustomer();
  35.         $paymentMethod $salesChannelContext->getPaymentMethod();
  36.         if($paymentMethod->getHandlerIdentifier() === UserCreditPayment::class) {
  37.             $customerCustomFields $customer->getCustomFields();
  38.             $definition = new DataValidationDefinition('order.enet_user_credit');
  39.             $definition->add('enet_user_credit', new UserCredit());
  40.             if(isset($customerCustomFields['enet_credit_value']) && $customerCustomFields['enet_credit_value']) {
  41.                 $userCreditValue floatval($customerCustomFields['enet_credit_value']);
  42.                 $cart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  43.                 $cartTotalPrice $cart->getPrice()->getTotalPrice();
  44.                 if($userCreditValue $cartTotalPrice) {
  45.                     UserCredit::$errors[] = 'to-low-user-credit';
  46.                 }
  47.             }
  48.             else {
  49.                 UserCredit::$errors[] = 'no-user-credit';
  50.             }
  51.             $violations $this->validator->getViolations($data->all(), $definition);
  52.             if (!$violations->count()) {
  53.                 return;
  54.             }
  55.             throw new ConstraintViolationException($violations$data->all());
  56.         }
  57.     }
  58. }