Skip to content

Commit 7e7418d

Browse files
committed
Deprecate API factory methods, prefer dependency injection
1 parent ee8b1b1 commit 7e7418d

File tree

7 files changed

+226
-66
lines changed

7 files changed

+226
-66
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

3-
## 1.0.12 - WIP
3+
## 1.1.0 - WIP
44
- Moving from PestPHP to PHPUnit
5+
- Deprecate API factory methods, prefer dependency injection
56

67
## 1.0.11 - 2026-01-05
78
- Adding parameters for updating a story

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,9 @@ And then create a new workflow stage:
11731173

11741174
```php
11751175
use Storyblok\ManagementApi\Data\WorkflowStageData;
1176+
use Storyblok\ManagementApi\Endpoints\WorkflowStageApi;
11761177

1177-
$workflowStageApi = $client->workflowStageApi($spaceId);
1178+
$workflowStageApi = new WorkflowStageApi($client, $spaceId);
11781179
$workflowStageData = new WorkflowStageData();
11791180
$workflowStageData->setName("Name");
11801181
$workflowStageData->setWorkflowId($workflowId);

src/ManagementApiClient.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,113 @@ public function httpAssetClient(): HttpClientInterface
105105
return $this->httpAssetClient;
106106
}
107107

108+
/**
109+
* @deprecated Use `new StoryApi($managementApiClient, $spaceId, $logger)` instead.
110+
*/
108111
public function storyApi(
109112
string|int $spaceId,
110113
?LoggerInterface $logger = null,
111114
): StoryApi {
115+
trigger_error(
116+
'Method storyApi() is deprecated. Use `new StoryApi($managementApiClient, $spaceId, $logger)` instead.',
117+
E_USER_DEPRECATED,
118+
);
119+
112120
return new StoryApi($this, $spaceId, $logger ?? new NullLogger());
113121
}
114122

123+
/**
124+
* @deprecated Use `new StoryBulkApi($managementApiClient, $spaceId, $logger)` instead.
125+
*/
115126
public function storyBulkApi(
116127
string|int $spaceId,
117128
?LoggerInterface $logger = null,
118129
): StoryBulkApi {
130+
trigger_error(
131+
'Method storyBulkApi() is deprecated. Use `new StoryBulkApi($managementApiClient, $spaceId, $logger)` instead.',
132+
E_USER_DEPRECATED,
133+
);
134+
119135
return new StoryBulkApi($this, $spaceId, $logger ?? new NullLogger());
120136
}
121137

138+
/**
139+
* @deprecated Use `new AssetApi($managementApiClient, $spaceId)` instead.
140+
*/
122141
public function assetApi(string|int $spaceId): AssetApi
123142
{
143+
trigger_error(
144+
'Method assetApi() is deprecated. Use `new AssetApi($managementApiClient, $spaceId)` instead.',
145+
E_USER_DEPRECATED,
146+
);
147+
124148
return new AssetApi($this, $spaceId);
125149
}
126150

151+
/**
152+
* @deprecated Use `new TagApi($managementApiClient, $spaceId)` instead.
153+
*/
127154
public function tagApi(string|int $spaceId): TagApi
128155
{
156+
trigger_error(
157+
'Method tagApi() is deprecated. Use `new TagApi($managementApiClient, $spaceId)` instead.',
158+
E_USER_DEPRECATED,
159+
);
160+
129161
return new TagApi($this, $spaceId);
130162
}
131163

164+
/**
165+
* @deprecated Use `new WorkflowApi($managementApiClient, $spaceId)` instead.
166+
*/
132167
public function workflowApi(string|int $spaceId): WorkflowApi
133168
{
169+
trigger_error(
170+
'Method workflowApi() is deprecated. Use `new WorkflowApi($managementApiClient, $spaceId)` instead.',
171+
E_USER_DEPRECATED,
172+
);
173+
134174
return new WorkflowApi($this, $spaceId);
135175
}
136176

177+
/**
178+
* @deprecated Use `new WorkflowStageApi($managementApiClient, $spaceId)` instead.
179+
*/
137180
public function workflowStageApi(string|int $spaceId): WorkflowStageApi
138181
{
182+
trigger_error(
183+
'Method workflowStageApi() is deprecated. Use `new WorkflowStageApi($managementApiClient, $spaceId)` instead.',
184+
E_USER_DEPRECATED,
185+
);
186+
139187
return new WorkflowStageApi($this, $spaceId);
140188
}
141189

190+
/**
191+
* @deprecated Use `new ComponentApi($managementApiClient, $spaceId, $logger)` instead.
192+
*/
142193
public function componentApi(
143194
string|int $spaceId,
144195
?LoggerInterface $logger = null,
145196
): ComponentApi {
197+
trigger_error(
198+
'Method componentApi() is deprecated. Use `new ComponentApi($managementApiClient, $spaceId, $logger)` instead.',
199+
E_USER_DEPRECATED,
200+
);
201+
146202
return new ComponentApi($this, $spaceId, $logger ?? new NullLogger());
147203
}
148204

205+
/**
206+
* @deprecated Use `new ManagementApi($managementApiClient)` instead.
207+
*/
149208
public function managementApi(): ManagementApi
150209
{
210+
trigger_error(
211+
'Method managementApi() is deprecated. Use `new ManagementApi($managementApiClient)` instead.',
212+
E_USER_DEPRECATED,
213+
);
214+
151215
return new ManagementApi($this);
152216
}
153217
}

tests/Feature/AssetApiTest.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public function testOneAssetAssetData(): void
3333
$storyblokData = $storyblokResponse->data();
3434

3535
$this->assertSame(111, $storyblokData->get("id"));
36-
$this->assertSame("https://a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg", $storyblokData->filenameCDN());
36+
$this->assertSame(
37+
"https://a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg",
38+
$storyblokData->filenameCDN(),
39+
);
3740
$this->assertSame(200, $storyblokResponse->getResponseStatusCode());
3841
$this->assertSame("image/jpeg", $storyblokData->contentType());
3942
$this->assertSame(3_094_788, $storyblokData->contentLength());
@@ -51,13 +54,16 @@ public function testOneAssetAssetData(): void
5154
public function testListAssetsAssetsData(): void
5255
{
5356
$responses = [
54-
$this->mockResponse("list-assets", 200, ["total" => 2, "per-page" => 25]),
57+
$this->mockResponse("list-assets", 200, [
58+
"total" => 2,
59+
"per-page" => 25,
60+
]),
5561
$this->mockResponse("empty-asset", 404),
5662
];
5763

5864
$client = new MockHttpClient($responses);
5965
$mapiClient = ManagementApiClient::initTest($client);
60-
$assetApi = $mapiClient->assetApi("222");
66+
$assetApi = new AssetApi($mapiClient, "222");
6167

6268
$storyblokResponse = $assetApi->page();
6369
$storyblokData = $storyblokResponse->data();
@@ -69,7 +75,10 @@ public function testListAssetsAssetsData(): void
6975
}
7076

7177
$this->assertSame(200, $storyblokResponse->getResponseStatusCode());
72-
$this->assertSame("No error detected, HTTP Status Code: 200", $storyblokResponse->getErrorMessage());
78+
$this->assertSame(
79+
"No error detected, HTTP Status Code: 200",
80+
$storyblokResponse->getErrorMessage(),
81+
);
7382
$this->assertSame(2, $storyblokResponse->total());
7483
$this->assertSame(25, $storyblokResponse->perPage());
7584

@@ -84,7 +93,10 @@ public function testListAssetsAssetsData(): void
8493
public function testListAssetsParams(): void
8594
{
8695
$responses = [
87-
$this->mockResponse("list-assets", 200, ["total" => 2, "per-page" => 25]),
96+
$this->mockResponse("list-assets", 200, [
97+
"total" => 2,
98+
"per-page" => 25,
99+
]),
88100
$this->mockResponse("list-assets", 200, [
89101
"total" => 200,
90102
"per-page" => 25,
@@ -98,21 +110,27 @@ public function testListAssetsParams(): void
98110

99111
$client = new MockHttpClient($responses);
100112
$mapiClient = ManagementApiClient::initTest($client);
101-
$assetApi = $mapiClient->assetApi("222");
113+
$assetApi = new AssetApi($mapiClient, "222");
102114

103115
$response = $assetApi->page(params: new AssetsParams(inFolder: -1));
104116

105117
$url = $response->getLastCalledUrl();
106118
$this->assertMatchesRegularExpression('/.*in_folder=-1.*$/', $url);
107-
$this->assertMatchesRegularExpression('/.*page=1&per_page=25.*$/', $url);
119+
$this->assertMatchesRegularExpression(
120+
'/.*page=1&per_page=25.*$/',
121+
$url,
122+
);
108123

109124
$response = $assetApi->page(
110125
params: new AssetsParams(inFolder: -1),
111126
page: new PaginationParams(5, 30),
112127
);
113128

114129
$url = $response->getLastCalledUrl();
115-
$this->assertMatchesRegularExpression('/.*page=5&per_page=30.*$/', $url);
130+
$this->assertMatchesRegularExpression(
131+
'/.*page=5&per_page=30.*$/',
132+
$url,
133+
);
116134

117135
$response = $assetApi->page(
118136
params: new AssetsParams(search: "something", withTags: "aaa"),
@@ -135,7 +153,7 @@ public function testAssetPayload(): void
135153

136154
$client = new MockHttpClient($responses);
137155
$mapiClient = ManagementApiClient::initTest($client);
138-
$assetApi = $mapiClient->assetApi("222");
156+
$assetApi = new AssetApi($mapiClient, "222");
139157

140158
$payload = $assetApi->buildPayload(
141159
"./tests/Feature/Data/image-test.png",
@@ -242,7 +260,10 @@ public function testAssetFieldHandling(): void
242260
$assetField = AssetField::makeFromAsset($asset);
243261

244262
$this->assertSame(111, $assetField->get("id"));
245-
$this->assertSame("https://a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg", $assetField->get("filename"));
263+
$this->assertSame(
264+
"https://a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg",
265+
$assetField->get("filename"),
266+
);
246267
$this->assertSame("", $assetField->getString("title"));
247268
}
248269

@@ -264,7 +285,10 @@ public function testSetAssetInStoryComponent(): void
264285
$content->setAsset("image", $asset);
265286

266287
$this->assertNull($content->get("id"));
267-
$this->assertSame("https://a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg", $content->get("image.filename"));
288+
$this->assertSame(
289+
"https://a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg",
290+
$content->get("image.filename"),
291+
);
268292
$this->assertSame("asset", $content->get("image.fieldtype"));
269293
$this->assertSame("My New Article", $content->get("title"));
270294
}
Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
namespace Tests\Feature;
66

7+
use Storyblok\ManagementApi\Data\Enum\Region;
8+
use Storyblok\ManagementApi\Endpoints\ManagementApi;
79
use Tests\TestCase;
810
use Storyblok\ManagementApi\ManagementApiClient;
11+
use Symfony\Component\HttpClient\CurlHttpClient;
912
use Symfony\Component\HttpClient\Exception\ClientException;
13+
use Symfony\Component\HttpClient\HttpClient;
1014
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\RetryableHttpClient;
1116

12-
final class ManagementApiClientTest extends TestCase
17+
final class ManagementApiTest extends TestCase
1318
{
1419
public function testMultipleResourcesStoryblokData(): void
1520
{
@@ -26,7 +31,7 @@ public function testMultipleResourcesStoryblokData(): void
2631
baseUri: "https://mapi.storyblok.com",
2732
);
2833
$mapiClient = ManagementApiClient::initTest($client);
29-
$managementApi = $mapiClient->managementApi();
34+
$managementApi = new ManagementApi($mapiClient);
3035

3136
$spaceId = "321388";
3237
$response = $managementApi->get(
@@ -36,7 +41,10 @@ public function testMultipleResourcesStoryblokData(): void
3641
],
3742
);
3843

39-
$this->assertSame("https://mapi.storyblok.com/v1/spaces/321388/internal_tags?by_object_type=asset", $response->getLastCalledUrl());
44+
$this->assertSame(
45+
"https://mapi.storyblok.com/v1/spaces/321388/internal_tags?by_object_type=asset",
46+
$response->getLastCalledUrl(),
47+
);
4048
$this->assertSame(8, $response->total());
4149
$this->assertTrue($response->isOk());
4250

@@ -73,7 +81,7 @@ public function testCreateResourceStoryblokData(): void
7381
baseUri: "https://mapi.storyblok.com",
7482
);
7583
$mapiClient = ManagementApiClient::initTest($client);
76-
$managementApi = $mapiClient->managementApi();
84+
$managementApi = new ManagementApi($mapiClient);
7785

7886
$spaceId = "321388";
7987
$response = $managementApi->post(
@@ -107,7 +115,7 @@ public function testDeleteResourceStoryblokData(): void
107115
baseUri: "https://mapi.storyblok.com",
108116
);
109117
$mapiClient = ManagementApiClient::initTest($client);
110-
$managementApi = $mapiClient->managementApi();
118+
$managementApi = new ManagementApi($mapiClient);
111119

112120
$spaceId = "321388";
113121
$tagId = "56980";
@@ -137,7 +145,7 @@ public function testEditResourceStoryblokData(): void
137145
baseUri: "https://mapi.storyblok.com",
138146
);
139147
$mapiClient = ManagementApiClient::initTest($client);
140-
$managementApi = $mapiClient->managementApi();
148+
$managementApi = new ManagementApi($mapiClient);
141149

142150
$spaceId = "321388";
143151
$tagId = "56980";
@@ -170,7 +178,7 @@ public function testStoryblokDataToArray(): void
170178
baseUri: "https://mapi.storyblok.com",
171179
);
172180
$mapiClient = ManagementApiClient::initTest($client);
173-
$managementApi = $mapiClient->managementApi();
181+
$managementApi = new ManagementApi($mapiClient);
174182

175183
$spaceId = "321388";
176184
$response = $managementApi->get(
@@ -182,4 +190,33 @@ public function testStoryblokDataToArray(): void
182190

183191
$this->assertIsArray($response->toArray());
184192
}
193+
194+
public function testInitializeManagementClient(): void
195+
{
196+
$client = new ManagementApiClient(
197+
personalAccessToken: "aaa",
198+
region: Region::EU,
199+
baseUri: "",
200+
shouldRetry: true,
201+
);
202+
$httpClient = $client->httpClient();
203+
$this->assertInstanceOf(RetryableHttpClient::class, $httpClient);
204+
205+
$client = new ManagementApiClient(
206+
personalAccessToken: "aaa",
207+
region: Region::EU,
208+
baseUri: "",
209+
shouldRetry: false,
210+
);
211+
$httpClient = $client->httpClient();
212+
$this->assertInstanceOf(CurlHttpClient::class, $httpClient);
213+
214+
$client = ManagementApiClient::init(
215+
personalAccessToken: "aaa",
216+
region: Region::EU,
217+
baseUri: "",
218+
);
219+
$httpClient = $client->httpClient();
220+
$this->assertInstanceOf(CurlHttpClient::class, $httpClient);
221+
}
185222
}

0 commit comments

Comments
 (0)