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 new group with the Example and Test #27

Merged
merged 15 commits into from
Aug 8, 2024
Merged
4 changes: 4 additions & 0 deletions Example/GroupsExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function groupsExample()
// instantiate pushover group (can be injected into service using Dependency Injection)
$group = new Group("replace_with_pushover_group_key", $application);

// Use any valid key or placeholder ^[a-zA-Z0-9]{30}$ as group key to create new group
$createGroupResponse = $group->create('Test');
$newGroupKey = $createGroupResponse->getGroupKey();

// 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 @@ -25,6 +25,7 @@ Light, simple and fast, yet comprehensive wrapper for the [Pushover](https://pus
- Query emergency priority receipt
- Cancel emergency priority retry
- Groups API ([Example](Example/GroupsExample.php))
- Create the new group
- Retrieve information about the group
- Add / Remove users
- Enable / Disable users
Expand Down
24 changes: 24 additions & 0 deletions src/Api/Groups/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Serhiy\Pushover\Client\Curl\Curl;
use Serhiy\Pushover\Client\GroupsClient;
use Serhiy\Pushover\Client\Request\Request;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\AddUserToGroupResponse;
use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse;
Expand Down Expand Up @@ -56,6 +57,11 @@ class Group
*/
private $users;

/**
* @param string $key Group key. (Put str_repeat('0', 30) before creating new group)
*
* @throws InvalidArgumentException
*/
public function __construct(string $key, Application $application)
{
if (1 != preg_match("/^[a-zA-Z0-9]{30}$/", $key)) {
Expand Down Expand Up @@ -121,6 +127,24 @@ public function retrieveGroupInformation(): RetrieveGroupResponse
return $response;
}

/**
* Create the group. Reflected in the API and in the group editor on our website.
*/
public function create(string $name): CreateGroupResponse
{
$this->name = $name;

$client = new GroupsClient($this, GroupsClient::ACTION_CREATE_GROUP);
$request = new Request($client->buildApiUrl(), Request::POST, $client->buildCurlPostFields());

$curlResponse = Curl::do($request);

$response = new CreateGroupResponse($curlResponse);
$response->setRequest($request);

return $response;
}

/**
* Adds an existing Pushover user to your Delivery Group.
*
Expand Down
9 changes: 8 additions & 1 deletion src/Client/GroupsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class GroupsClient extends Client implements ClientInterface
const ACTION_DISABLE_USER = "disable_user";
const ACTION_ENABLE_USER = "enable_user";
const ACTION_RENAME_GROUP = "rename";
const ACTION_CREATE_GROUP = "create";

/**
* @var Group
Expand All @@ -53,6 +54,10 @@ public function __construct(Group $group, string $action)
*/
public function buildApiUrl()
{
if ($this->action == self::ACTION_CREATE_GROUP) {
return Curl::API_BASE_URL."/".Curl::API_VERSION."/groups.json";
}

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 Expand Up @@ -89,7 +94,9 @@ public function buildCurlPostFields(Recipient $recipient = null): array
}
}

if ($this->action == self::ACTION_RENAME_GROUP) {
if ($this->action == self::ACTION_RENAME_GROUP ||
$this->action == self::ACTION_CREATE_GROUP
) {
$curlPostFields["name"] = $this->group->getName();
}

Expand Down
50 changes: 50 additions & 0 deletions src/Client/Response/CreateGroupResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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 Serhiy Lunak
*/
class CreateGroupResponse extends Response
{
/**
* @var string Obtained Group Key
*/
private $groupKey;

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

/**
* @param mixed $curlResponse
*/
private function processCurlResponse($curlResponse): void
{
$decodedCurlResponse = $this->processInitialCurlResponse($curlResponse);
$this->groupKey = property_exists($decodedCurlResponse,'group') ? $decodedCurlResponse->group : null;
}

/**
* @return string Group key obtained
*/
public function getGroupKey(): string
{
return $this->groupKey;
}
}
14 changes: 14 additions & 0 deletions tests/Api/Groups/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Application;
use Serhiy\Pushover\Client\Response\RetrieveGroupResponse;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;

/**
* @author Serhiy Lunak
Expand Down Expand Up @@ -65,4 +66,17 @@ public function testRetrieveGroupInformation()

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

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

$response = $group->create('unit test '. date('Y-m-d H:i:s'));

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