src/EventListener/Strava/WebhookAthleteSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Strava;
  3. use App\Entity\SocialToken;
  4. use App\Entity\User;
  5. use App\Repository\SocialTokenRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use NuBox\Strava\Api\Event\StravaApiEvents;
  8. use NuBox\Strava\Api\Event\Webhook\ActivityDeletedEvent;
  9. use NuBox\Strava\Api\Event\Webhook\WebhookEventInterface;
  10. use NuBox\Strava\Api\Service\StravaApi;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. final class WebhookAthleteSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private readonly SocialTokenRepository $socialTokenRepository,
  16.         private readonly EntityManagerInterface $entityManager,
  17.     ) {
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             StravaApiEvents::WEBHOOK_ATHLETE_DELETED => 'onAthleteDeleted',
  23.         ];
  24.     }
  25.     public function onAthleteDeleted(ActivityDeletedEvent $event): void
  26.     {
  27.         $socialToken $this->getSocialToken($event);
  28.         $this->entityManager->remove($socialToken);
  29.         $this->entityManager->flush();
  30.     }
  31.     private function getSocialToken(WebhookEventInterface $event): SocialToken
  32.     {
  33.         $socialUserId $event->getWebhook()->getOwnerId();
  34.         $socialToken $this->socialTokenRepository->findOneBy([
  35.             'type' => StravaApi::TYPE,
  36.             'socialUserId' => $socialUserId,
  37.         ]);
  38.         assert($socialToken instanceof SocialToken);
  39.         return $socialToken;
  40.     }
  41. }