<?php
/**
 * Click analytics retention purger.
 *
 * @package Affilun\Application\Analytics
 */

declare(strict_types=1);

namespace Affilun\Application\Analytics;

use Affilun\Domain\Clock;
use Affilun\Infrastructure\Persistence\ClickEventRepository;

/**
 * Single-tick click analytics retention purge driver.
 */
final class ClickRetentionPurger {

	/**
	 * Click repository.
	 */
	private ClickEventRepository $events;

	/**
	 * Retention policy.
	 */
	private ClickRetentionPolicy $policy;

	/**
	 * Clock.
	 */
	private Clock $clock;

	/**
	 * Construct the purger.
	 *
	 * @param ClickEventRepository $events Click repository.
	 * @param ClickRetentionPolicy $policy Retention policy.
	 * @param Clock                $clock  Clock.
	 */
	public function __construct( ClickEventRepository $events, ClickRetentionPolicy $policy, Clock $clock ) {
		$this->events = $events;
		$this->policy = $policy;
		$this->clock  = $clock;
	}

	/**
	 * Execute one purge tick.
	 */
	public function purge(): int {
		return $this->events->deleteOlderThan( $this->policy->cutoffFrom( $this->clock->now() ) );
	}
}
