Automattic\WooCommerce\Internal\CLI\Migrator\Core

PlatformRegistry::get_mapperpublicWC 1.0

Retrieves and instantiates the mapper class for a given platform.

Method of the class: PlatformRegistry{}

No Hooks.

Returns

PlatformMapperInterface. An instance of the platform's mapper class.

Usage

$PlatformRegistry = new PlatformRegistry();
$PlatformRegistry->get_mapper( $platform_id, $args ): PlatformMapperInterface;
$platform_id(string) (required)
The ID of the platform.
$args(array)
Optional arguments to pass to the mapper constructor.
Default: array()

PlatformRegistry::get_mapper() code WC 10.7.0

public function get_mapper( string $platform_id, array $args = array() ): PlatformMapperInterface {
	$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 )
			)
		);
	}

	$mapper_class = $platform['mapper'];

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

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

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

	// If arguments are provided, instantiate manually to pass constructor args.
	// Otherwise, use the WooCommerce DI container for dependency injection.
	if ( ! empty( $args ) ) {
		return new $mapper_class( $args );
	} else {
		$container = wc_get_container();
		return $container->get( $mapper_class );
	}
}