custom/plugins/EasyCreditRatenkauf/src/Subscriber/ExpressCheckoutCartHandler.php line 92

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Netzkollektiv\EasyCredit\Subscriber;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Netzkollektiv\EasyCredit\Api\Storage;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  13. use Shopware\Storefront\Controller\CartLineItemController;
  14. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  15. use Shopware\Core\Checkout\Cart\Cart;
  16. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  17. use Netzkollektiv\EasyCredit\Cart\InitError;
  18. use Netzkollektiv\EasyCredit\Service\CustomerService;
  19. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  20. class ExpressCheckoutCartHandler implements EventSubscriberInterface
  21. {
  22.     private Storage $storage;
  23.     public function __construct(
  24.         Storage $storage
  25.     ) {
  26.         $this->storage $storage;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             KernelEvents::CONTROLLER_ARGUMENTS => 'clearCart',
  32.             CheckoutCartPageLoadedEvent::class => 'addErrorsToCart',
  33.             'framework.validation.address.create' => 'disableAddressValidation',
  34.             'framework.validation.customer.create' => 'disableCustomerValidation'
  35.         ];
  36.     }
  37.     public function clearCart(ControllerArgumentsEvent $event)
  38.     {
  39.         $controller $event->getController();
  40.         $arguments $event->getArguments();
  41.         $request $event->getRequest();
  42.         if (\is_array($controller)) {
  43.             $controller $controller[0];
  44.         }
  45.         $cart $arguments[0] ?? null;
  46.         if (
  47.             !$controller instanceof CartLineItemController ||
  48.             $cart === null ||
  49.             !$request->get('easycredit')
  50.         ) {
  51.             return;
  52.         }
  53.         $this->storage->clear();
  54.         foreach ($cart->getLineItems() as $lineItem) {
  55.             $cart->getLineItems()->removeElement($lineItem);
  56.         }
  57.     }
  58.     public function addErrorsToCart(CheckoutCartPageLoadedEvent $event)
  59.     {
  60.         if ($errorMessage $this->storage->get('error')) {
  61.             $this->storage->set('error'null)->persist();
  62.             $error = new InitError($errorMessage);
  63.             $event->getPage()->getCart()->getErrors()->add($error);
  64.         }
  65.     }
  66.     public function disableAddressValidation(BuildValidationEvent $event): void
  67.     {
  68.         if (!$event->getContext()->hasExtension(CustomerService::EXPRESS_ACTIVE)) {
  69.             return;
  70.         }
  71.         $event->getDefinition()->set('additionalAddressLine1')
  72.             ->set('additionalAddressLine2')
  73.             ->set('phoneNumber');
  74.     }
  75.     public function disableCustomerValidation(BuildValidationEvent $event): void
  76.     {
  77.         if (!$event->getContext()->hasExtension(CustomerService::EXPRESS_ACTIVE)) {
  78.             return;
  79.         }
  80.         $event->getDefinition()->set('birthdayDay')
  81.             ->set('birthdayMonth')
  82.             ->set('birthdayYear');
  83.     }
  84. }