Skip to content

Commit

Permalink
Create Marketplace/Region enums, add helpers to Endpoint enum
Browse files Browse the repository at this point in the history
* Endpoint::byCountryCode() and Endpoint::byRegion() static methods
* Endpoint::byMarketplaceId() accepts a string or a Marketplace enum value
* Marketplace::fromCountryCode() and Marketplace::fromRegion() static methods
  • Loading branch information
jlevers committed Jul 10, 2024
1 parent 8eef808 commit 31b6e75
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 50 deletions.
71 changes: 21 additions & 50 deletions src/Enums/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SellingPartnerApi\Enums;

use InvalidArgumentException;
use SellingPartnerApi\Traits\EnumTrait;

enum Endpoint: string
Expand Down Expand Up @@ -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,
};
}
}
65 changes: 65 additions & 0 deletions src/Enums/Marketplace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace SellingPartnerApi\Enums;

use InvalidArgumentException;
use SellingPartnerApi\Traits\EnumTrait;

enum Marketplace: string
{
use EnumTrait;

// North America
case BR = 'A2Q3Y263D00KWC';
case CA = 'A2EUQ1WTGCTBG2';
case MX = 'A1AM78C64UM0Y8';
case US = 'ATVPDKIKX0DER';

// Europe
case AE = 'A2VIGQ35RCS4UG';
case BE = 'AMEN7PMS3EDWL';
case DE = 'A1PA6795UKMFR9';
case EG = 'ARBP9OOSHTCHU';
case ES = 'A1RKKUPIHCS9HS';
case FR = 'A13V1IB3VIYZZH';
case GB = 'A1F83G8C2ARO7P';
case IN = 'A21TJRUUN4KGV';
case IT = 'APJ6JRA9NG5V4';
case NL = 'A1805IZSGTT6HS';
case PL = 'A1C3SOZRARQ6R3';
case SA = 'A17E79C6D8DWNP';
case SE = 'A2NODRKZP88ZB9';
case TR = 'A33AVAJ2PDY3EV';
case ZA = 'AE08WJ6YKNBMC';

// Far East
case AU = 'A39IBJ37TRP1C6';
case JP = 'A1VC38T7YXB528';
case SG = 'A19VAU5U5O7RUS';

public static function fromCountryCode(string $countryCode): Marketplace
{
$_countryCode = strtoupper($countryCode);
$constantStr = self::class.'::'.$_countryCode;
if (defined($constantStr)) {
return constant($constantStr);
}

throw new InvalidArgumentException('Unknown country code: '.$countryCode);
}

public static function toRegion(self $marketplace): Region
{
return match ($marketplace) {
Marketplace::BR, Marketplace::CA, Marketplace::MX, Marketplace::US => 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,
};
}
}
16 changes: 16 additions & 0 deletions src/Enums/Region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SellingPartnerApi\Enums;

use SellingPartnerApi\Traits\EnumTrait;

enum Region: string
{
use EnumTrait;

case NA = 'NA';
case EU = 'EU';
case FE = 'FE';
}

0 comments on commit 31b6e75

Please sign in to comment.