src/EventListener/CustomerEditedListener.php line 32

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