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

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