diff --git a/.github/workflows/test-coverage.yml b/.github/workflows/test-coverage.yml index fa2bd43..f069e6b 100644 --- a/.github/workflows/test-coverage.yml +++ b/.github/workflows/test-coverage.yml @@ -57,6 +57,6 @@ jobs: uses: 'codecov/codecov-action@v5.1.2' with: files: './.build/logs/clover.xml' - token: ${{ secrets.CODECOV_TOKEN }} + token: ${{ env.my_codecov_token }} diff --git a/README.md b/README.md index 678cc72..000aae3 100644 --- a/README.md +++ b/README.md @@ -30,27 +30,14 @@ use Storyblok\Mapi\MapiClient; /** @var MapiClient $client */ $client = new MapiClient($storyblokPersonalAccessToken); ``` -The second parameter (a string) for setting the region. In this case, you can use the string "US" or "AP" or "CA" or "CN". +The second parameter is for setting the region. +We provide and Enum class for setting the region.In this case, you can use the Region Enum Region::US or Region::AP or Region::CA or Region::CN. For example: ```php -$client = new MapiClient($storyblokPersonalAccessToken, 'US'); -``` -Or if you prefer you can use the static methods `initEU` to access the European region. - -If you need access to other regions, you can use: -- `initUS()` for the US region -- `initAP()` for the Asian Pacific region -- `initCA()` for the Canadian region -- `initCN()` for the China region. +use \Storyblok\Mapi\Data\Enum\Region; -You can use a more generic `init()` method defining the second parameter (a string) for setting the region. In this case, you can use the string "US" or "AP" or "CA" or "CN": - -```php -// Using the default region EU -$client = MapiClient::init($storyblokPersonalAccessToken); -// Using the region US as an alternative way of initUS() -$client = MapiClient::init($storyblokPersonalAccessToken, 'US'); +$client = new MapiClient($storyblokPersonalAccessToken, Region::US); ``` ## Handling the Personal Access Token diff --git a/src/Data/Enum/Region.php b/src/Data/Enum/Region.php new file mode 100644 index 0000000..1b155fb --- /dev/null +++ b/src/Data/Enum/Region.php @@ -0,0 +1,35 @@ + + */ + public static function values(): array + { + return array_column(self::cases(), 'value'); + } + + /** + * Check if a value is a valid region. + * + * @param string $value the code of region to be validated + * @return bool true if the region is one of the valid region (Upper case) + */ + public static function isValid(string $value): bool + { + return in_array($value, self::values(), true); + } +} diff --git a/src/MapiClient.php b/src/MapiClient.php index 3fd58c7..547ef21 100644 --- a/src/MapiClient.php +++ b/src/MapiClient.php @@ -4,6 +4,7 @@ namespace Storyblok\Mapi; +use Storyblok\Mapi\Data\Enum\Region; use Storyblok\Mapi\Endpoints\AssetApi; use Storyblok\Mapi\Endpoints\ManagementApi; use Storyblok\Mapi\Endpoints\SpaceApi; @@ -18,10 +19,10 @@ class MapiClient public function __construct( string $personalAccessToken, - string $region = "EU", + Region $region = Region::EU, ?string $baseUri = null, ) { - $baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region); + $baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region->value); $this->httpClient = HttpClient::create() ->withOptions([ 'base_uri' => $baseUriMapi, @@ -34,50 +35,11 @@ public function __construct( ]); } - public static function initEU(string $personalAccessToken): self - { - return self::init( - $personalAccessToken, - "EU", - ); - } - - public static function initUS(string $personalAccessToken): self - { - return self::init( - $personalAccessToken, - "US", - ); - } - - public static function initAP(string $personalAccessToken): self - { - return self::init( - $personalAccessToken, - "AP", - ); - } - - public static function initCA(string $personalAccessToken): self - { - return self::init( - $personalAccessToken, - "CA", - ); - } - - public static function initCN(string $personalAccessToken): self - { - return self::init( - $personalAccessToken, - "CN", - ); - } public static function init( string $personalAccessToken, - string $region = "EU", + Region $region = Region::EU, ?string $baseUri = null, ): self {