Skip to content

Commit

Permalink
feat(space-groups): Add endpoints for space groups
Browse files Browse the repository at this point in the history
  • Loading branch information
adrorocker committed Mar 18, 2024
1 parent 13877c0 commit 371f0aa
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/CircleSo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MemberTag\MemberTag,
TaggedMembers\TaggedMembers,
};
use AdroSoftware\CircleSoSdk\Endpoint\SpaceGroup\SpaceGroups;

final class CircleSo extends AbstractClient implements InteractsWithEndpoints
{
Expand Down Expand Up @@ -46,4 +47,9 @@ public function taggedMembers(): TaggedMembers
{
return new TaggedMembers($this);
}

public function spaceGroups(): SpaceGroups
{
return new SpaceGroups($this);
}
}
132 changes: 132 additions & 0 deletions src/Endpoint/SpaceGroup/SpaceGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace AdroSoftware\CircleSoSdk\Endpoint\SpaceGroup;

use AdroSoftware\CircleSoSdk\Endpoint\AbstractEndpoint;
use AdroSoftware\CircleSoSdk\Endpoint\EndpointInterface;
use AdroSoftware\CircleSoSdk\Exception\{
CommunityIdNotPresentException,
UnsuccessfulResponseException,
};

class SpaceGroups extends AbstractEndpoint implements EndpointInterface
{
/**
* @throws CommunityIdNotPresentException
*/
public function spaceGroups(?int $communityId = null): mixed
{
$this->ensureCommunityIdIsPresent($communityId);

return $this->factorResponse(
$this->circleSo->getHttpClient()->get(
"/space_groups?community_id={$this->communityId}"
)
);
}

/**
* @throws CommunityIdNotPresentException
* @throws UnsuccessfulResponseException
*/
public function show(int $id, ?int $communityId = null): mixed
{
$this->ensureCommunityIdIsPresent($communityId);

return $this->factorResponse(
$this->circleSo->getHttpClient()->get(
"/space_groups/{$id}?community_id={$this->communityId}"
)
);
}

/**
* Create a space group.
*
* The following json example should demonstrate
* what the `$data` array should look like:
* ```json
* {
* "name": "Awesome Space Group",
* "slug": "awesome-space-group",
* "add_members_to_space_group_on_space_join": false,
* "allow_members_to_create_spaces": true,
* "automatically_add_members_to_new_spaces": false,
* "hide_non_member_spaces_from_sidebar": true,
* "is_hidden_from_non_members": false,
* "space_order_array": [],
* "position": 1
* }
* ```
* @throws CommunityIdNotPresentException
* @throws UnsuccessfulResponseException
*/
public function create(
array $data,
?int $communityId = null,
): mixed {
$this->ensureCommunityIdIsPresent($communityId);

return $this->factorResponse(
$this->circleSo->getHttpClient()->post(
uri: "/space_groups?community_id={$this->communityId}",
body: json_encode($data),
)
);
}

/**
* Update a space group.
*
* The following json example should demonstrate
* what the `$data` array should look like:
* ```json
* {
* "name": "Awesome Space Group",
* "slug": "awesome-space-group",
* "add_members_to_space_group_on_space_join": false,
* "allow_members_to_create_spaces": true,
* "automatically_add_members_to_new_spaces": false,
* "hide_non_member_spaces_from_sidebar": true,
* "is_hidden_from_non_members": false,
* }
* ```
* @throws CommunityIdNotPresentException
* @throws UnsuccessfulResponseException
*/
public function update(
int $id,
array $data,
?int $communityId = null,
): mixed {
$this->ensureCommunityIdIsPresent($communityId);

return $this->factorResponse(
$this->circleSo->getHttpClient()->put(
uri: "/space_groups/{$id}?community_id={$this->communityId}",
body: json_encode($data),
)
);
}

/**
* Delete a space group.
*
* @throws CommunityIdNotPresentException
* @throws UnsuccessfulResponseException
*/
public function delete(
int $id,
?int $communityId = null,
): mixed {
$this->ensureCommunityIdIsPresent($communityId);

return $this->factorResponse(
$this->circleSo->getHttpClient()->delete(
"/space_groups/{$id}?community_id={$this->communityId}",
)
);
}
}
108 changes: 108 additions & 0 deletions tests/Endpoint/SpaceGroup/SpaceGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace AdroSoftware\CircleSoSdk\Tests\Endpoint\SpaceGroup;

use AdroSoftware\CircleSoSdk\Tests\TestCase;
use GuzzleHttp\Psr7\Response;

class SpaceGroupTest extends TestCase
{
public function test_it_can_get_space_groups()
{
$sdk = $this->getSdkWithMockedClient([
new Response(200, [], json_response('space_groups')),
]);

$spaceGroups = $sdk->spaceGroups()
->communityId(1)
->spaceGroups();

$this->assertIsArray($spaceGroups);

$spaceGroupOne = $spaceGroups[0];
$spaceGroupTwo = $spaceGroups[1];
$spaceGroupThree = $spaceGroups[2];

$this->assertArrayHasKey('id', $spaceGroupOne);

$this->assertSame(1, $spaceGroupOne['id']);
$this->assertSame(2, $spaceGroupTwo['id']);
$this->assertSame('space-group-three', $spaceGroupThree['slug']);
}

public function test_it_can_get_a_space_group()
{
$sdk = $this->getSdkWithMockedClient([
new Response(200, [], json_response('space_group')),
]);

$spaceGroup = $sdk->spaceGroups()
->communityId(1)
->show(1);

$this->assertIsArray($spaceGroup);

$this->assertSame(1, $spaceGroup['id']);
$this->assertSame('space-group-one', $spaceGroup['slug']);
}

public function test_it_can_create_a_space_group()
{
$sdk = $this->getSdkWithMockedClient([
new Response(200, [], json_response('space_group_create')),
]);

$spaceGroupResponse = $sdk->spaceGroups()
->communityId(1)
->create([
'name' => 'Awesome Space Group',
'slug' => 'awesome-space-group',
]);

$this->assertIsArray($spaceGroupResponse);

$spaceGroup = $spaceGroupResponse['space_group'];

$this->assertSame(4, $spaceGroup['id']);
$this->assertSame('awesome-space-group', $spaceGroup['slug']);
}

public function test_it_can_update_a_space_group()
{
$sdk = $this->getSdkWithMockedClient([
new Response(200, [], json_response('space_group_update')),
]);

$spaceGroupResponse = $sdk->spaceGroups()
->communityId(1)
->update(4, [
'name' => 'Awesome Space Group',
'slug' => 'awesome-space-group',
]);

$this->assertIsArray($spaceGroupResponse);

$spaceGroup = $spaceGroupResponse['space_group'];

$this->assertSame(4, $spaceGroup['id']);
$this->assertSame('awesome-space-group', $spaceGroup['slug']);
}

public function test_it_can_delete_a_space_group()
{
$sdk = $this->getSdkWithMockedClient([
new Response(200, [], json_response('space_group_delete')),
]);

$spaceGroupResponse = $sdk->spaceGroups()
->communityId(1)
->delete(4);

$this->assertIsArray($spaceGroupResponse);

$this->assertSame('The space group has been removed from the community', $spaceGroupResponse['message']);
$this->assertTrue($spaceGroupResponse['success']);
}
}
21 changes: 21 additions & 0 deletions tests/stubs/responses/space_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": 1,
"name": "Space Group One",
"is_hidden_from_non_members": false,
"allow_members_to_create_spaces": false,
"community_id": 1,
"space_order_array": [
123456,
123457,
123458
],
"created_at": "2022-09-26T19:31:32.224Z",
"updated_at": "2023-10-05T21:15:23.225Z",
"automatically_add_members_to_new_spaces": true,
"add_members_to_space_group_on_space_join": true,
"slug": "space-group-one",
"hide_non_member_spaces_from_sidebar": null,
"space_group_members_count": 10,
"active_space_group_members_count": 20,
"hide_members_count": false
}
20 changes: 20 additions & 0 deletions tests/stubs/responses/space_group_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"success": true,
"message": "Space Group created.",
"space_group":
{
"id": 4,
"name": "Awesome Space Group",
"is_hidden_from_non_members": false,
"allow_members_to_create_spaces": true,
"community_id": 1,
"space_order_array": null,
"created_at": "2022-06-17T13:22:33.256Z",
"updated_at": "2022-06-17T13:22:33.256Z",
"automatically_add_members_to_new_spaces": false,
"add_members_to_space_group_on_space_join": false,
"slug": "awesome-space-group",
"hide_non_member_spaces_from_sidebar": true,
"space_group_members_count": 1
}
}
4 changes: 4 additions & 0 deletions tests/stubs/responses/space_group_delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"success": true,
"message": "The space group has been removed from the community"
}
19 changes: 19 additions & 0 deletions tests/stubs/responses/space_group_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"success": true,
"message": "Space Group updated.",
"space_group": {
"community_id": 1,
"name": "Awesome Space Group",
"is_hidden_from_non_members": false,
"allow_members_to_create_spaces": true,
"automatically_add_members_to_new_spaces": true,
"add_members_to_space_group_on_space_join": true,
"hide_non_member_spaces_from_sidebar": false,
"slug": "awesome-space-group",
"id": 4,
"space_order_array": [],
"created_at": "2022-06-17T13:27:31.661Z",
"updated_at": "2022-06-17T13:27:31.871Z",
"space_group_members_count": 1
}
}
65 changes: 65 additions & 0 deletions tests/stubs/responses/space_groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"id": 1,
"name": "Space Group One",
"is_hidden_from_non_members": false,
"allow_members_to_create_spaces": false,
"community_id": 1,
"space_order_array": [
123456,
123457,
123458
],
"created_at": "2022-09-26T19:31:32.224Z",
"updated_at": "2023-10-05T21:15:23.225Z",
"automatically_add_members_to_new_spaces": true,
"add_members_to_space_group_on_space_join": true,
"slug": "space-group-one",
"hide_non_member_spaces_from_sidebar": null,
"space_group_members_count": 10,
"active_space_group_members_count": 20,
"hide_members_count": false
},
{
"id": 2,
"name": "Space Group Two",
"is_hidden_from_non_members": false,
"allow_members_to_create_spaces": false,
"community_id": 1,
"space_order_array": [
123459,
123460,
123461
],
"created_at": "2022-09-26T19:32:42.679Z",
"updated_at": "2023-07-22T17:03:11.670Z",
"automatically_add_members_to_new_spaces": false,
"add_members_to_space_group_on_space_join": false,
"slug": "space-group-two",
"hide_non_member_spaces_from_sidebar": false,
"space_group_members_count": 21,
"active_space_group_members_count": 100,
"hide_members_count": null
},
{
"id": 3,
"name": "Space Group Three",
"is_hidden_from_non_members": false,
"allow_members_to_create_spaces": false,
"community_id": 1,
"space_order_array": [
123462,
123463,
123464
],
"created_at": "2022-10-13T19:40:43.721Z",
"updated_at": "2023-07-15T18:53:32.219Z",
"automatically_add_members_to_new_spaces": false,
"add_members_to_space_group_on_space_join": false,
"slug": "space-group-three",
"hide_non_member_spaces_from_sidebar": false,
"space_group_members_count": 1000,
"active_space_group_members_count": 2000,
"hide_members_count": true
}
]

0 comments on commit 371f0aa

Please sign in to comment.