Automattic\WooCommerce\Internal\CLI\Migrator\Core

PlatformRegistry::get_fetcherpublicWC 1.0

Retrieves and instantiates the fetcher class for a given platform.

Method of the class: PlatformRegistry{}

No Hooks.

Returns

PlatformFetcherInterface. An instance of the platform's fetcher class.

Usage

$PlatformRegistry = new PlatformRegistry();
$PlatformRegistry->get_fetcher( $platform_id ): PlatformFetcherInterface;
$platform_id(string) (required)
The ID of the platform.

PlatformRegistry::get_fetcher() code WC 10.7.0

public function get_fetcher( string $platform_id ): PlatformFetcherInterface {
	$platform = $this->get_platform( $platform_id );

	if ( ! $platform ) {
		throw new InvalidArgumentException(
			sprintf(
				/* translators: %s: Platform ID */
				esc_html__( 'Platform %s not found.', 'woocommerce' ),
				esc_html( $platform_id )
			)
		);
	}

	$fetcher_class = $platform['fetcher'];

	// Validate that fetcher class is a non-empty string.
	if ( ! is_string( $fetcher_class ) || empty( $fetcher_class ) ) {
		throw new InvalidArgumentException(
			sprintf(
				/* translators: %s: Platform ID */
				esc_html__( 'Invalid fetcher class for platform %s. Fetcher must be a non-empty string.', 'woocommerce' ),
				esc_html( $platform_id )
			)
		);
	}

	if ( ! class_exists( $fetcher_class ) ) {
		throw new InvalidArgumentException(
			sprintf(
				/* translators: %1$s: Platform ID, %2$s: Class name */
				esc_html__( 'Invalid fetcher class for platform %1$s. Class %2$s does not exist.', 'woocommerce' ),
				esc_html( $platform_id ),
				esc_html( $fetcher_class )
			)
		);
	}

	if ( ! in_array( PlatformFetcherInterface::class, class_implements( $fetcher_class ), true ) ) {
		throw new InvalidArgumentException(
			sprintf(
				/* translators: %1$s: Platform ID, %2$s: Class name, %3$s: Interface name */
				esc_html__( 'Invalid fetcher class for platform %1$s. Class %2$s does not implement %3$s.', 'woocommerce' ),
				esc_html( $platform_id ),
				esc_html( $fetcher_class ),
				esc_html( PlatformFetcherInterface::class )
			)
		);
	}

	// Get credentials from credential manager and pass to fetcher constructor.
	$credentials = $this->credential_manager->get_credentials( $platform_id );
	if ( ! is_array( $credentials ) ) {
		throw new InvalidArgumentException(
			sprintf(
				/* translators: %s: platform ID */
				'No credentials found for platform "%s". Please configure credentials using: wp wc migrate setup',
				esc_html( $platform_id )
			)
		);
	}
	return new $fetcher_class( $credentials );
}