<?php
/**
 * Handles cron syncers.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Abstracts\Hub_API\Sync\Cron;

use Northmill_Online_Shared\Abstracts\Hub_API\Sync\Abstract_Syncer;
use Northmill_Online_Shared\Abstracts\Hub_API\Sync\Cron\Models\Cron_Sync_Model;

defined( 'ABSPATH' ) || exit;

/**
 * Class Cron_Syncers
 */
class Cron_Syncers {

	/**
	 * All constructed cron syncers.
	 *
	 * @var Cron_Sync_Model[]
	 */
	private static array $cron_syncers = array();

	/**
	 * Add a new syncer.
	 *
	 * @param string          $key      Syncer key.
	 *                                  This key is used to get the syncer instance.
	 * @param Abstract_Syncer $syncer   Syncer instance.
	 * @param int             $priority Syncer priority.
	 *
	 * @return Cron_Sync_Model Syncer model.
	 */
	public static function add_syncer( string $key, Abstract_Syncer $syncer, int $priority = 10 ): Cron_Sync_Model {
		self::$cron_syncers[ $key ] = new Cron_Sync_Model(
			$priority,
			$syncer,
		);
		return self::$cron_syncers[ $key ];
	}

	/**
	 * Get cron syncer by name.
	 * Flo Light uses this.
	 *
	 * @param string $name Cron syncer name.
	 *
	 * @return Cron_Sync_Model|null
	 */
	public static function get_cron_syncer( string $name ): ?Cron_Sync_Model {
		return self::$cron_syncers[ $name ] ?? null;
	}

	/**
	 * Get all cron syncers.
	 *
	 * @return Cron_Sync_Model[]
	 */
	public static function get_all(): array {
		return self::$cron_syncers;
	}
}
