src/App/Doctrine/Cache/InvalidateListener.php line 65

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace App\Doctrine\Cache;
  3. use App\Doctrine\Common\Cache\TaggedMemcachedCache;
  4. use App\Entity\DealBid;
  5. use App\Entity\ProjectBid;
  6. use App\Entity\ProposalBid;
  7. use App\Event\CreditsEvent;
  8. use Doctrine\Common\Util\ClassUtils;
  9. use Doctrine\Persistence\Event\LifecycleEventArgs;
  10. use Doctrine\Persistence\ObjectManager;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  13. class InvalidateListener implements EventSubscriberInterface
  14. {
  15.     private TaggedMemcachedCache $taggedCache;
  16.     private bool $enabled true;
  17.     public function __construct(TaggedMemcachedCache $taggedCache)
  18.     {
  19.         $this->taggedCache $taggedCache;
  20.     }
  21.     /**
  22.      * note this listener is auto-removed if we do not use result_cache: service in doctrine config
  23.      *
  24.      * @see \App\DependencyInjection\Compiler\RemoveInvalidateListenerPass
  25.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
  26.      * @param LifecycleEventArgs<ObjectManager> $args
  27.      * @param object $entity
  28.      */
  29.     public function postUpdate($entityLifecycleEventArgs $args): void
  30.     {
  31.         if (!$this->enabled) {
  32.             return;
  33.         }
  34.         $className get_class($entity);
  35.         $entityManager $args->getObjectManager();
  36.         $metadata $entityManager->getClassMetadata($className);
  37.         if (!isset($metadata->cache['region'])) {
  38.             return;
  39.         }
  40.         /** @var string $modelKey */
  41.         $modelKey $metadata->cache['region'];
  42.         $this->taggedCache->removeTag($modelKey);
  43.         if ($modelKey === 'cache_project' || $modelKey === 'cache_deal' || $modelKey === 'cache_shop_video' || $modelKey === 'feed') {
  44.             $this->taggedCache->removeTag('categorization');
  45.         }
  46.     }
  47.     public function setEnabled(bool $enabled): void
  48.     {
  49.         $this->enabled $enabled;
  50.     }
  51.     public function onCredits(CreditsEvent $event): void
  52.     {
  53.         $related $event->getRelatedEntity();
  54.         if (!$related instanceof ProposalBid) {
  55.             return;
  56.         }
  57.         switch (ClassUtils::getClass($related)) {
  58.             case DealBid::class:
  59.                 $modelKey 'cache_deal';
  60.                 break;
  61.             case ProjectBid::class:
  62.                 $modelKey 'cache_project';
  63.                 break;
  64.             default:
  65.                 throw new UnexpectedTypeException($relatedDealBid::class . '|' ProjectBid::class);
  66.         }
  67.         $this->taggedCache->removeTag($modelKey);
  68.     }
  69.     /**
  70.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  71.      */
  72.     public static function getSubscribedEvents(): array
  73.     {
  74.         return [
  75.             CreditsEvent::class => ['onCredits', -255],
  76.         ];
  77.     }
  78. }