src/Event/FlyOutNavigationSubscriber.php line 44
<?php
namespace App\Event;
use App\Service\Navigation\FlyOutNavigationService;
use Sulu\Bundle\PageBundle\Domain\Event\PagePublishedEvent;
use Sulu\Bundle\PageBundle\Domain\Event\PageUnpublishedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
class FlyOutNavigationSubscriber implements EventSubscriberInterface
{
private FlyOutNavigationService $flyOutNavigationService;
private string $flyOutNavigationDir;
private string $flyOutNavigationPath;
public function __construct(FlyOutNavigationService $flyOutNavigationService, string $projectDir)
{
$this->flyOutNavigationService = $flyOutNavigationService;
$this->flyOutNavigationDir = $projectDir . '/navigation';
$this->flyOutNavigationPath = $this->flyOutNavigationDir . '/flyOutNavigation.json';
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
PagePublishedEvent::class => 'createFlyOutNavigation',
PageUnpublishedEvent::class => 'createFlyOutNavigation'
];
}
/**
* Everytime a page in Backend is published, the fly-out navigation JSON will be updated.
*
* @param PagePublishedEvent|PageUnpublishedEvent $event
* @return void
*/
public function createFlyOutNavigation(PagePublishedEvent|PageUnpublishedEvent $event): void
{
$navigation = $this->flyOutNavigationService->getContentFromDB();
$content = json_encode($navigation);
$filesystem = new Filesystem();
if (!$filesystem->exists($this->flyOutNavigationDir)) {
$filesystem->mkdir($this->flyOutNavigationDir);
}
file_put_contents($this->flyOutNavigationPath, $content);
if (!file_exists($this->flyOutNavigationPath)) {
throw new FileNotFoundException('Can not create JSON file for Fly-Out-Navigation!');
}
}
}