diff --git a/.gitignore b/.gitignore index 85ffd95..85f2c8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .phpunit.cache vendor composer.lock +.idea/ +.vscode/ .DS_Store \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon index e8b3e64..d32e949 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,9 @@ parameters: - checkMissingIterableValueType: false level: 6 paths: - - src \ No newline at end of file + - src + + ignoreErrors: + - + identifier: missingType.iterableValue \ No newline at end of file diff --git a/src/AbstractClient.php b/src/AbstractClient.php index 78a620b..10ba4f0 100644 --- a/src/AbstractClient.php +++ b/src/AbstractClient.php @@ -94,14 +94,13 @@ public function factorResponse(ResponseInterface $response, ?bool $throwNotFound protected function checkIfRequestWasSuccessful(?array $response = null, ?bool $throwNotFoundException = null): void { if (isset($response['status']) && $response['status'] === Status::UNAUTHORIZED) { - $message = isset($response['message']) ? $response['message'] : "The request was not authorized."; + $message = $response['message'] ?? "The request was not authorized."; throw new RequestUnauthorizedException($message); } if (isset($response['success']) && boolval($response['success']) === false) { - $message = isset($response['message']) ? - $response['message'] : + $message = $response['message'] ?? "The request did not return a successful response."; throw new UnsuccessfulResponseException($message); diff --git a/src/Endpoint/Cource/Coruces.php b/src/Endpoint/Cource/Coruces.php index e74ee02..f78caf7 100644 --- a/src/Endpoint/Cource/Coruces.php +++ b/src/Endpoint/Cource/Coruces.php @@ -6,10 +6,11 @@ use AdroSoftware\CircleSoSdk\Endpoint\AbstractEndpoint; use AdroSoftware\CircleSoSdk\Endpoint\EndpointInterface; -use AdroSoftware\CircleSoSdk\Exception\{ - CommunityIdNotPresentException, - UnsuccessfulResponseException, -}; +use AdroSoftware\CircleSoSdk\Exception\{CommunityIdNotPresentException, + RequestUnauthorizedException, + ResourceNotFoundException, + UnsuccessfulResponseException}; +use Http\Client\Exception; class Courses extends AbstractEndpoint implements EndpointInterface { @@ -110,6 +111,7 @@ public function showLesson(int $id, ?int $communityId = null): mixed /** * Create a course lesson. * + * @param array $data * @throws CommunityIdNotPresentException * @throws UnsuccessfulResponseException */ @@ -131,6 +133,9 @@ public function createLesson( * Delete a course lesson. * * @throws CommunityIdNotPresentException + * @throws Exception + * @throws RequestUnauthorizedException + * @throws ResourceNotFoundException * @throws UnsuccessfulResponseException */ public function deleteLesson( diff --git a/src/Endpoint/Member/Members.php b/src/Endpoint/Member/Members.php index 7e3a170..70f4288 100644 --- a/src/Endpoint/Member/Members.php +++ b/src/Endpoint/Member/Members.php @@ -146,4 +146,28 @@ public function remove(string $email, ?int $communityId = null): mixed ) ); } + + /** + * Invite Member to Community + */ + public function invite(string $email, ?int $communityId = null): mixed + { + $endpoint = "/community_members/"; + + $data = [ + 'email' => $email, + ]; + + if (!empty($communityId)) { + $this->ensureCommunityIdIsPresent($communityId); + $data['community_id'] = $communityId; + } + + return $this->factorResponse( + $this->circleSo->getHttpClient()->post( + uri: $endpoint, + body: json_encode($data), + ) + ); + } } diff --git a/tests/Endpoint/Member/MembersTest.php b/tests/Endpoint/Member/MembersTest.php index df76cd6..5aea68f 100644 --- a/tests/Endpoint/Member/MembersTest.php +++ b/tests/Endpoint/Member/MembersTest.php @@ -152,4 +152,38 @@ public function test_member_removed_failed(): void ->remove('i_dont_exist@example.com'); } + + public function test_member_invite_ok(): void + { + $circleSo = $this->getSdkWithMockedClient([ + new Response(200, [], json_response('invite_member')), + ]); + + $response = $circleSo + ->members() + ->communityId(1) + ->invite('adro@example.com'); + + $this->assertSame(true, $response['success']); + $this->assertSame('This user has been invited to the community.', $response['message']); + } + + public function test_member_invite_failed(): void + { + $this->expectException(UnsuccessfulResponseException::class); + $this->expectExceptionMessage( + 'This user could not be invited. Please ensure that the user and community specified exists, and that the user is not already a member of the community.' + ); + + $this->expectExceptionCode(500); + + $circleSo = $this->getSdkWithMockedClient([ + new Response(200, [], json_response('invite_member_failed')), + ]); + + $circleSo + ->members() + ->communityId(1) + ->invite('error@example.com'); + } } diff --git a/tests/stubs/responses/invite_member.json b/tests/stubs/responses/invite_member.json new file mode 100644 index 0000000..88038d8 --- /dev/null +++ b/tests/stubs/responses/invite_member.json @@ -0,0 +1,34 @@ +{ + "success": true, + "message": "This user has been invited to the community.", + "user": { + "id": 1, + "email": "adri@example.com", + "first_name": "Adro", + "last_name": "Morelos", + "bio": null, + "time_zone": null, + "accepted_terms_at": "2023-05-19T08:31:00.000Z", + "accepted_privacy_at": "2023-05-19T08:31:00.000Z", + "admin": null, + "prefs": null, + "created_at": "2023-05-19T08:31:00.000Z", + "updated_at": "2023-05-19T08:31:00.000Z", + "headline": "API Test Purposes", + "posts_count": 0, + "comments_count": 0, + "profile_info": {}, + "public_uid": "195c6c33", + "affiliate_code": null, + "affiliate_ref": null, + "password_confirmed_at": null, + "password_confirmation_sent_at": null, + "password_set_at": null, + "checkout_sessions": null, + "is_flagged": false, + "utm_source": null, + "terms_of_service": null, + "attachable_sgid": "BAh7CEkiCGdpZAY6BkVUSSIwZ2lkOi8vanVtcHN0YXJ0LWFwcC9Vc2VyLzkxNTE0Nzc_ZXhwaXJlc19pbgY7AFRJIgxwdXJwb3NlBjsAVEkiD2F0dGFjaGFibGUGOwBUSSIPZXhwaXJlc19hdAY7AFQw--de1c1ec6a2629b31ff66c06058c0ab25a40ee126", + "community_member_id": 10117090 + } +} \ No newline at end of file diff --git a/tests/stubs/responses/invite_member_failed.json b/tests/stubs/responses/invite_member_failed.json new file mode 100644 index 0000000..9618a99 --- /dev/null +++ b/tests/stubs/responses/invite_member_failed.json @@ -0,0 +1,4 @@ +{ + "success": false, + "message": "This user could not be invited. Please ensure that the user and community specified exists, and that the user is not already a member of the community." +} \ No newline at end of file