Skip to content

Commit 6062dd6

Browse files
committed
refactor(php): Use DTO to represent a workspace
1 parent eaace4c commit 6062dd6

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

lib/Controller/WorkspaceController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ public function findAll(): JSONResponse {
144144
continue;
145145
}
146146

147-
$spaces[$workspace['name']] = $this->workspaceFormatter->format($workspace, $folderInfo);
147+
$workspaceDto = $this->workspaceFormatter->format($workspace, $folderInfo);
148+
$spaces[$workspace['name']] = $workspaceDto->jsonSerialize();
148149
}
149150

150151
// We only want to return those workspaces for which the connected user is a manager

lib/Service/Dto/WorkspaceDto.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace OCA\Workspace\Service\Dto;
4+
5+
class WorkspaceDto implements \JsonSerializable {
6+
public function __construct(
7+
public readonly ?int $id,
8+
public readonly ?string $color,
9+
public readonly ?int $groupfolderId,
10+
public readonly bool $isOpen = false,
11+
public readonly ?string $name,
12+
public readonly int|string|null $quota,
13+
public readonly ?int $size,
14+
public readonly mixed $managers,
15+
public readonly object $users,
16+
public readonly ?int $usersCount,
17+
public readonly array $groups,
18+
public readonly object $addedGroups,
19+
) {
20+
}
21+
22+
public function jsonSerialize(): array {
23+
return [
24+
'id' => $this->id,
25+
'color' => $this->color,
26+
'groupfolderId' => $this->groupfolderId,
27+
'isOpen' => $this->isOpen,
28+
'name' => $this->name,
29+
'quota' => $this->quota,
30+
'size' => $this->size,
31+
'managers' => $this->managers,
32+
'users' => $this->users,
33+
'usersCount' => $this->usersCount,
34+
'groups' => $this->groups,
35+
'added_groups' => $this->addedGroups,
36+
];
37+
}
38+
}

lib/Service/Formatter/WorkspaceFormatter.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OCA\Workspace\Service\Formatter;
44

5+
use OCA\Workspace\Service\Dto\WorkspaceDto;
56
use OCA\Workspace\Service\Group\GroupFormatter;
67
use OCA\Workspace\Service\Group\UserGroup;
78
use OCP\IGroupManager;
@@ -20,25 +21,13 @@ public function __construct(
2021
/**
2122
* @param array $workspace
2223
* @param array $folderInfo
23-
* @return array
24+
* @return WorkspaceDto
2425
*/
25-
public function format(array $workspace, array $folderInfo): array {
26-
$space = [
27-
'id' => $workspace['id'] ?? null,
28-
'color' => $workspace['color_code'] ?? null,
29-
'groupfolderId' => $workspace['groupfolder_id'] ?? null,
30-
'isOpen' => false,
31-
'name' => $workspace['name'] ?? null,
32-
'quota' => $folderInfo['quota'] ?? null,
33-
'size' => $folderInfo['size'] ?? null,
34-
'managers' => null,
35-
'users' => (object)[],
36-
'usersCount' => self::NO_USERS,
37-
];
38-
26+
public function format(array $workspace, array $folderInfo): WorkspaceDto {
3927
$wsGroups = [];
4028
$addedGroups = [];
4129
$gids = array_keys($folderInfo['groups'] ?? []);
30+
$usersCount = self::NO_USERS;
4231

4332
foreach ($gids as $gid) {
4433
$group = $this->groupManager->get($gid);
@@ -59,13 +48,23 @@ public function format(array $workspace, array $folderInfo): array {
5948
}
6049

6150
if (UserGroup::isWorkspaceUserGroupId($gid)) {
62-
$space['usersCount'] = $group->count();
51+
$usersCount = $group->count();
6352
}
6453
}
6554

66-
$space['groups'] = GroupFormatter::formatGroups($wsGroups);
67-
$space['added_groups'] = (object)GroupFormatter::formatGroups($addedGroups);
68-
69-
return $space;
55+
return new WorkspaceDto(
56+
id: $workspace['id'] ?? null,
57+
color: $workspace['color_code'] ?? null,
58+
groupfolderId: $workspace['groupfolder_id'] ?? null,
59+
isOpen: false,
60+
name: $workspace['name'] ?? null,
61+
quota: $folderInfo['quota'] ?? null,
62+
size: $folderInfo['size'] ?? null,
63+
managers: null,
64+
users: (object)[],
65+
usersCount: $usersCount,
66+
groups: GroupFormatter::formatGroups($wsGroups),
67+
addedGroups: (object)GroupFormatter::formatGroups($addedGroups)
68+
);
7069
}
7170
}

0 commit comments

Comments
 (0)