src/FMT/Application/ParamConverter/EnabledUserConverter.php line 84

Open in your IDE?
  1. <?php
  2. namespace FMT\Application\ParamConverter;
  3. use FMT\Data\Entity\User;
  4. use FMT\Data\Entity\UserProfile;
  5. use FMT\Domain\Service\UserManagerInterface;
  6. use FMT\Application\ParamConverter\Traits\InheritanceTrait;
  7. use FOS\UserBundle\Doctrine\UserManager;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  9. use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. /**
  14.  * Class EnabledUserConverter
  15.  * @package FMT\Application\ParamConverter
  16.  */
  17. class EnabledUserConverter implements ParamConverterInterface
  18. {
  19.     use InheritanceTrait;
  20.     /**
  21.      * @var UserManagerInterface
  22.      */
  23.     private $userManager;
  24.     /**
  25.      * @var User|null
  26.      */
  27.     private $currentUser;
  28.     /**
  29.      * EnabledUserConverter constructor.
  30.      * @param UserManager $manager
  31.      * @param TokenStorageInterface $tokenStorage
  32.      */
  33.     public function __construct(UserManager $managerTokenStorageInterface $tokenStorage)
  34.     {
  35.         $this->userManager $manager;
  36.         $token $tokenStorage->getToken();
  37.         $this->currentUser $token $token->getUser() : null;
  38.     }
  39.     /**
  40.      * @inheritDoc
  41.      */
  42.     public function apply(Request $requestParamConverter $configuration)
  43.     {
  44.         $name $configuration->getName();
  45.         $id $request->attributes->get('id');
  46.         if (!$id) {
  47.             return false;
  48.         }
  49.         $user $this->userManager->findUserBy([
  50.             'id' => $id,
  51.             'enabled' => true,
  52.         ]);
  53.         if (!$this->isUserInstance($user)) {
  54.             throw new NotFoundHttpException();
  55.         }
  56.         $options $configuration->getOptions();
  57.         if (!array_key_exists('invitation'$options) || !$options['invitation']) {
  58.             switch (true) {
  59.                 case $user->getProfile()->getVisible() === UserProfile::VISIBILITY_NON:
  60.                 case $user->getProfile()->getVisible() === UserProfile::VISIBILITY_REGISTRED &&
  61.                     !$this->isUserInstance($this->currentUser):
  62.                     throw new NotFoundHttpException();
  63.             }
  64.         }
  65.         $request->attributes->set($name$user);
  66.         return true;
  67.     }
  68.     /**
  69.      * @inheritDoc
  70.      */
  71.     public function supports(ParamConverter $configuration)
  72.     {
  73.         return $this->isInstanceOf($configuration->getClass(), User::class);
  74.     }
  75.     /**
  76.      * @param $user
  77.      * @return bool
  78.      */
  79.     private function isUserInstance($user)
  80.     {
  81.         return $user instanceof User;
  82.     }
  83. }