Skip to content

Commit 07a3cb2

Browse files
committed
Refactoring instancing Api classes
1 parent 52dcc75 commit 07a3cb2

File tree

10 files changed

+63
-43
lines changed

10 files changed

+63
-43
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ To handle Stories, get stories, get a single story, create a story, update a sto
171171
use Storyblok\ManagementApi\ManagementApiClient;
172172
$spaceId= "1234";
173173
$client = new ManagementApiClient($storyblokPersonalAccessToken);
174-
$storyApi = $client->storyApi($spaceId);
174+
$storyApi = new StoryApi($client, $spaceId);
175175
```
176176

177177

@@ -199,8 +199,9 @@ foreach ($data as $key => $story) {
199199
You can filter stories using `StoriesParams`.
200200

201201
```php
202+
use Storyblok\ManagementApi\Endpoints\StoryApi;
202203

203-
$storyApi = $client->storyApi($spaceId);
204+
$storyApi = new StoryApi($client, $spaceId);
204205
$stories = $storyApi->page(
205206
new StoriesParams(containComponent: "feature"),
206207
new PaginationParams(1, 1000)
@@ -217,9 +218,10 @@ In this example, you will retrieve all stories where the "title" field is empty.
217218

218219
```php
219220
use Storyblok\ManagementApi\QueryParameters\Filters\Filter;
221+
use Storyblok\ManagementApi\Endpoints\StoryBulkApi;
220222
use Storyblok\ManagementApi\QueryParameters\Filters\QueryFilters;
221223

222-
$storyBulkApi = $client->storyBulkApi($spaceId);
224+
$storyBulkApi = new StoryBulkApi($client, $spaceId);
223225
$stories = $storyBulkApi->all(
224226
filters: (new QueryFilters())->add(
225227
new Filter(
@@ -315,7 +317,7 @@ myslug-003;My Story 3 BULK;page
315317
Next, you can implement a script to load and parse the CSV file. In this case, we use `SplFileObject` and then call the `createStories` method to process the data:
316318

317319
```php
318-
$storyBulkApi = $client->storyBulkApi($spaceId);
320+
$storyBulkApi = new StoryBulkApi($client, $spaceId);
319321
$file = new SplFileObject("stories.csv");
320322
$file->setFlags(SplFileObject::READ_CSV);
321323
$file->setCsvControl(separator: ";");
@@ -339,7 +341,7 @@ To get the current user, owner of the Personal access token used you can use the
339341

340342
```php
341343

342-
$response = $c->userApi()->me();
344+
$response = new UserApi($client)->me();
343345
/** @var UserData $currentUser */
344346
$currentUser = $response->data();
345347
// "User ID"
@@ -367,15 +369,15 @@ use Storyblok\ManagementApi\ManagementApiClient;
367369
$client = new ManagementApiClient($storyblokPersonalAccessToken);
368370

369371
$spaceId = "spaceid";
370-
$assetApi = $client->assetApi($spaceId);
372+
$assetApi = new AssetApi($client, $spaceId);
371373
```
372374

373375
### Getting the assets list
374376

375377
To get the assets list you can use the `assetApi` and the `AssetsData`.
376378

377379
```php
378-
$assetApi = $client->assetApi($spaceId);
380+
$assetApi = new AssetApi($client, $spaceId);
379381
$response = $assetApi->page();
380382
/** @var AssetsData $assets */
381383
$assets = $response->data();
@@ -395,7 +397,7 @@ Using the `AssetsParams` class you can set up filters for filtering the assets.
395397
```php
396398
use Storyblok\ManagementApi\QueryParameters\{AssetsParams,PaginationParams};
397399

398-
$assetApi = $client->assetApi($spaceId);
400+
$assetApi = new AssetApi($client, $spaceId);
399401
$assets = $assetApi->page(
400402
new AssetsParams(
401403
inFolder: -1,
@@ -449,7 +451,7 @@ if ($response->isOk()) {
449451
To delete an asset, you can use the `delete()` method. The `delete()` method requires the asset ID (you want to delete) as parameter:
450452

451453
```php
452-
$assetApi = $c->assetApi($spaceId);
454+
$assetApi = new AssetApi($client, $spaceId);
453455
echo "DELETING " . $assetId . PHP_EOL;
454456
$response = $assetApi->delete($assetId);
455457
$deletedAsset = $response->data();
@@ -467,7 +469,7 @@ use Storyblok\ManagementApi\ManagementApiClient;
467469
$client = new ManagementApiClient($storyblokPersonalAccessToken);
468470

469471
$spaceId = "spaceid";
470-
$tagApi = $client->tagApi($spaceId);
472+
$tagApi = new TagApi($client, $spaceId);
471473
```
472474

473475
### Getting the tags list
@@ -517,8 +519,8 @@ use Storyblok\ManagementApi\ManagementApiClient;
517519
$client = new ManagementApiClient($storyblokPersonalAccessToken);
518520

519521
$spaceId = "your-space-id";
520-
$storyApi = $client->storyApi($spaceId);
521-
$assetApi = $client->assetApi($spaceId);
522+
$storyApi = new StoryApi($client, $spaceId);
523+
$assetApi = new AssetApi($client, $spaceId);
522524

523525
echo "UPLOADING ASSET..." . PHP_EOL;
524526
$response = $assetApi->upload("image.png");
@@ -565,7 +567,7 @@ If you need to handle workflows (retrieving workflows or create new custom workf
565567
### Retrieving workflows
566568

567569
```php
568-
$workflowApi = $client->workflowApi($spaceId);
570+
$workflowApi = new WorkflowApi($client, $spaceId);
569571
$response = $workflowApi->list();
570572
/** @var WorkflowsData $workflows */
571573
$workflows = $response->data();
@@ -582,7 +584,7 @@ foreach ($workflows as $key => $workflow) {
582584
### Creating a new custom workflow
583585

584586
```php
585-
$workflowApi = $client->workflowApi($spaceId);
587+
$workflowApi = new WorkflowApi($client, $spaceId);
586588
$workflowData = new WorkflowData();
587589
$workflowData->setName("Name");
588590
$response = $workflowApi->create($workflowData);
@@ -608,7 +610,7 @@ foreach ($workflowStages as $key => $workflowStage) {
608610
In this example, we are going to retrieve the first workflow id available (probably you should retrieve a proper workflow that makes sense for your use case):
609611

610612
```php
611-
$workflowApi = $client->workflowApi($spaceId);
613+
$workflowApi = new WorkflowApi($client, $spaceId);
612614
$response = $workflowApi->list();
613615
$workflowId = $response->data()->get("0.id");
614616
```
@@ -654,14 +656,14 @@ $client = new ManagementApiClient($storyblokPersonalAccessToken);
654656
Getting the ManagementApi instance:
655657

656658
```php
657-
$managementApi = $client->managementApi()
659+
$managementApi = new ManagementApi($client);
658660
```
659661

660662
Calling GET HTTP method with `spaces/:spaceid/internal_tags`:
661663

662664
```php
663665
$spaceId = "12345";
664-
$response = $clientEU->managementApi()->get(
666+
$response = new ManagementApi($client)->get(
665667
"spaces/{$spaceId}/internal_tags",
666668
[
667669
"by_object_type" => "asset",

src/Endpoints/AssetApi.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Storyblok\ManagementApi\Endpoints;
66

7-
use GuzzleHttp\Client;
87
use Storyblok\ManagementApi\Data\AssetData;
98
use Storyblok\ManagementApi\Data\AssetsData;
109
use Storyblok\ManagementApi\Data\StoryblokData;

src/Endpoints/EndpointBase.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Storyblok\ManagementApi\Endpoints;
66

7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\NullLogger;
79
use Storyblok\ManagementApi\Data\StoryblokData;
10+
use Storyblok\ManagementApi\ManagementApiClient;
811
use Storyblok\ManagementApi\StoryblokResponse;
912
use Storyblok\ManagementApi\StoryblokResponseInterface;
1013
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -15,7 +18,14 @@
1518
*/
1619
class EndpointBase
1720
{
18-
public function __construct(protected HttpClientInterface $httpClient) {}
21+
protected HttpClientInterface $httpClient;
22+
23+
public function __construct(
24+
protected ManagementApiClient $managementClient,
25+
protected LoggerInterface $logger = new NullLogger(),
26+
) {
27+
$this->httpClient = $managementClient->httpClient();
28+
}
1929

2030

2131
/**

src/Endpoints/EndpointSpace.php

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

55
namespace Storyblok\ManagementApi\Endpoints;
66

7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\NullLogger;
9+
use Storyblok\ManagementApi\ManagementApiClient;
710
use Symfony\Contracts\HttpClient\HttpClientInterface;
811

912
/**
@@ -13,9 +16,10 @@
1316
class EndpointSpace extends EndpointBase
1417
{
1518
public function __construct(
16-
protected HttpClientInterface $httpClient,
19+
protected ManagementApiClient $managementClient,
1720
protected string|int $spaceId,
21+
LoggerInterface $logger = new NullLogger(),
1822
) {
19-
parent::__construct($httpClient);
23+
parent::__construct($managementClient, $logger);
2024
}
2125
}

src/Endpoints/StoryApi.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Storyblok\ManagementApi\Endpoints;
66

7+
use Psr\Log\NullLogger;
8+
use Storyblok\ManagementApi\ManagementApiClient;
79
use Storyblok\ManagementApi\QueryParameters\Filters\QueryFilters;
810
use Storyblok\ManagementApi\QueryParameters\PaginationParams;
911
use Storyblok\ManagementApi\QueryParameters\StoriesParams;
@@ -28,11 +30,11 @@ class StoryApi extends EndpointSpace
2830
* StoryApi constructor.
2931
*/
3032
public function __construct(
31-
HttpClientInterface $httpClient,
33+
ManagementApiClient $managementClient,
3234
string|int $spaceId,
33-
private readonly LoggerInterface $logger,
35+
LoggerInterface $logger = new NullLogger(),
3436
) {
35-
parent::__construct($httpClient, $spaceId);
37+
parent::__construct($managementClient, $spaceId, $logger);
3638
}
3739

3840
/**

src/Endpoints/StoryBulkApi.php

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

55
namespace Storyblok\ManagementApi\Endpoints;
66

7+
use Psr\Log\NullLogger;
8+
use Storyblok\ManagementApi\ManagementApiClient;
79
use Storyblok\ManagementApi\QueryParameters\Filters\QueryFilters;
810
use Storyblok\ManagementApi\QueryParameters\PaginationParams;
911
use Storyblok\ManagementApi\QueryParameters\StoriesParams;
@@ -40,12 +42,12 @@ class StoryBulkApi extends EndpointSpace
4042
* StoryApi constructor.
4143
*/
4244
public function __construct(
43-
HttpClientInterface $httpClient,
45+
ManagementApiClient $managementClient,
4446
string|int $spaceId,
45-
private readonly LoggerInterface $logger,
47+
LoggerInterface $logger = new NullLogger(),
4648
) {
47-
parent::__construct($httpClient, $spaceId);
48-
$this->api = new StoryApi($httpClient, $spaceId, $logger);
49+
parent::__construct($managementClient, $spaceId, $logger);
50+
$this->api = new StoryApi($managementClient, $spaceId, $logger);
4951
}
5052

5153
/**

src/Endpoints/TagApi.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Storyblok\ManagementApi\Endpoints;
66

7-
use Storyblok\ManagementApi\Data\StoryblokData;
87
use Storyblok\ManagementApi\Data\TagData;
98
use Storyblok\ManagementApi\Data\TagsData;
109
use Storyblok\ManagementApi\StoryblokResponseInterface;

src/Endpoints/UserApi.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Storyblok\ManagementApi\Endpoints;
66

7-
use Storyblok\ManagementApi\Data\SpaceData;
8-
use Storyblok\ManagementApi\Data\SpacesData;
9-
use Storyblok\ManagementApi\Data\StoryblokData;
107
use Storyblok\ManagementApi\Data\UserData;
118
use Storyblok\ManagementApi\StoryblokResponseInterface;
129

src/ManagementApiClient.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,20 @@ public static function initTest(
7373

7474
}
7575

76+
public function httpClient(): HttpClientInterface
77+
{
78+
return $this->httpClient;
79+
}
80+
7681
public function spaceApi(): SpaceApi
7782
{
78-
return new SpaceApi($this->httpClient);
83+
return new SpaceApi($this);
7984
}
8085

8186
public function storyApi(string|int $spaceId, ?LoggerInterface $logger = null): StoryApi
8287
{
8388
return new StoryApi(
84-
$this->httpClient,
89+
$this,
8590
$spaceId,
8691
$logger ?? new NullLogger(),
8792
);
@@ -90,39 +95,39 @@ public function storyApi(string|int $spaceId, ?LoggerInterface $logger = null):
9095
public function storyBulkApi(string|int $spaceId, ?LoggerInterface $logger = null): StoryBulkApi
9196
{
9297
return new StoryBulkApi(
93-
$this->httpClient,
98+
$this,
9499
$spaceId,
95100
$logger ?? new NullLogger(),
96101
);
97102
}
98103

99104
public function userApi(): UserApi
100105
{
101-
return new UserApi($this->httpClient);
106+
return new UserApi($this);
102107
}
103108

104109
public function assetApi(string|int $spaceId): AssetApi
105110
{
106-
return new AssetApi($this->httpClient, $spaceId);
111+
return new AssetApi($this, $spaceId);
107112
}
108113

109114
public function tagApi(string|int $spaceId): TagApi
110115
{
111-
return new TagApi($this->httpClient, $spaceId);
116+
return new TagApi($this, $spaceId);
112117
}
113118

114119
public function workflowApi(string|int $spaceId): WorkflowApi
115120
{
116-
return new WorkflowApi($this->httpClient, $spaceId);
121+
return new WorkflowApi($this, $spaceId);
117122
}
118123

119124
public function workflowStageApi(string|int $spaceId): WorkflowStageApi
120125
{
121-
return new WorkflowStageApi($this->httpClient, $spaceId);
126+
return new WorkflowStageApi($this, $spaceId);
122127
}
123128

124129
public function managementApi(): ManagementApi
125130
{
126-
return new ManagementApi($this->httpClient);
131+
return new ManagementApi($this);
127132
}
128133
}

tests/Feature/StoryBulkTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function warning(string|\Stringable $message, array $context = []): void
149149
$mapiClient = ManagementApiClient::initTest($client);
150150

151151
// Use TestStoryApi instead of regular StoryApi
152-
$storyBulkApi = new TestStoryBulkApi($client, '222', $mockLogger);
152+
$storyBulkApi = new TestStoryBulkApi($mapiClient, '222', $mockLogger);
153153

154154
// Create test stories
155155
$stories = [
@@ -225,7 +225,7 @@ public function error(string|\Stringable $message, array $context = []): void
225225
$mapiClient = ManagementApiClient::initTest($client);
226226

227227
// Use TestStoryApi instead of regular StoryApi
228-
$storyApi = new TestStoryBulkApi($client, '222', $mockLogger);
228+
$storyApi = new TestStoryBulkApi($mapiClient, '222', $mockLogger);
229229

230230
// Create test story
231231
$stories = [

0 commit comments

Comments
 (0)