Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create group rebased #50

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
57a6394
Prep for next release
slunak Aug 8, 2024
6cfcee6
Run php-cs-fixer on src
slunak Aug 8, 2024
bcde230
Group List implementation #29
Vitexus Aug 12, 2024
e1848c2
Process only correct response #29
Vitexus Aug 12, 2024
de1f864
missing use added #29
Vitexus Aug 12, 2024
fa09c58
Update tests/Client/GroupsClientTest.php
Vitexus Aug 12, 2024
14ad546
ListGroupResponse.php renamed to ListGroupsResponse.php
Vitexus Aug 12, 2024
9849a90
php-cs-fixer fix src used
Vitexus Aug 12, 2024
1530b28
Group and GroupTest fix
Vitexus Aug 12, 2024
08491b4
The api response parsing to array of groups test.
Vitexus Aug 12, 2024
2e2b752
cs fixer used
Vitexus Aug 12, 2024
946eca9
returned class fix
Vitexus Aug 12, 2024
9d79a59
argument type fix
Vitexus Aug 12, 2024
4b7b2da
Add PHP-CS-Fixer config
OskarStark Aug 12, 2024
cd13f6d
rebased
OskarStark Aug 12, 2024
e8658e2
-
OskarStark Aug 12, 2024
0b12ecd
CS-Fixer: Enable `void_return`
OskarStark Aug 20, 2024
d7e7918
CS-Fixer: Enable `ordered_imports`
OskarStark Aug 20, 2024
fb5b3f8
CS-Fixer: Cleanup config
OskarStark Aug 20, 2024
f857c13
CS-Fixer: Enable `phpdoc_to_property_type`
OskarStark Aug 20, 2024
98d1856
fix tests
slunak Aug 21, 2024
2ebd799
CS-Fixer: Enable `declare_strict_types`
OskarStark Aug 21, 2024
1d8fc06
CS-Fixer: Enable `phpdoc_summary`
OskarStark Aug 21, 2024
cdcebb1
CS-Fixer: Enable `strict_comparison``
OskarStark Aug 22, 2024
0bf7225
CS-Fixer: Enable `nullable_type_declaration_for_default_null_value`
OskarStark Aug 22, 2024
daacc7f
comments must be max 80 characters per line long
Vitexus Aug 26, 2024
08b1ee5
missing variable for $group added
Vitexus Aug 26, 2024
e9b7bee
ListGroupsResponseTest phpdoc fix
Vitexus Aug 26, 2024
2aa6804
Merge branch 'master' into create_group
Vitexus Aug 26, 2024
f87f869
Changes requested by @OskarStark for #50
Vitexus Aug 26, 2024
bcba688
Merge branch 'master' of github.com:Vitexus/pushover-php into create_…
Vitexus Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
.php-cs-fixer.dist.php
.idea/
.build
.php-version
Vitexus marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions Example/GroupsExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions src/Api/Groups/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Serhiy\Pushover\Client\Request\Request;
use Serhiy\Pushover\Client\Response\AddUserToGroupResponse;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\ListGroupsResponse;
use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse;
Expand All @@ -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
*/
Expand Down Expand Up @@ -136,6 +140,23 @@ 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.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Client/AssignLicenseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class AssignLicenseClient extends Client implements ClientInterface
{
public const API_PATH = 'licenses/assign.json';

/**
* {@inheritDoc}
*/
Vitexus marked this conversation as resolved.
Show resolved Hide resolved
public function buildApiUrl(): string
{
return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/'.self::API_PATH;
Expand Down
6 changes: 6 additions & 0 deletions src/Client/GroupsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ 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';
public const ACTION_LIST_GROUPS = 'list';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private?


private Group $group;

/**
Expand All @@ -53,6 +55,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();
}
Expand Down
57 changes: 57 additions & 0 deletions src/Client/Response/ListGroupsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Pushover package.
*
* (c) Serhiy Lunak <https://github.com/slunak>
*
* 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
*/
class ListGroupsResponse extends Response
{
/**
* @var array
*/
public $groups = [];

/**
* @param mixed $curlResponse
*/
public function __construct($curlResponse)
{
$this->processCurlResponse($curlResponse);
}

/**
* @param mixed $curlResponse
*/
private function processCurlResponse($curlResponse): void
{
$this->groups = [];
$decodedCurlResponse = $this->processInitialCurlResponse($curlResponse);
if (property_exists($decodedCurlResponse, 'groups')) {
foreach ($decodedCurlResponse->groups as $grp) {
$this->groups[$grp->name] = $grp->group;
}
}
}

/**
* List of groups
*
* @return array<string, string> group names with keys eg.['name'=>'key',..]
*/
public function getGroups(): array
{
return $this->groups;
}
}
15 changes: 15 additions & 0 deletions tests/Api/Groups/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Serhiy\Pushover\Application;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\RetrieveGroupResponse;
use Serhiy\Pushover\Client\Response\ListGroupsResponse;

/**
* @author Serhiy Lunak
Expand Down Expand Up @@ -76,4 +77,18 @@ public function testCreate(): void

$this->assertInstanceOf(CreateGroupResponse::class, $response);
}

/**
* @group Integration
*/
public function testList()
{
$application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token
$group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application);

$response = $group->list();

$this->assertInstanceOf(ListGroupsResponse::class, $response);
}

}
2 changes: 2 additions & 0 deletions tests/Client/GroupsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ 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());
Vitexus marked this conversation as resolved.
Show resolved Hide resolved
}

public function testBuildCurlPostFields(): void
Expand Down
44 changes: 44 additions & 0 deletions tests/Client/Response/ListGroupsResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Pushover package.
*
* (c) Serhiy Lunak <https://github.com/slunak>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Client\Response;

use Serhiy\Pushover\Client\Response\ListGroupsResponse;
use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Recipient;

class ListGroupsResponseTest extends TestCase
{
/**
* prepare ListGroupsResponse Test
Vitexus marked this conversation as resolved.
Show resolved Hide resolved
*/
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->assertEquals("aaaaaaaa-1111-bbbb-2222-cccccccccccc", $response->getRequestToken());
return $response;
}


/**
* @depends testCanBeCreated
* @param ListGroupsResponse $response
*/
public function testGetGroups(ListGroupsResponse $response)
{
$groups = $response->getGroups();
$this->assertEquals($groups, ['Group1'=>'111111111111111111111111111111','group2'=>'222222222222222222222222222222', 'Group 3'=>'333333333333333333333333333333']);
}
}
Loading