<?php
/**
 * Group Transient
 *
 * Grouped transients that can invalidate their own group independently of other groups
 * or the global cache.
 *
 * @package AmbassadorHub\Cache
 */

namespace AmbassadorHub\Cache;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Grouped transients: can ONLY bump their own group.
 *
 * Subclasses MUST override TRANSIENT_GROUP with a non-empty string.
 */
abstract class GroupTransient extends AbstractTransientBase {

	// Subclasses MUST override to non-empty.
	protected const TRANSIENT_GROUP = '';

	/**
	 * Override to enforce non-empty group.
	 *
	 * @return string Group name.
	 * @throws \LogicException If TRANSIENT_GROUP is empty.
	 */
	protected static function get_group(): string {
		$group = parent::get_group();

		if ( '' === $group ) {
			throw new \LogicException(
				sprintf( '%s must define a non-empty TRANSIENT_GROUP.', static::class )
			);
		}

		return $group;
	}

	/**
	 * Bump the version for this group only.
	 *
	 * Invalidates only transients belonging to this group — not global transients or other groups.
	 *
	 * @return void
	 */
	public static function bump_version(): void {
		$option  = static::get_group_option();
		$version = (int) get_option( $option, 0 );
		update_option( $option, $version + 1 );
	}
}
