src/EventListener/CustomerEditedListener.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Application\DTO\A4BDataCommunication\A4BDataCommunicationCustomerDTO;
  5. use App\Application\DTO\Hubspot\HubspotCustomerDTO;
  6. use App\Application\Message\A4BDataCommunicationCustomerMessage;
  7. use App\Application\Message\HubspotCustomerMessage;
  8. use App\Event\CustomerEditedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use App\Client\ThePowerMBA\Service\UserService as ThePowerMBAUserService;
  11. use App\Service\Customer\ThePowerMBAService;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. use function Sentry\captureException;
  14. class CustomerEditedListener implements EventSubscriberInterface
  15. {
  16.     private ThePowerMBAUserService $thePowerMBAUserService;
  17.     private ThePowerMBAService $thePowerMBAService;
  18.     private MessageBusInterface $bus;
  19.     public function __construct(
  20.         ThePowerMBAUserService $thePowerMBAUserService,
  21.         ThePowerMBAService $thePowerMBAService,
  22.         MessageBusInterface $bus
  23.     ) {
  24.         $this->thePowerMBAUserService $thePowerMBAUserService;
  25.         $this->thePowerMBAService $thePowerMBAService;
  26.         $this->bus $bus;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [CustomerEditedEvent::class => 'onChange'];
  31.     }
  32.     public function onChange(CustomerEditedEvent $event): void
  33.     {
  34.         $customer $event->getCustomer();
  35.         $this->bus->dispatch(new A4BDataCommunicationCustomerMessage(new A4BDataCommunicationCustomerDTO($customer->getId())));
  36.         $this->bus->dispatch(new HubspotCustomerMessage(new HubspotCustomerDTO($customer->getId())));
  37.         if (!$customer->getThePowerMbaUserId()) {
  38.             return;
  39.         }
  40.         try {
  41.             $userResponse $this->thePowerMBAUserService->getById($customer->getThePowerMbaUserId());
  42.         } catch (\Throwable $exception) {
  43.             captureException($exception);
  44.             return;
  45.         }
  46.         if (
  47.             $customer->getEmail() === $userResponse->getEmail()
  48.             && $customer->getName() === $userResponse->getFirstName()
  49.             && $customer->getSurnames() === $userResponse->getLastName()
  50.             && $customer->getLanguage()->getIsoCode() === $userResponse->getLanguage()
  51.         ) {
  52.             return;
  53.         }
  54.         $this->thePowerMBAService->updateUserParameters($customer$userResponse->getPrograms());
  55.     }
  56. }