custom/plugins/SwagAmazonPay/src/Core/EventListeners/CustomerRecoverSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\AmazonPay\Core\EventListeners;
  8. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  11. use Swag\AmazonPay\Components\Account\AmazonPayAccountServiceInterfaceV2;
  12. use Swag\AmazonPay\Core\Checkout\Customer\Exception\AmazonCustomerRecoveryException;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Validator\Constraints\Callback;
  15. /**
  16.  * This class is used to prevent customer password recovery creation for Amazon Customers.
  17.  * The AmazonCustomerRecoveryException is caught in Swag\AmazonPay\Storefront\Controller\AmazonAuthController::generateAccountRecovery
  18.  */
  19. class CustomerRecoverSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var AmazonPayAccountServiceInterfaceV2
  23.      */
  24.     private $accountService;
  25.     /**
  26.      * Annotated as object because 6.4 differs from older shopware versions.
  27.      * In 6.4 there is a abstract factory, a cached implementation and an uncached one.
  28.      *
  29.      * @var object
  30.      */
  31.     private $salesChannelContextFactory;
  32.     public function __construct(
  33.         AmazonPayAccountServiceInterfaceV2 $accountService,
  34.         object $salesChannelContextFactory
  35.     ) {
  36.         $this->accountService $accountService;
  37.         $this->salesChannelContextFactory $salesChannelContextFactory;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             'framework.validation.customer.email.recover' => 'onCustomerEmailRecover',
  43.         ];
  44.     }
  45.     /**
  46.      * @throws AmazonCustomerRecoveryException
  47.      */
  48.     public function onCustomerEmailRecover(BuildValidationEvent $event): void
  49.     {
  50.         $contextSource $event->getContext()->getSource();
  51.         if (!$contextSource instanceof SalesChannelApiSource) {
  52.             return;
  53.         }
  54.         if (!\method_exists($this->salesChannelContextFactory'create')) {
  55.             return;
  56.         }
  57.         $salesChannelContext $this->salesChannelContextFactory->create(Uuid::randomHex(), $contextSource->getSalesChannelId());
  58.         $event->getDefinition()->add(
  59.             'email',
  60.             new Callback(
  61.                 function (string $email) use ($salesChannelContext): void {
  62.                     $emailBoundToAmazonAccount = (bool) $this->accountService->getAmazonCustomerIdByEmail(
  63.                         $email,
  64.                         $salesChannelContext
  65.                     );
  66.                     if (!$emailBoundToAmazonAccount) {
  67.                         return;
  68.                     }
  69.                     throw new AmazonCustomerRecoveryException();
  70.                 }
  71.             )
  72.         );
  73.     }
  74. }