<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Drops the Contao cron jobs this application does not run.
 *
 * Timing is the whole point: Contao's AddCronJobsPass reads the
 * "contao.cronjob" tags and gives contao.cron an addCronJob() call holding a
 * Reference to each service. Remove a definition after that has happened and
 * the reference outlives it, which fails the container at compile time with
 * "contao.cron has a dependency on a non-existent service". So this runs first
 * — see FerienpassCmsBundle::build() for the priority — and Contao then never
 * sees the tags at all.
 */
class RemoveCronJobsPass implements CompilerPassInterface
{
    private const CRON_JOBS = [
        // Preview links are a Contao back end feature we do not expose.
        'contao.cron.purge_preview_links',
        // Registration happens through our own participant flow.
        'contao.cron.purge_registrations',
    ];

    public function process(ContainerBuilder $container): void
    {
        foreach (self::CRON_JOBS as $serviceId) {
            if ($container->hasDefinition($serviceId)) {
                $container->removeDefinition($serviceId);
            }
        }
    }
}
