custom/plugins/TpayShopwarePayment/src/Subscriber/SalesChannelContextSwitch.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Tpay Krajowy Integrator Płatności S.A. <https://tpay.com/>
  4.  *
  5.  * @author    Jakub Medyński <jme@crehler.com>
  6.  * @support   Tpay <pt@tpay.com>
  7.  * @created   05 maj 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Tpay\ShopwarePayment\Subscriber;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextSwitchEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Tpay\ShopwarePayment\Component\TpayPayment\BankList\TpayBankListInterface;
  18. use Tpay\ShopwarePayment\Component\TpayPayment\BankList\TpayBankStruct;
  19. use Tpay\ShopwarePayment\TpayShopwarePayment;
  20. class SalesChannelContextSwitch implements EventSubscriberInterface
  21. {
  22.     /** @var TpayBankListInterface */
  23.     private $bankListService;
  24.     /** @var EntityRepositoryInterface */
  25.     private $customerRepository;
  26.     /** @var RequestStack */
  27.     private $requestStack;
  28.     public function __construct(TpayBankListInterface $bankListServiceEntityRepositoryInterface $customerRepositoryRequestStack $requestStack)
  29.     {
  30.         $this->bankListService $bankListService;
  31.         $this->customerRepository $customerRepository;
  32.         $this->requestStack $requestStack;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             SalesChannelContextSwitchEvent::class => 'onSalesChannelContextSwitch'
  38.         ];
  39.     }
  40.     public function onSalesChannelContextSwitch(SalesChannelContextSwitchEvent $event): void
  41.     {
  42.         $context $event->getContext();
  43.         $customer $event->getSalesChannelContext()->getCustomer();
  44.         $selectedBankId $event->getRequestDataBag()->getInt('tpayBank');
  45.         if (empty($selectedBankId)) {
  46.             $selectedBankId = (int) $this->requestStack->getCurrentRequest()->get('tpayBank');
  47.         }
  48.         if ($customer === null || empty($selectedBankId)) {
  49.             return;
  50.         }
  51.         $bank = new TpayBankStruct(
  52.             $this->bankListService->getBankList($event->getSalesChannelContext())
  53.                 ->offsetGet($selectedBankId)
  54.         );
  55.         $this->customerRepository->update([
  56.             [
  57.                 'id' => $customer->getId(),
  58.                 'customFields' => [TpayShopwarePayment::CUSTOMER_CUSTOM_FIELDS_TPAY_SELECTED_BANK => $bank->jsonSerialize()]
  59.             ]
  60.         ], $context);
  61.     }
  62. }