diff --git a/Example/GroupsExample.php b/Example/GroupsExample.php index b8f9789..ae50497 100644 --- a/Example/GroupsExample.php +++ b/Example/GroupsExample.php @@ -41,6 +41,11 @@ public function groupsExample() $createGroupResponse = $group->create('Test'); $newGroupKey = $createGroupResponse->getGroupKey(); + // Obtain list of all groups + $listGroupsResponse = $group->list(); + /** @var array $groups ['name' => 'key', 'name2' => 'key2'] */ + $groups = $listGroupsResponse->getGroups(); + // Retrieve information about the group from the API and populate the object with it. /** @var RetrieveGroupResponse $retrieveGroupResponse */ $retrieveGroupResponse = $group->retrieveGroupInformation(); diff --git a/README.md b/README.md index 0ff061d..4aac34f 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Light, simple and fast, yet comprehensive wrapper for the [Pushover](https://pus - Cancel emergency priority retry - Groups API ([Example](Example/GroupsExample.php)) - Create a group + - List groups - Retrieve information about the group - Add / Remove users - Enable / Disable users diff --git a/src/Api/Groups/Group.php b/src/Api/Groups/Group.php index e8048f5..e87c6c0 100644 --- a/src/Api/Groups/Group.php +++ b/src/Api/Groups/Group.php @@ -21,6 +21,7 @@ use Serhiy\Pushover\Client\Response\CreateGroupResponse; use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse; use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse; +use Serhiy\Pushover\Client\Response\ListGroupsResponse; use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse; use Serhiy\Pushover\Client\Response\RenameGroupResponse; use Serhiy\Pushover\Client\Response\RetrieveGroupResponse; @@ -30,10 +31,13 @@ /** * Pushover Delivery Group. * - * Delivery Groups allow broadcasting the same Pushover message to a number of different users at once with just a single group token, - * used in place of a user token (or in place of specifying multiple user keys in every request). For situations where subscriptions are not appropriate, - * or where automated manipulation of group members is required, such as changing an on-call notification group, - * or syncing with an external directory system, our Groups API is available. + * Delivery Groups allow broadcasting the same Pushover message to a number of + * different users at once with just a single group token, used in place of a + * user token (or in place of specifying multiple user keys in every request). + * For situations where subscriptions are not appropriate, or where automated + * manipulation of group members is required, such as changing an on-call + * notification group, or syncing with an external directory system, our Groups + * API is available. * * @author Serhiy Lunak */ @@ -136,12 +140,26 @@ public function create(string $name): CreateGroupResponse return $response; } + /** + * List groups. + */ + public function list(): ListGroupsResponse + { + $client = new GroupsClient($this, GroupsClient::ACTION_LIST_GROUPS); + $request = new Request($client->buildApiUrl(), Request::GET); + + $curlResponse = Curl::do($request); + + $response = new ListGroupsResponse($curlResponse); + $response->setRequest($request); + + return $response; + } + /** * Adds an existing Pushover user to your Delivery Group. - * - * @return AddUserToGroupResponse */ - public function addUser(Recipient $recipient) + public function addUser(Recipient $recipient): AddUserToGroupResponse { $client = new GroupsClient($this, GroupsClient::ACTION_ADD_USER); $request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields($recipient)); diff --git a/src/Client/Curl/Curl.php b/src/Client/Curl/Curl.php index d41df29..3e3ea7d 100644 --- a/src/Client/Curl/Curl.php +++ b/src/Client/Curl/Curl.php @@ -24,7 +24,6 @@ class Curl { public const API_BASE_URL = 'https://api.pushover.net'; - public const API_VERSION = '1'; /** diff --git a/src/Client/GroupsClient.php b/src/Client/GroupsClient.php index 33ba731..469f77d 100644 --- a/src/Client/GroupsClient.php +++ b/src/Client/GroupsClient.php @@ -30,12 +30,13 @@ class GroupsClient extends Client implements ClientInterface public const ACTION_ENABLE_USER = 'enable_user'; public const ACTION_RENAME_GROUP = 'rename'; public const ACTION_CREATE_GROUP = 'create'; - private Group $group; + public const ACTION_LIST_GROUPS = 'list'; /** * Action that client performs. */ private string $action; + private Group $group; public function __construct(Group $group, string $action) { @@ -53,6 +54,10 @@ public function buildApiUrl(): string return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json'; } + if ($this->action === self::ACTION_LIST_GROUPS) { + return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json?token='.$this->group->getApplication()->getToken(); + } + if ($this->action === self::ACTION_RETRIEVE_GROUP) { return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'.json?token='.$this->group->getApplication()->getToken(); } diff --git a/src/Client/Response/ListGroupsResponse.php b/src/Client/Response/ListGroupsResponse.php new file mode 100644 index 0000000..e400c67 --- /dev/null +++ b/src/Client/Response/ListGroupsResponse.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Serhiy\Pushover\Client\Response; + +use Serhiy\Pushover\Client\Response\Base\Response; + +/** + * @author Vítězslav Dvořák + */ +final class ListGroupsResponse extends Response +{ + /** + * @var array + */ + public array $groups = []; + + /** + * @param mixed $curlResponse + */ + public function __construct($curlResponse) + { + $this->processCurlResponse($curlResponse); + } + + /** + * List of groups. + * + * @return array group names with keys eg. ['name' => 'key', ...] + */ + public function getGroups(): array + { + return $this->groups; + } + + /** + * @param mixed $curlResponse + */ + private function processCurlResponse($curlResponse): void + { + $decodedCurlResponse = $this->processInitialCurlResponse($curlResponse); + + if (property_exists($decodedCurlResponse, 'groups')) { + foreach ($decodedCurlResponse->groups as $grp) { + $this->groups[$grp->name] = $grp->group; + } + } + } +} diff --git a/tests/Api/Groups/GroupTest.php b/tests/Api/Groups/GroupTest.php index a1d9549..b61fdbb 100644 --- a/tests/Api/Groups/GroupTest.php +++ b/tests/Api/Groups/GroupTest.php @@ -17,6 +17,7 @@ use Serhiy\Pushover\Api\Groups\Group; use Serhiy\Pushover\Application; use Serhiy\Pushover\Client\Response\CreateGroupResponse; +use Serhiy\Pushover\Client\Response\ListGroupsResponse; use Serhiy\Pushover\Client\Response\RetrieveGroupResponse; /** @@ -76,4 +77,17 @@ public function testCreate(): void $this->assertInstanceOf(CreateGroupResponse::class, $response); } + + /** + * @group Integration + */ + public function testList(): void + { + $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token + $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); + + $response = $group->list(); + + $this->assertInstanceOf(ListGroupsResponse::class, $response); + } } diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index 06b009a..b5e23c0 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -46,6 +46,9 @@ public function testBuildApiUrl(): void $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); $this->assertEquals('https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); + + $client = new GroupsClient($group, GroupsClient::ACTION_LIST_GROUPS); + $this->assertSame('https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); } public function testBuildCurlPostFields(): void diff --git a/tests/Client/Response/ListGroupsResponseTest.php b/tests/Client/Response/ListGroupsResponseTest.php new file mode 100644 index 0000000..473733a --- /dev/null +++ b/tests/Client/Response/ListGroupsResponseTest.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Client\Response; + +use PHPUnit\Framework\TestCase; +use Serhiy\Pushover\Client\Response\ListGroupsResponse; + +final class ListGroupsResponseTest extends TestCase +{ + public function testCanBeCreated(): ListGroupsResponse + { + $successfulCurlResponse = '{"groups":[{"group":"111111111111111111111111111111","name":"Group1"},{"group":"222222222222222222222222222222","name":"group2"},{"group":"333333333333333333333333333333","name":"Group 3"}],"status":1,"request":"aaaaaaaa-1111-bbbb-2222-cccccccccccc"}'; + $response = new ListGroupsResponse($successfulCurlResponse); + + $this->assertInstanceOf(ListGroupsResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + $this->assertSame('aaaaaaaa-1111-bbbb-2222-cccccccccccc', $response->getRequestToken()); + + return $response; + } + + /** + * @depends testCanBeCreated + */ + public function testGetGroups(ListGroupsResponse $response): void + { + $groups = $response->getGroups(); + $this->assertSame($groups, ['Group1' => '111111111111111111111111111111', 'group2' => '222222222222222222222222222222', 'Group 3' => '333333333333333333333333333333']); + } +}