Skip to content

Commit 48257fc

Browse files
committed
Refactoring User class
1 parent 4552052 commit 48257fc

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ $client = new ManagementApiClient($storyblokPersonalAccessToken);
9191

9292
The Storyblok **Management API Client** provides two main approaches for interacting with the API:
9393

94-
- Using specific API classes (like `StoryApi` or `SpaceApi` or `AssetApi` or `TagApi`)
94+
- Using specific API classes (like `StoryApi` or `SpaceApi` or `AssetApi` or `TagApi` or `UserApi`)
95+
- Using specific API classes for handling bulk data (like `StoryBulkApi`)
9596
- Using the `ManagementApi` class
9697

9798
The `ManagementApi` class offers a flexible, generic interface for managing content. It includes methods to get, create, update, and delete content. With this approach, you can define the endpoint path and pass query string parameters as a generic array. The response is returned as a `StoryblokData` object, allowing you to access the JSON payload, status codes, and other details directly.
@@ -102,10 +103,11 @@ If a dedicated API class like `SpaceApi` or `StoryApi` does not exist for your d
102103

103104
In addition to the general-purpose `ManagementApi` class, the Storyblok Management PHP client also provides specific classes such as `SpaceApi`, `StoryApi`, `TagApi` and `AssetApi`. These classes function similarly to the `ManagementApi` but are tailored for specific scenarios, offering additional methods or data types to work with particular resources.
104105

105-
- **`SpaceApi`** focuses on managing space-level operations, such as retrieving space information, performing backup etc.
106-
- **`StoryApi`** specializes in handling stories and their content, including creating, updating, retrieving, and deleting stories. This class also provides methods that deal with the structure and fields specific to stories.
107-
- **`AssetApi`** designed to manage assets like images, files, and other media. It provides methods to upload, retrieve, and manage assets, offering features specific to media management.
108-
- **`TagApi`** designed to manage tags.
106+
- `SpaceApi` focuses on managing space-level operations, such as retrieving space information, performing backup etc.
107+
- `StoryApi` specializes in handling stories and their content, including creating, updating, retrieving, and deleting stories. This class also provides methods that deal with the structure and fields specific to stories.
108+
- `AssetApi` designed to manage assets like images, files, and other media. It provides methods to upload, retrieve, and manage assets, offering features specific to media management.
109+
- `TagApi` designed to manage tags.
110+
- `UserApi` designed to handle the current user. "Current" means the user related to the access token used for instancing the `ManagementApiClient` object.
109111

110112
These specialized classes extend the functionality of the `ManagementApi` class, offering more precise control and optimized methods for interacting with specific resource types in your Storyblok space.
111113

@@ -387,27 +389,35 @@ $createdStories = iterator_to_array($storyBulkApi->createStories($stories));
387389

388390
### Getting the current user
389391

390-
To get the current user, owner of the Personal access token used you can use the userApi and the UserData.
392+
To get the current user, owner of the Personal access token used you can use the `UserApi` class for calling endpoints and the `User` for accessing to returned data object properties.
393+
For example, here we want to retrieve the current user (via the `me()` method) and obtaining the `User` object via `data()`.
391394

392395
```php
396+
use Storyblok\ManagementApi\Endpoints\UserApi;
397+
use Storyblok\ManagementApi\ManagementApiClient;
398+
399+
$client = new ManagementApiClient(getAccessToken());
393400

394-
$response = new UserApi($client)->me();
395-
/** @var UserData $currentUser */
396-
$currentUser = $response->data();
401+
$currentUser = (new UserApi($client))->me()->data();
397402
// "User ID"
398-
echo $currentUser->id();
403+
echo $currentUser->id() . PHP_EOL;
399404
// "User identifier"
400-
echo $currentUser->userid();
405+
echo $currentUser->userid() . PHP_EOL;
401406
// "User email"
402-
echo $currentUser->email());
407+
echo $currentUser->email() . PHP_EOL;
403408
// "User has Organization"
404-
echo $currentUser->hasOrganization() ? " HAS ORG" : "NO ORG";
405-
// "User Organization"
406-
echo $currentUser->orgName();
409+
echo ($currentUser->hasOrganization() ? " HAS ORG:" . $currentUser->orgName() : "NO ORG") . PHP_EOL;
407410
// "User has Partner"
408-
echo $currentUser->hasPartner() ? " HAS PARTNER" : "NO PARTNER";
411+
echo ($currentUser->hasPartner() ? " HAS PARTNER" : "NO PARTNER") . PHP_EOL;;
412+
409413
```
410414

415+
416+
Typically, all the data object provides you some helper methods like:
417+
- `toArray()` to obtain the data in a PHP array;
418+
- `toJson()` to obtain a JSON string;
419+
- `dump()` for debugging purposes, it prints on standard output the indented JSON.
420+
411421
## Handling assets
412422

413423
### Getting the AssetApi instance

src/Data/UserData.php renamed to src/Data/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Storyblok\ManagementApi\StoryblokUtils;
88

9-
class UserData extends StoryblokData
9+
class User extends StoryblokData
1010
{
1111
/**
1212
* @param array<string, array<mixed>> $data

src/Endpoints/UserApi.php

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

55
namespace Storyblok\ManagementApi\Endpoints;
66

7-
use Storyblok\ManagementApi\Data\UserData;
7+
use Storyblok\ManagementApi\Data\User;
88
use Storyblok\ManagementApi\Response\StoryblokResponseInterface;
99
use Storyblok\ManagementApi\Response\UserResponse;
1010
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

src/Response/UserResponse.php

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

55
namespace Storyblok\ManagementApi\Response;
66

7-
use Storyblok\ManagementApi\Data\UserData;
7+
use Storyblok\ManagementApi\Data\User;
88
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
99
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
1010
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
@@ -23,12 +23,12 @@ class UserResponse extends StoryblokResponse implements StoryblokResponseInterfa
2323
* @throws ClientExceptionInterface
2424
*/
2525
#[\Override]
26-
public function data(): UserData
26+
public function data(): User
2727
{
2828
$key = "user";
2929
$array = $this->toArray();
3030
if (array_key_exists($key, $array)) {
31-
return new UserData($array[$key]);
31+
return new User($array[$key]);
3232
}
3333

3434
throw new StoryblokFormatException(sprintf("Expected '%s' in the response.", $key));

tests/Feature/UserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
$storyblokResponse = $userApi->me();
2121
$string = $storyblokResponse->getLastCalledUrl();
2222
expect($string)->toMatch('/.*users.*/');
23-
/* @var \Storyblok\ManagementApi\Data\UserData $userData */
23+
/* @var \Storyblok\ManagementApi\Data\User $userData */
2424
$userData = $storyblokResponse->data();
2525
expect($userData->firstname())->toBe("John");
2626
expect($userData->lastname())->toBe("Doe");
@@ -40,8 +40,8 @@
4040
expect($userData->avatarUrl())->toBe("https://img2.storyblok.com/72x72/avatars/118830/01290bd7fa/myimage.JPG");
4141
expect($userData->avatarUrl(null))->toBe("https://img2.storyblok.com/avatars/118830/01290bd7fa/myimage.JPG");
4242

43-
$userData = \Storyblok\ManagementApi\Data\UserData::make([]);
44-
expect($userData)->toBeInstanceOf(\Storyblok\ManagementApi\Data\UserData::class);
43+
$userData = \Storyblok\ManagementApi\Data\User::make([]);
44+
expect($userData)->toBeInstanceOf(\Storyblok\ManagementApi\Data\User::class);
4545

4646
$userApi = new UserApi($mapiClient);
4747
expect($userApi)->toBeInstanceOf(UserApi::class);

0 commit comments

Comments
 (0)