vendor/gedmo/doctrine-extensions/src/SoftDeleteable/Filter/SoftDeleteableFilter.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Doctrine Behavioral Extensions package.
  4.  * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Gedmo\SoftDeleteable\Filter;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\ORM\Mapping\ClassMetadata;
  11. use Doctrine\ORM\Query\Filter\SQLFilter;
  12. use Gedmo\SoftDeleteable\SoftDeleteableListener;
  13. /**
  14.  * The SoftDeleteableFilter adds the condition necessary to
  15.  * filter entities which were deleted "softly"
  16.  *
  17.  * @author Gustavo Falco <comfortablynumb84@gmail.com>
  18.  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  19.  * @author Patrik Votoček <patrik@votocek.cz>
  20.  */
  21. class SoftDeleteableFilter extends SQLFilter
  22. {
  23.     /**
  24.      * @var SoftDeleteableListener
  25.      */
  26.     protected $listener;
  27.     /**
  28.      * @var EntityManagerInterface
  29.      */
  30.     protected $entityManager;
  31.     /**
  32.      * @var array<string, bool>
  33.      * @phpstan-var array<class-string, bool>
  34.      */
  35.     protected $disabled = [];
  36.     /**
  37.      * @param string $targetTableAlias
  38.      *
  39.      * @return string
  40.      *
  41.      * @throws \Doctrine\DBAL\Exception
  42.      */
  43.     public function addFilterConstraint(ClassMetadata $targetEntity$targetTableAlias)
  44.     {
  45.         $class $targetEntity->getName();
  46.         if (true === ($this->disabled[$class] ?? false)) {
  47.             return '';
  48.         }
  49.         if (true === ($this->disabled[$targetEntity->rootEntityName] ?? false)) {
  50.             return '';
  51.         }
  52.         $config $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
  53.         if (!isset($config['softDeleteable']) || !$config['softDeleteable']) {
  54.             return '';
  55.         }
  56.         $platform $this->getConnection()->getDatabasePlatform();
  57.         $quoteStrategy $this->getEntityManager()->getConfiguration()->getQuoteStrategy();
  58.         $column $quoteStrategy->getColumnName($config['fieldName'], $targetEntity$platform);
  59.         $addCondSql $platform->getIsNullExpression($targetTableAlias.'.'.$column);
  60.         if (isset($config['timeAware']) && $config['timeAware']) {
  61.             $addCondSql "({$addCondSql} OR {$targetTableAlias}.{$column} > {$platform->getCurrentTimestampSQL()})";
  62.         }
  63.         return $addCondSql;
  64.     }
  65.     /**
  66.      * @param string $class
  67.      *
  68.      * @phpstan-param class-string $class
  69.      *
  70.      * @return void
  71.      */
  72.     public function disableForEntity($class)
  73.     {
  74.         $this->disabled[$class] = true;
  75.         // Make sure the hash (@see SQLFilter::__toString()) for this filter will be changed to invalidate the query cache.
  76.         $this->setParameter(sprintf('disabled_%s'$class), true);
  77.     }
  78.     /**
  79.      * @param string $class
  80.      *
  81.      * @phpstan-param class-string $class
  82.      *
  83.      * @return void
  84.      */
  85.     public function enableForEntity($class)
  86.     {
  87.         $this->disabled[$class] = false;
  88.         // Make sure the hash (@see SQLFilter::__toString()) for this filter will be changed to invalidate the query cache.
  89.         $this->setParameter(sprintf('disabled_%s'$class), false);
  90.     }
  91.     /**
  92.      * @return SoftDeleteableListener
  93.      *
  94.      * @throws \RuntimeException
  95.      */
  96.     protected function getListener()
  97.     {
  98.         if (null === $this->listener) {
  99.             $em $this->getEntityManager();
  100.             $evm $em->getEventManager();
  101.             foreach ($evm->getListeners() as $listeners) {
  102.                 foreach ($listeners as $listener) {
  103.                     if ($listener instanceof SoftDeleteableListener) {
  104.                         $this->listener $listener;
  105.                         break 2;
  106.                     }
  107.                 }
  108.             }
  109.             if (null === $this->listener) {
  110.                 throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
  111.             }
  112.         }
  113.         return $this->listener;
  114.     }
  115.     /**
  116.      * @return EntityManagerInterface
  117.      */
  118.     protected function getEntityManager()
  119.     {
  120.         if (null === $this->entityManager) {
  121.             $getEntityManager \Closure::bind(function (): EntityManagerInterface {
  122.                 return $this->em;
  123.             }, $thisparent::class);
  124.             $this->entityManager $getEntityManager();
  125.         }
  126.         return $this->entityManager;
  127.     }
  128. }