vendor/ibexa/fieldtype-page/src/lib/Event/Subscriber/CachePurge/ContentEventsSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\FieldTypePage\Event\Subscriber\CachePurge;
  8. use Ibexa\Contracts\Core\Persistence\Content\Location\Handler as LocationHandler;
  9. use Ibexa\Contracts\Core\Persistence\URL\Handler as UrlHandler;
  10. use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
  11. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  12. use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
  13. use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
  14. use Ibexa\Contracts\HttpCache\PurgeClient\PurgeClientInterface;
  15. use Ibexa\FieldTypePage\FieldType\LandingPage\Type;
  16. use Ibexa\HttpCache\EventSubscriber\CachePurge\AbstractSubscriber;
  17. class ContentEventsSubscriber extends AbstractSubscriber
  18. {
  19. /** @var bool */
  20. private $isTranslationAware;
  21. public function __construct(
  22. PurgeClientInterface $purgeClient,
  23. LocationHandler $locationHandler,
  24. UrlHandler $urlHandler,
  25. bool $isTranslationAware
  26. ) {
  27. parent::__construct($purgeClient, $locationHandler, $urlHandler);
  28. $this->isTranslationAware = $isTranslationAware;
  29. }
  30. public static function getSubscribedEvents(): array
  31. {
  32. return [
  33. PublishVersionEvent::class => 'onPublishVersionEvent',
  34. ];
  35. }
  36. public function onPublishVersionEvent(PublishVersionEvent $event): void
  37. {
  38. $content = $event->getContent();
  39. $contentType = $content->getContentType();
  40. if ($this->isTranslationAware && $this->isLandingPage($contentType)) {
  41. $tags = [ContentTagInterface::CONTENT_PREFIX . $content->contentInfo->id];
  42. $this->purgeClient->purge($tags);
  43. }
  44. }
  45. private function isLandingPage(ContentType $contentType): bool
  46. {
  47. return $contentType->getFieldDefinitions()->any(static function (FieldDefinition $fieldDefinition): bool {
  48. return $fieldDefinition->fieldTypeIdentifier === Type::IDENTIFIER;
  49. });
  50. }
  51. }