From 31b6e75be9fa8cc503e1bdcd68a55e3fe856478e Mon Sep 17 00:00:00 2001 From: Jesse Evers Date: Wed, 10 Jul 2024 12:25:54 -0400 Subject: [PATCH] Create Marketplace/Region enums, add helpers to Endpoint enum * Endpoint::byCountryCode() and Endpoint::byRegion() static methods * Endpoint::byMarketplaceId() accepts a string or a Marketplace enum value * Marketplace::fromCountryCode() and Marketplace::fromRegion() static methods --- src/Enums/Endpoint.php | 71 ++++++++++++--------------------------- src/Enums/Marketplace.php | 65 +++++++++++++++++++++++++++++++++++ src/Enums/Region.php | 16 +++++++++ 3 files changed, 102 insertions(+), 50 deletions(-) create mode 100644 src/Enums/Marketplace.php create mode 100644 src/Enums/Region.php diff --git a/src/Enums/Endpoint.php b/src/Enums/Endpoint.php index 5d1b18565..ce42505d5 100644 --- a/src/Enums/Endpoint.php +++ b/src/Enums/Endpoint.php @@ -4,7 +4,6 @@ namespace SellingPartnerApi\Enums; -use InvalidArgumentException; use SellingPartnerApi\Traits\EnumTrait; enum Endpoint: string @@ -36,67 +35,39 @@ public static function host(Endpoint $endpoint): string return str_replace('https://', '', $endpoint->value); } - public static function byMarketplaceId(string $marketplaceId, bool $sandbox = false): self + public static function byMarketplaceId(string|Marketplace $marketplaceId, bool $sandbox = false): self { - return match ($marketplaceId) { - 'A2Q3Y263D00KWC', 'A2EUQ1WTGCTBG2', 'A1AM78C64UM0Y8', 'ATVPDKIKX0DER' => $sandbox ? self::NA_SANDBOX : self::NA, - - 'A2VIGQ35RCS4UG', 'AMEN7PMS3EDWL', 'A1PA6795UKMFR9', 'ARBP9OOSHTCHU', - 'A1RKKUPIHCS9HS', 'A13V1IB3VIYZZH', 'A1F83G8C2ARO7P', 'A21TJRUUN4KGV', - 'APJ6JRA9NG5V4', 'A1805IZSGTT6HS', 'A1C3SOZRARQ6R3', 'A17E79C6D8DWNP', - 'A2NODRKZP88ZB9', 'A33AVAJ2PDY3EV', 'AE08WJ6YKNBMC' => $sandbox ? self::EU_SANDBOX : self::EU, - - 'A19VAU5U5O7RUS', 'A39IBJ37TRP1C6', 'A1VC38T7YXB528' => $sandbox ? self::FE_SANDBOX : self::FE, + if (is_string($marketplaceId)) { + $marketplaceId = Marketplace::from($marketplaceId); + } - default => throw new InvalidArgumentException("Unknown marketplace ID $marketplaceId"), + return match (Marketplace::toRegion($marketplaceId)) { + Region::NA => $sandbox ? self::NA_SANDBOX : self::NA, + Region::EU => $sandbox ? self::EU_SANDBOX : self::EU, + Region::FE => $sandbox ? self::FE_SANDBOX : self::FE, }; } public static function byCountryCode(string $countryCode, bool $sandbox = false): self { - $countryToMarketplaceId = [ - 'BR' => 'A2Q3Y263D00KWC', - 'CA' => 'A2EUQ1WTGCTBG2', - 'MX' => 'A1AM78C64UM0Y8', - 'US' => 'ATVPDKIKX0DER', - - 'AE' => 'A2VIGQ35RCS4UG', - 'BE' => 'AMEN7PMS3EDWL', - 'DE' => 'A1PA6795UKMFR9', - 'EG' => 'ARBP9OOSHTCHU', - 'ES' => 'A1RKKUPIHCS9HS', - 'FR' => 'A13V1IB3VIYZZH', - // UK and GB are both common country codes for the United Kingdom - 'GB' => 'A1F83G8C2ARO7P', - 'UK' => 'A1F83G8C2ARO7P', - 'IN' => 'A21TJRUUN4KGV', - 'IT' => 'APJ6JRA9NG5V4', - 'NL' => 'A1805IZSGTT6HS', - 'PL' => 'A1C3SOZRARQ6R3', - 'SA' => 'A17E79C6D8DWNP', - 'SE' => 'A2NODRKZP88ZB9', - 'TR' => 'A33AVAJ2PDY3EV', - 'ZA' => 'AE08WJ6YKNBMC', - - 'AU' => 'A39IBJ37TRP1C6', - 'JP' => 'A1VC38T7YXB528', - 'SG' => 'A19VAU5U5O7RUS', - ]; + // UK and GB are both common country codes for the United Kingdom + $marketplace = $countryCode === 'UK' + ? Marketplace::GB + : Marketplace::fromCountryCode($countryCode); - if (! isset($countryToMarketplaceId[$countryCode])) { - throw new InvalidArgumentException("Unknown country code $countryCode"); - } - - return self::byMarketplaceId($countryToMarketplaceId[$countryCode], $sandbox); + return self::byMarketplaceId($marketplace, $sandbox); } - public static function byRegion(string $region, bool $sandbox = false): self + public static function byRegion(string|Region $region, bool $sandbox = false): self { + if (is_string($region)) { + $region = Region::from($region); + } + return match ($region) { - 'NA' => $sandbox ? self::NA_SANDBOX : self::NA, - 'EU' => $sandbox ? self::EU_SANDBOX : self::EU, - 'FE' => $sandbox ? self::FE_SANDBOX : self::FE, - default => throw new InvalidArgumentException("Unknown region $region"), + Region::NA => $sandbox ? self::NA_SANDBOX : self::NA, + Region::EU => $sandbox ? self::EU_SANDBOX : self::EU, + Region::FE => $sandbox ? self::FE_SANDBOX : self::FE, }; } } diff --git a/src/Enums/Marketplace.php b/src/Enums/Marketplace.php new file mode 100644 index 000000000..c94b8ee53 --- /dev/null +++ b/src/Enums/Marketplace.php @@ -0,0 +1,65 @@ + Region::NA, + + Marketplace::AE, Marketplace::BE, Marketplace::DE, Marketplace::EG, Marketplace::ES, Marketplace::FR, + Marketplace::GB, Marketplace::IN, Marketplace::IT, Marketplace::NL, Marketplace::PL, Marketplace::SA, + Marketplace::SE, Marketplace::TR, Marketplace::ZA => Region::EU, + + Marketplace::AU, Marketplace::JP, Marketplace::SG => Region::FE, + }; + } +} diff --git a/src/Enums/Region.php b/src/Enums/Region.php new file mode 100644 index 000000000..8eca3bcc8 --- /dev/null +++ b/src/Enums/Region.php @@ -0,0 +1,16 @@ +