<?php 
 
namespace App\EventSubscriber; 
 
use ApiPlatform\Symfony\EventListener\EventPriorities; 
use App\Entity\Document; 
use App\Evaluation\EvaluationPrepare; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpKernel\Event\ViewEvent; 
use Symfony\Component\HttpKernel\KernelEvents; 
use Symfony\Component\Security\Core\Security; 
 
final class DocumentSubscriber implements EventSubscriberInterface 
{ 
    public function __construct(private EvaluationPrepare $evaluationPrepare, private Security $security) 
    { 
    } 
 
    public static function getSubscribedEvents() 
    { 
        return [ 
            KernelEvents::VIEW => ['prepareEvaluations', EventPriorities::POST_WRITE], 
        ]; 
    } 
 
    public function prepareEvaluations(ViewEvent $event): void 
    { 
        $document = $event->getControllerResult(); 
        $method = $event->getRequest()->getMethod(); 
        if ((!$document instanceof Document)) { 
            return; 
        } 
 
        $this->evaluationPrepare->prepare($document, $this->security->getUser()); 
    } 
}