Skip to content

Commit 476f6f7

Browse files
committed
Implemeting Region enum
1 parent 479dbe9 commit 476f6f7

File tree

4 files changed

+44
-60
lines changed

4 files changed

+44
-60
lines changed

.github/workflows/test-coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ jobs:
5757
uses: 'codecov/[email protected]'
5858
with:
5959
files: './.build/logs/clover.xml'
60-
token: ${{ secrets.CODECOV_TOKEN }}
60+
token: ${{ env.my_codecov_token }}
6161

6262

README.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,14 @@ use Storyblok\Mapi\MapiClient;
3030
/** @var MapiClient $client */
3131
$client = new MapiClient($storyblokPersonalAccessToken);
3232
```
33-
The second parameter (a string) for setting the region. In this case, you can use the string "US" or "AP" or "CA" or "CN".
33+
The second parameter is for setting the region.
34+
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.
3435
For example:
3536
```php
36-
$client = new MapiClient($storyblokPersonalAccessToken, 'US');
37-
```
38-
Or if you prefer you can use the static methods `initEU` to access the European region.
39-
40-
If you need access to other regions, you can use:
4137

42-
- `initUS()` for the US region
43-
- `initAP()` for the Asian Pacific region
44-
- `initCA()` for the Canadian region
45-
- `initCN()` for the China region.
38+
use \Storyblok\Mapi\Data\Enum\Region;
4639

47-
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":
48-
49-
```php
50-
// Using the default region EU
51-
$client = MapiClient::init($storyblokPersonalAccessToken);
52-
// Using the region US as an alternative way of initUS()
53-
$client = MapiClient::init($storyblokPersonalAccessToken, 'US');
40+
$client = new MapiClient($storyblokPersonalAccessToken, Region::US);
5441
```
5542

5643
## Handling the Personal Access Token

src/Data/Enum/Region.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Storyblok\Mapi\Data\Enum;
6+
7+
enum Region: string
8+
{
9+
case EU = 'EU';
10+
case US = 'US';
11+
case AP = 'AP';
12+
case CA = 'CA';
13+
case CN = 'CN';
14+
15+
/**
16+
* Get all region values as an array.
17+
*
18+
* @return array<string>
19+
*/
20+
public static function values(): array
21+
{
22+
return array_column(self::cases(), 'value');
23+
}
24+
25+
/**
26+
* Check if a value is a valid region.
27+
*
28+
* @param string $value the code of region to be validated
29+
* @return bool true if the region is one of the valid region (Upper case)
30+
*/
31+
public static function isValid(string $value): bool
32+
{
33+
return in_array($value, self::values(), true);
34+
}
35+
}

src/MapiClient.php

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Storyblok\Mapi;
66

7+
use Storyblok\Mapi\Data\Enum\Region;
78
use Storyblok\Mapi\Endpoints\AssetApi;
89
use Storyblok\Mapi\Endpoints\ManagementApi;
910
use Storyblok\Mapi\Endpoints\SpaceApi;
@@ -18,10 +19,10 @@ class MapiClient
1819

1920
public function __construct(
2021
string $personalAccessToken,
21-
string $region = "EU",
22+
Region $region = Region::EU,
2223
?string $baseUri = null,
2324
) {
24-
$baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region);
25+
$baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region->value);
2526
$this->httpClient = HttpClient::create()
2627
->withOptions([
2728
'base_uri' => $baseUriMapi,
@@ -34,50 +35,11 @@ public function __construct(
3435
]);
3536
}
3637

37-
public static function initEU(string $personalAccessToken): self
38-
{
39-
return self::init(
40-
$personalAccessToken,
41-
"EU",
42-
);
43-
}
44-
45-
public static function initUS(string $personalAccessToken): self
46-
{
47-
return self::init(
48-
$personalAccessToken,
49-
"US",
50-
);
51-
}
52-
53-
public static function initAP(string $personalAccessToken): self
54-
{
55-
return self::init(
56-
$personalAccessToken,
57-
"AP",
58-
);
59-
}
60-
61-
public static function initCA(string $personalAccessToken): self
62-
{
63-
return self::init(
64-
$personalAccessToken,
65-
"CA",
66-
);
67-
}
68-
69-
public static function initCN(string $personalAccessToken): self
70-
{
71-
return self::init(
72-
$personalAccessToken,
73-
"CN",
74-
);
75-
}
7638

7739

7840
public static function init(
7941
string $personalAccessToken,
80-
string $region = "EU",
42+
Region $region = Region::EU,
8143
?string $baseUri = null,
8244
): self {
8345

0 commit comments

Comments
 (0)