<?php


namespace AutoAdmin\Bundle\Seo\EventListener;


use AutoAdmin\Bundle\Content\Event\PostContentMetadataEvent;
use AutoAdmin\Bundle\Seo\EventListener\Traits\IsNotFrontUrlTrait;
use AutoAdmin\Bundle\Seo\Schema\SchemaBuilderManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SchemaBuildSubscriber implements EventSubscriberInterface
{
    use IsNotFrontUrlTrait;

    private SchemaBuilderManager $schemaManager;

    public function __construct(SchemaBuilderManager $schemaManager)
    {
        $this->schemaManager = $schemaManager;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            PostContentMetadataEvent::class => ['postContentMetadataEvent',  -10],
        ];
    }

    public function postContentMetadataEvent(PostContentMetadataEvent $event): void
    {
        if ($this->isNotFrontUrl($event->getRequest())) {
            return;
        }

        $contentContext = $event->getContentContext();
        $this->schemaManager->build($contentContext->getSeoPage()->getSchemaStack());
    }
}
