<?php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Ajoute X-Robots-Tag sur toutes les réponses (HTML, API, fichiers servis par Symfony).
 */
class NoIndexSubscriber implements EventSubscriberInterface
{
    private const ROBOTS_HEADER = 'noindex, nofollow, noarchive, nosnippet';

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::RESPONSE => ['onKernelResponse', -255],
        ];
    }

    public function onKernelResponse(ResponseEvent $event): void
    {
        if (!$event->isMainRequest()) {
            return;
        }

        $event->getResponse()->headers->set('X-Robots-Tag', self::ROBOTS_HEADER);
    }
}
