Skip to content

Commit

Permalink
Merge pull request #27 from Vitexus/create_group
Browse files Browse the repository at this point in the history
Create new group with the Example and Test
  • Loading branch information
slunak authored Aug 8, 2024
2 parents 13c0455 + da43fb0 commit ce70bbd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Change Log


All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
Expand All @@ -11,6 +10,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

- Test setTtl() is duplicated #24

### Added

- Added support for creating a new group #26

## [1.4.0]

### Added
Expand Down
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 a 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 @@ -16,6 +16,7 @@
use Serhiy\Pushover\Client\GroupsClient;
use Serhiy\Pushover\Client\Request\Request;
use Serhiy\Pushover\Client\Response\AddUserToGroupResponse;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\DisableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\EnableUserInGroupResponse;
use Serhiy\Pushover\Client\Response\RemoveUserFromGroupResponse;
Expand Down Expand Up @@ -56,6 +57,11 @@ class Group
*/
private $users;

/**
* @param string $key Group key. (Use any valid key or placeholder e.g. str_repeat('0', 30) if you are creating a 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 a group.
*/
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 @@ -14,6 +14,7 @@
use Serhiy\Pushover\Api\Groups\Group;
use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Application;
use Serhiy\Pushover\Client\Response\CreateGroupResponse;
use Serhiy\Pushover\Client\Response\RetrieveGroupResponse;

/**
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);
}
}

0 comments on commit ce70bbd

Please sign in to comment.