<?php
namespace Roothirsch\CoreBundle\Translation\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use Roothirsch\CoreBundle\Translation\Entity\TranslationScope;
use Roothirsch\CoreBundle\Translation\Repository\TranslationScopeRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class TranslationScopeReadSubscriber implements EventSubscriberInterface
{
/**
* @var TranslationScopeRepository
*/
protected $translationScopeRepository;
public function __construct(TranslationScopeRepository $translationScopeRepository)
{
$this->translationScopeRepository = $translationScopeRepository;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['handle', EventPriorities::PRE_READ],
];
}
public function handle(RequestEvent $event)
{
if ($event->getRequest()->attributes->get('_api_resource_class') !== TranslationScope::class) {
return;
}
$translationScope = $this->translationScopeRepository->findOneBy(['namespace' => $event->getRequest()->attributes->get('id')]);
if ($translationScope instanceof TranslationScope) {
$event->getRequest()->attributes->set('id', $translationScope->getId());
}
$translationScope = $this->translationScopeRepository->findOneBy(['pushToken' => $event->getRequest()->attributes->get('id')]);
if ($translationScope instanceof TranslationScope) {
$event->getRequest()->attributes->set('id', $translationScope->getId());
}
}
}