<?php
/**
 * REST controller registry.
 *
 * @package RewindBrevoFormBridge
 */

namespace RewindBrevoFormBridge\Rest;

defined( 'ABSPATH' ) || exit;

/**
 * Owns the map of rbfb/v1 controllers and registers their routes on
 * rest_api_init. Nothing else calls register_rest_route().
 *
 * @since 0.4.0
 */
class ApiRegistry {

	/**
	 * Controllers to register.
	 *
	 * @var BaseController[]
	 */
	private array $controllers;

	/**
	 * Constructor.
	 *
	 * @since 0.4.0
	 * @param BaseController[] $controllers Controllers keyed by route base.
	 */
	public function __construct( array $controllers ) {
		$this->controllers = $controllers;
	}

	/**
	 * Attach the REST hook.
	 *
	 * @since 0.4.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
	}

	/**
	 * Register every controller's routes.
	 *
	 * @since 0.4.0
	 * @return void
	 */
	public function register_routes(): void {
		foreach ( $this->controllers as $controller ) {
			$controller->register_routes();
		}
	}
}
