<?php
declare(strict_types=1);

namespace Autogedal\BlackFriday\Model;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\Event\ManagerInterface;

/** Drops the cached product data and the landing page from the full page cache (built-in or Varnish). */
class CacheCleaner
{
    public function __construct(
        private readonly CacheInterface $cache,
        private readonly ManagerInterface $eventManager,
        private readonly PageIdentity $identity,
        private readonly TeaserIdentity $teaserIdentity
    ) {
    }

    public function clean(): void
    {
        $this->cache->clean([PageIdentity::CACHE_TAG]);
        $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->identity]);
    }

    /** All the pages with the site-wide teasers (in practice the whole page cache): only on teaser settings changes. */
    public function cleanTeaser(): void
    {
        $this->cache->clean([TeaserIdentity::CACHE_TAG]);
        $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->teaserIdentity]);
    }
}
