src/EventListener/Strava/TokenSubscriber.php line 73

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\DataObject\StravaTokenInterface;
  8. use NuBox\Strava\Api\Event\StravaApiEvents;
  9. use NuBox\Strava\Api\Event\Token\CreatedEvent;
  10. use NuBox\Strava\Api\Event\Token\InvalidEvent;
  11. use NuBox\Strava\Api\Event\Token\RefreshedEvent;
  12. use NuBox\Strava\Api\Event\Token\RequestEvent;
  13. use NuBox\Strava\Api\Event\Token\RevokedEvent;
  14. use NuBox\Strava\Api\Event\Token\TokenEventInterface;
  15. use NuBox\Strava\Api\Service\StravaApi;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Security\Core\Security;
  18. final class TokenSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private readonly EntityManagerInterface $entityManager,
  22.         private readonly Security $security,
  23.     ) {
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             StravaApiEvents::TOKEN_CREATED => 'onTokenCreated',
  29.             StravaApiEvents::TOKEN_REFRESHED => 'onTokenRefreshed',
  30.             StravaApiEvents::TOKEN_REVOKED => 'onTokenRevoked',
  31.             StravaApiEvents::TOKEN_INVALID => 'onTokenRevoked',
  32.             StravaApiEvents::TOKEN_REQUEST => 'onTokenRequest',
  33.         ];
  34.     }
  35.     public function onTokenCreated(CreatedEvent $event): void
  36.     {
  37.         $token $event->getToken();
  38.         $socialToken = new SocialToken();
  39.         $socialToken->setUser($this->getUserByLoggedUser());
  40.         $socialToken->setType(StravaApi::TYPE);
  41.         $socialToken->setTokenType($token->getTokenType());
  42.         $socialToken->setExpiresAt($token->getExpiresAt());
  43.         $socialToken->setExpiresIn($token->getExpiresIn());
  44.         $socialToken->setRefreshToken($token->getRefreshToken());
  45.         $socialToken->setAccessToken($token->getAccessToken());
  46.         $socialToken->setSocialUserId($token->getAthlete()?->getId() ?? 0);
  47.         $socialToken->setScopes($token->getScopes());
  48.         $this->entityManager->persist($socialToken);
  49.         $this->entityManager->flush();
  50.     }
  51.     public function onTokenRefreshed(RefreshedEvent $event): void
  52.     {
  53.         $token $event->getToken();
  54.         $socialToken $event->getOriginalToken();
  55.         $socialToken->setRefreshToken($token->getRefreshToken());
  56.         $socialToken->setTokenType($token->getTokenType());
  57.         $socialToken->setExpiresAt($token->getExpiresAt());
  58.         $socialToken->setExpiresIn($token->getExpiresIn());
  59.         $socialToken->setAccessToken($token->getAccessToken());
  60.         $this->entityManager->flush();
  61.     }
  62.     public function onTokenRequest(RequestEvent $event): void
  63.     {
  64.         $event->setToken($this->getSocialTokenByLoggedUser());
  65.     }
  66.     public function onTokenRevoked(RevokedEvent|InvalidEvent $event): void
  67.     {
  68.         $this->entityManager->remove($event->getToken());
  69.         $this->entityManager->flush();
  70.     }
  71.     public function getUserByLoggedUser(): User
  72.     {
  73.         $user $this->security->getUser();
  74.         assert($user instanceof User);
  75.         return $user;
  76.     }
  77.     public function getSocialTokenByLoggedUser(): StravaTokenInterface
  78.     {
  79.         return $this->getUserByLoggedUser()->getSocialToken(StravaApi::TYPE);
  80.     }
  81. }