<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Application\DTO\A4BDataCommunication\A4BDataCommunicationCustomerDTO;
use App\Application\DTO\Hubspot\HubspotCustomerDTO;
use App\Application\Message\A4BDataCommunicationCustomerMessage;
use App\Application\Message\HubspotCustomerMessage;
use App\Event\CustomerEditedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Client\ThePowerMBA\Service\UserService as ThePowerMBAUserService;
use App\Service\Customer\ThePowerMBAService;
use Symfony\Component\Messenger\MessageBusInterface;
use function Sentry\captureException;
class CustomerEditedListener implements EventSubscriberInterface
{
private ThePowerMBAUserService $thePowerMBAUserService;
private ThePowerMBAService $thePowerMBAService;
private MessageBusInterface $bus;
public function __construct(
ThePowerMBAUserService $thePowerMBAUserService,
ThePowerMBAService $thePowerMBAService,
MessageBusInterface $bus
) {
$this->thePowerMBAUserService = $thePowerMBAUserService;
$this->thePowerMBAService = $thePowerMBAService;
$this->bus = $bus;
}
public static function getSubscribedEvents(): array
{
return [CustomerEditedEvent::class => 'onChange'];
}
public function onChange(CustomerEditedEvent $event): void
{
$customer = $event->getCustomer();
$this->bus->dispatch(new A4BDataCommunicationCustomerMessage(new A4BDataCommunicationCustomerDTO($customer->getId())));
$this->bus->dispatch(new HubspotCustomerMessage(new HubspotCustomerDTO($customer->getId())));
if (!$customer->getThePowerMbaUserId()) {
return;
}
try {
$userResponse = $this->thePowerMBAUserService->getById($customer->getThePowerMbaUserId());
} catch (\Throwable $exception) {
captureException($exception);
return;
}
if (
$customer->getEmail() === $userResponse->getEmail()
&& $customer->getName() === $userResponse->getFirstName()
&& $customer->getSurnames() === $userResponse->getLastName()
&& $customer->getLanguage()->getIsoCode() === $userResponse->getLanguage()
) {
return;
}
$this->thePowerMBAService->updateUserParameters($customer, $userResponse->getPrograms());
}
}