<?php
namespace App\EventSubscriber;
use App\Event\PasswordChangedEvent;
use App\Services\ApiConsumerService;
use App\Services\AsymmetricEncryptionService;
use App\Services\Rabbit\Publisher\UsuarioHermesPublisher;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class PasswordChangedEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private AsymmetricEncryptionService $asymmetricEncryptionService,
private ApiConsumerService $apiConsumerService
) {}
public static function getSubscribedEvents(): array
{
return [PasswordChangedEvent::class => 'onPasswordChanged'];
}
public function onPasswordChanged(PasswordChangedEvent $event): void
{
$response = $this->apiConsumerService->getData(
'usuario-movil', [
'username' => $event->username,
// 'userToken' => $event->userToken, //No sé porqeu pero me falla al enviar este.
'uuid' => $event->uuid
]
);
if (!$response) throw new EntityNotFoundException('HERMES.NOT_FOUND_ENTITY.USUARIO_MOVIL');
$usuarioMovil = json_decode($response)[0];
$this->apiConsumerService->postData('usuario-movil/'.$usuarioMovil->id, json_encode([
'plain_password' => $this->asymmetricEncryptionService->publicEncrypt($event->newPassword)
]));
}
}