src/Social/FrontendBundle/EventListener/ViewingAnotherUserListener.php line 87

Open in your IDE?
  1. <?php
  2. namespace Social\FrontendBundle\EventListener;
  3. use Social\UserBundle\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Routing\RouterInterface;
  6. use Social\FrontendBundle\Entity\Relationship;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Social\UserBundle\Repository\UserRepository;
  14. use Social\FrontendBundle\Repository\RelationshipRepository;
  15. use Sentry\Client as SentryClient;
  16. /**
  17.  * Class ViewingAnotherUserListener
  18.  *
  19.  * @package Social\FrontendBundle\EventListener
  20.  */
  21. class ViewingAnotherUserListener implements EventSubscriberInterface
  22. {
  23.     private $sc null;
  24.     /**
  25.      * @var EntityManagerInterface $em
  26.      */
  27.     private $em null;
  28.     /**
  29.      * @var UserRepository $userRepository
  30.      */
  31.     private $userRepository;
  32.     /**
  33.      * @var RelationshipRepository $relationshipRepository
  34.      */
  35.     private $relationshipRepository;
  36.     /**
  37.      * @var RouterInterface $router
  38.      */
  39.     private $router;
  40.     /**
  41.      * @var TokenStorageInterface $tokenStorage
  42.      */
  43.     private $tokenStorage;
  44.     /**
  45.      * @var SentryClient $sentryClient
  46.      */
  47.     private $sentryClient;
  48.     /**
  49.      * ViewingAnotherUserListener constructor.
  50.      *
  51.      * @param TokenStorageInterface  $tokenStorage
  52.      * @param EntityManagerInterface $entityManager
  53.      * @param RouterInterface        $router
  54.      * @param SentryClient           $client
  55.      */
  56.     public function __construct(
  57.         TokenStorageInterface $tokenStorage,
  58.         EntityManagerInterface $entityManager,
  59.         RouterInterface $router,
  60.         SentryClient $client
  61.     ) {
  62.         $this->tokenStorage $tokenStorage;
  63.         $this->em           $entityManager;
  64.         $this->router       $router;
  65.         $this->sentryClient $client;
  66.         $this->userRepository         $this->em->getRepository(User::class);
  67.         $this->relationshipRepository $this->em->getRepository(Relationship::class);
  68.     }
  69.     /**
  70.      * @param GetResponseEvent $event
  71.      *
  72.      * @return GetResponseEvent
  73.      */
  74.     public function onKernelRequest(GetResponseEvent $event): GetResponseEvent
  75.     {
  76.         try {
  77.             if (!$this->tokenStorage->getToken() instanceof TokenInterface) {
  78.                 return $event;
  79.             }
  80.             $user $this->tokenStorage->getToken()->getUser();
  81.             if (!$user instanceof User) {
  82.                 return $event;
  83.             }
  84.             $viewingUser null;
  85.             if (!$route $event->getRequest()->get('_route')) {
  86.                 return $event;
  87.             }
  88.             $request         $event->getRequest();
  89.             $route           $request->get('_route');
  90.             $otherUserRoutes = [
  91.                 'social_frontend_user_about',
  92.                 'social_frontend_user_friends',
  93.                 'social_frontend_user_photo',
  94.                 'social_frontend_user_photos',
  95.                 'social_frontend_user_video',
  96.                 'social_frontend_user_videos',
  97.                 'social_frontend_user_album',
  98.             ];
  99.             $routesWithIds = [
  100.                 'social_frontend_photos_load_more',
  101.                 'social_frontend_videos_load_more',
  102.                 'social_frontend_albums_load_more',
  103.             ];
  104.             if (in_array($route$otherUserRoutes)) {
  105.                 $viewingUser $this->userRepository
  106.                     ->findOneBy(
  107.                         [
  108.                             'username' => $event->getRequest()->get('username'),
  109.                         ]
  110.                     );
  111.                 if (!$viewingUser) {
  112.                     throw new NotFoundHttpException("User not found");
  113.                 }
  114.                 $this->setViewingUserData($user$viewingUser);
  115.             } elseif (in_array($route$routesWithIds)) {
  116.                 $userId $request->get('user_id');
  117.                 $viewingUser $this->userRepository->find($userId);
  118.                 if (!$viewingUser instanceof User) {
  119.                     throw new NotFoundHttpException("User not found");
  120.                 }
  121.                 $this->setViewingUserData($user$viewingUser);
  122.             }
  123.             return $event;
  124.         } catch (\Exception $exception) {
  125.             $this->sentryClient->captureException($exception);
  126.             return $event;
  127.         }
  128.     }
  129.     /**
  130.      * @return array
  131.      */
  132.     public static function getSubscribedEvents(): array
  133.     {
  134.         return [
  135.             KernelEvents::REQUEST => ['onKernelRequest'128],
  136.         ];
  137.     }
  138.     /**
  139.      * @param User $user
  140.      * @param User $viewingUser
  141.      */
  142.     private function setViewingUserData(User $userUser $viewingUser)
  143.     {
  144.         try {
  145.             if ($user->getId() != $viewingUser->getId()) {
  146.                 $user->setViewingUser($viewingUser); // set which entity you are viewing
  147.                 $relObj $this->relationshipRepository->returnRecordBetweenUsers($user$viewingUser);
  148.                 $user->setRelationshipObjectWithUser($viewingUser$relObj);
  149.             }
  150.         } catch (\Exception $exception) {
  151.             $this->sentryClient->captureException($exception);
  152.         }
  153.     }
  154. }