<?php
/**
 * ProductCategoriesCacheInvalidationHook class.
 *
 * Clears core product categories cache when categories are created, updated, or deleted.
 *
 * @package Ilabs\Firevert\Core\Hooks\Admin
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Hooks\Admin;

use Ilabs\Firevert\Core\Api\Controller\ProductCategoriesController;
use Ilabs\Firevert\Core\Repository\Cache\CacheRepository;

/**
 * Handles cache invalidation on product category create, edit, or delete.
 */
final class ProductCategoriesCacheInvalidationHook {

	/**
	 * Registers cache invalidation hooks.
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'create_product_cat', array( $this, 'invalidate_cache' ), 20, 2 );
		add_action( 'edited_product_cat', array( $this, 'invalidate_cache' ), 20, 2 );
		add_action( 'delete_product_cat', array( $this, 'invalidate_cache' ), 20, 3 );
	}

	/**
	 * Invalidates cache when a product category is created, edited, or deleted.
	 *
	 * @return void
	 */
	public function invalidate_cache(): void {
		/**
		 * Get from DI container.
		 *
		 * @var CacheRepository $cache
		 */
		$cache = firevert_container()->get( 'cache_repository' );

		$cache->flush_group( ProductCategoriesController::CATEGORIES_CACHE_GROUP );
	}
}
