<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Event\CustomerEditedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Client\ThePowerMBA\Service\UserService as ThePowerMBAUserService;
use App\Service\Customer\ThePowerMBAService;
use function Sentry\captureException;
class CustomerEditedListener implements EventSubscriberInterface
{
private ThePowerMBAUserService $thePowerMBAUserService;
private ThePowerMBAService $thePowerMBAService;
public function __construct(
ThePowerMBAUserService $thePowerMBAUserService,
ThePowerMBAService $thePowerMBAService
) {
$this->thePowerMBAUserService = $thePowerMBAUserService;
$this->thePowerMBAService = $thePowerMBAService;
}
public static function getSubscribedEvents(): array
{
return [CustomerEditedEvent::class => 'onChange'];
}
public function onChange(CustomerEditedEvent $event): void
{
$customer = $event->getCustomer();
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());
}
}