<?php
namespace AppBundle\EventListener;
use AppBundle\Core\UserPermissionAware;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\Core\MVC\Symfony\Event\ContentCacheClearEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CacheListener implements EventSubscriberInterface
{
use UserPermissionAware;
/**
* @var LocationService
*/
protected $locationService;
/**
* @var ContentService
*/
protected $contentService;
public function __construct(
ContentService $contentService,
LocationService $locationService
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
}
public static function getSubscribedEvents()
{
return [MVCEvents::CACHE_CLEAR_CONTENT => ['onContentCacheClear', 255]];
}
public function onContentCacheClear(ContentCacheClearEvent $event)
{
$contentInfo = $event->getContentInfo();
$locations = $this->locationService->loadLocations($contentInfo);
$reverseRelations = $this->contentService->loadReverseRelations($contentInfo);
foreach ($reverseRelations as $reverseRelation) {
foreach ($this->locationService->loadLocations($reverseRelation->getSourceContentInfo()) as $relatedLocation) {
$locations[] = $relatedLocation;
}
}
$this->addParentsLocationsToClear($event, $locations);
}
private function addParentsLocationsToClear(ContentCacheClearEvent $event, $locations)
{
foreach ($locations as $location) {
$parents = array_slice(array_reverse(explode("/", $location->pathString)), 2, 3);
foreach ($parents as $parent) {
if ($parent > 1) {
$parentLocation = $this->locationService->loadLocation($parent);
$event->addLocationToClear($parentLocation);
}
}
}
}
}