custom/plugins/ProcB2BPasswordRecovery/src/Core/Checkout/Contact/Subscriber/ContactChangePasswordSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProcB2BPasswordRecovery\Core\Checkout\Contact\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ContactChangePasswordSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var Connection
  14.      */
  15.     private Connection $connection;
  16.     /**
  17.      * @var SystemConfigService
  18.      */
  19.     private SystemConfigService $systemConfigService;
  20.     public function __construct(Connection $connectionSystemConfigService $systemConfigService)
  21.     {
  22.         $this->connection $connection;
  23.         $this->systemConfigService $systemConfigService;
  24.     }
  25.     /**
  26.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  32.         ];
  33.     }
  34.     /**
  35.      * @throws Exception
  36.      */
  37.     public function onCustomerWritten(EntityWrittenEvent $event): void
  38.     {
  39.         if ($this->systemConfigService->get('ProcB2BPasswordRecovery.config.b2bContactPasswordRecovery')) {
  40.             $payloads $event->getPayloads();
  41.             foreach ($payloads as $payload) {
  42.                 if (!empty($payload['password'])) {
  43.                     $this->changeContactPassword($payload['id'], $payload['password']);
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     /**
  49.      * @throws Exception
  50.      */
  51.     private function changeContactPassword(string $customerIdstring $password): void
  52.     {
  53.         $this->connection->executeUpdate(
  54.             'UPDATE `b2b_debtor_contact` SET `password` = :password WHERE email = (SELECT email FROM customer WHERE id = :id)',
  55.             [
  56.                 'id' => Uuid::fromHexToBytes($customerId),
  57.                 'password' => $password,
  58.             ]
  59.         );
  60.     }
  61. }