src/Doctrine/ExclusionsFilter.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Doctrine;
  3. use App\Annotation\Exclusions;
  4. use App\Entity\Company;
  5. use App\Entity\User;
  6. use App\Repository\Model\IExclusionRepository;
  7. use Doctrine\Common\Annotations\AnnotationReader;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\ORM\Mapping\ClassMetadata;
  10. use Doctrine\ORM\Query\Filter\SQLFilter;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  13. class ExclusionsFilter extends SQLFilter
  14. {
  15.     /**
  16.      * @var User
  17.      */
  18.     private $user;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $em;
  23.     /**
  24.      * @var LoggerInterface
  25.      */
  26.     private $logger;
  27.     /**
  28.      * @inheritDoc
  29.      */
  30.     public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
  31.     {
  32.         $result = "";
  33.         $reader = new AnnotationReader();
  34.         $annotation = $reader->getClassAnnotation($targetEntity->getReflectionClass(), Exclusions::class);
  35.         if ($annotation instanceof Exclusions && $this->user instanceof User && $this->user->hasRole(User::ROLE_CLIENT)) {
  36.             // $user = $this->em->getRepository(User::class)->find($this->user->getId());
  37.             $company = $this->user->getClient()->getCompany();
  38.             $repo = $this->em->getRepository($annotation->exclusionsClass);
  39.             if ($repo instanceof IExclusionRepository && $company instanceof Company) {
  40.                 $nameConverter = new CamelCaseToSnakeCaseNameConverter();
  41.                 $name = $nameConverter->normalize($targetEntity->getReflectionClass()->getShortName() . '_id');
  42.                 $class = $nameConverter->normalize((new \ReflectionClass($annotation->exclusionsClass))->getShortName());
  43.                 $result = "$targetTableAlias.id NOT IN((SELECT x0_.$name FROM $class AS x0_ WHERE x0_.company_id = {$company->getId()}))";
  44.             }
  45.         }
  46.         return $result;
  47.     }
  48.     /**
  49.      * @return mixed
  50.      */
  51.     public function getUser()
  52.     {
  53.         return $this->user;
  54.     }
  55.     /**
  56.      * @param mixed $user
  57.      * @return ExclusionsFilter
  58.      */
  59.     public function setUser($user): self
  60.     {
  61.         $this->user = $user;
  62.         return $this;
  63.     }
  64.     /**
  65.      * @param EntityManagerInterface $em
  66.      * @return $this
  67.      */
  68.     public function setEntityManager(EntityManagerInterface $em): self
  69.     {
  70.         $this->em = $em;
  71.         return $this;
  72.     }
  73.     /**
  74.      * @param mixed $logger
  75.      * @return ExclusionsFilter
  76.      */
  77.     public function setLogger($logger): self
  78.     {
  79.         $this->logger = $logger;
  80.         return $this;
  81.     }
  82.     /**
  83.      * @return mixed
  84.      */
  85.     public function getLogger()
  86.     {
  87.         return $this->logger;
  88.     }
  89. }