<?php

namespace EaLab\WP_Deployer\includes\Rest\DeploymentBundle;

use EaLab\WP_Deployer\includes\Rest\RestBase;

class SynchronizeRest extends RestBase {
	static public function getRegisterRestRouteDefinitions(): array {
		$args = array();

		$args[] = array(
			'route' => '/deployment-bundle/synchronize',
			'args'  => array(
				'methods'             => \WP_REST_Server::CREATABLE,
				'callback'            => array( static::class, 'handle' ),
				'permission_callback' => static function () {
					// If the request is from the admin, we allow it.
					return in_array( 'administrator', wp_get_current_user()->roles ?? array(), true );
				},
			),
		);

		return $args;
	}

	static public function handle( \WP_REST_Request $request ): \WP_REST_Response {
		// Synchronization is like import, but with UI adjustments.
		$request->set_param( 'display', 'synchronize' );

		return ImportRest::handle( $request );
	}
}
