<?php declare(strict_types=1);
namespace ProcB2BPasswordRecovery\Core\Checkout\Contact\Subscriber;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContactChangePasswordSubscriber implements EventSubscriberInterface
{
/**
* @var Connection
*/
private Connection $connection;
/**
* @var SystemConfigService
*/
private SystemConfigService $systemConfigService;
public function __construct(Connection $connection, SystemConfigService $systemConfigService)
{
$this->connection = $connection;
$this->systemConfigService = $systemConfigService;
}
/**
* @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
*/
public static function getSubscribedEvents()
{
return [
CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
];
}
/**
* @throws Exception
*/
public function onCustomerWritten(EntityWrittenEvent $event): void
{
if ($this->systemConfigService->get('ProcB2BPasswordRecovery.config.b2bContactPasswordRecovery')) {
$payloads = $event->getPayloads();
foreach ($payloads as $payload) {
if (!empty($payload['password'])) {
$this->changeContactPassword($payload['id'], $payload['password']);
}
}
}
}
/**
* @throws Exception
*/
private function changeContactPassword(string $customerId, string $password): void
{
$this->connection->executeUpdate(
'UPDATE `b2b_debtor_contact` SET `password` = :password WHERE email = (SELECT email FROM customer WHERE id = :id)',
[
'id' => Uuid::fromHexToBytes($customerId),
'password' => $password,
]
);
}
}