Skip to content

Commit

Permalink
Enhancement: Recipient rework (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored Oct 9, 2024
1 parent e3310c5 commit e1f2b27
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/Client/GlancesClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public function buildCurlPostFields(Glance $glance): array
'user' => $glance->getRecipient()->getUserKey(),
];

if (!empty($glance->getRecipient()->getDevice())) {
if (\count($glance->getRecipient()->getDevice()) > 1) {
throw new LogicException(sprintf('Glance can be pushed to only one device. "%s" devices provided.', \count($glance->getRecipient()->getDevice())));
if (!empty($glance->getRecipient()->getDevices())) {
if (\count($glance->getRecipient()->getDevices()) > 1) {
throw new LogicException(sprintf('Glance can be pushed to only one device. "%s" devices provided.', \count($glance->getRecipient()->getDevices())));
}

$curlPostFields['device'] = $glance->getRecipient()->getDeviceListCommaSeparated();
$curlPostFields['device'] = $glance->getRecipient()->getDevicesCommaSeparated();
}

$title = $glance->getGlanceDataFields()->getTitle();
Expand Down
4 changes: 2 additions & 2 deletions src/Client/GroupsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function buildCurlPostFields(?Recipient $recipient = null): array
$curlPostFields['user'] = $recipient->getUserKey();

if ($this->action === self::ACTION_ADD_USER) {
if (!empty($recipient->getDevice())) {
$curlPostFields['device'] = $recipient->getDevice()[0];
if (!empty($recipient->getDevices())) {
$curlPostFields['device'] = $recipient->getDevices()[0];
}

if (null !== $recipient->getMemo()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Client/MessageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function buildCurlPostFields(Notification $notification): array
'timestamp' => $notification->getMessage()->getTimestamp(),
];

if (!empty($notification->getRecipient()->getDevice())) {
$curlPostFields['device'] = $notification->getRecipient()->getDeviceListCommaSeparated();
if (!empty($notification->getRecipient()->getDevices())) {
$curlPostFields['device'] = $notification->getRecipient()->getDevicesCommaSeparated();
}

if (null !== $notification->getMessage()->getTitle()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Response/ReceiptResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function processCurlResponse(string $curlResponse): void
$recipient = new Recipient($decodedCurlResponse->acknowledged_by);
$recipient->addDevice($decodedCurlResponse->acknowledged_by_device);
$this->setAcknowledgedBy($recipient);
$this->setAcknowledgedByDevice($recipient->getDeviceListCommaSeparated());
$this->setAcknowledgedByDevice($recipient->getDevicesCommaSeparated());
}

$this->setLastDeliveredAt(new \DateTime('@'.$decodedCurlResponse->last_delivered_at));
Expand Down
8 changes: 4 additions & 4 deletions src/Client/SubscriptionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public function buildCurlPostFields(Subscription $subscription, Recipient $recip
'user' => $recipient->getUserKey(),
];

if (!empty($recipient->getDevice())) {
if (\count($recipient->getDevice()) > 1) {
throw new LogicException(sprintf('Only one device is supported. "%s" devices provided.', \count($recipient->getDevice())));
if (!empty($recipient->getDevices())) {
if (\count($recipient->getDevices()) > 1) {
throw new LogicException(sprintf('Only one device is supported. "%s" devices provided.', \count($recipient->getDevices())));
}

$curlPostFields['device_name'] = $recipient->getDeviceListCommaSeparated();
$curlPostFields['device_name'] = $recipient->getDevicesCommaSeparated();
}

if (null !== $sound) {
Expand Down
8 changes: 4 additions & 4 deletions src/Client/UserGroupValidationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public function buildCurlPostFields(Application $application, Recipient $recipie
'user' => $recipient->getUserKey(),
];

if (!empty($recipient->getDevice())) {
if (\count($recipient->getDevice()) > 1) {
throw new LogicException(sprintf('API can validate only 1 device at a time. "%s" devices provided.', \count($recipient->getDevice())));
if (!empty($recipient->getDevices())) {
if (\count($recipient->getDevices()) > 1) {
throw new LogicException(sprintf('API can validate only 1 device at a time. "%s" devices provided.', \count($recipient->getDevices())));
}

$curlPostFields['device'] = $recipient->getDeviceListCommaSeparated();
$curlPostFields['device'] = $recipient->getDevicesCommaSeparated();
}

return $curlPostFields;
Expand Down
37 changes: 29 additions & 8 deletions src/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Recipient
*
* @var array<string>
*/
private array $device = [];
private array $devices = [];

/**
* Used only when sending messages to a Group. If user is disabled in a group, they won't receive messages.
Expand Down Expand Up @@ -69,33 +69,54 @@ public function getUserKey(): string
}

/**
* @deprecated since 1.7.0, to be removed in 2.0. Use getDevices() instead.
*
* @return array<string>
*/
public function getDevice(): array
{
return $this->device;
@trigger_error(sprintf('Since serhiy/pushover 1.7.0: Method %s() is deprecated and will be removed in 2.0. Use %s() instead.', __METHOD__, 'getDevices()'), \E_USER_DEPRECATED);

return $this->getDevices();
}

/**
* Adds device to the device list / array.
* @return array<string>
*/
public function getDevices(): array
{
return $this->devices;
}

/**
* Adds device to the devices list / array.
*/
public function addDevice(string $device): void
{
if (1 !== preg_match('/^[a-zA-Z0-9_-]{1,25}$/', $device)) {
throw new InvalidArgumentException(sprintf('Device names are optional, may be up to 25 characters long, and will contain the character set [A-Za-z0-9_-]. "%s" given with "%s" characters."', $device, \strlen($device)));
}

if (!\in_array($device, $this->device, true)) {
$this->device[] = $device;
if (!\in_array($device, $this->devices, true)) {
$this->devices[] = $device;
}
}

/**
* Converts device array to comma separated list and returns it.
* @deprecated since 1.7.0, to be removed in 2.0. Use getDeviceListCommaSeparated() instead.
*
* Converts devices array to comma separated list and returns it.
*/
public function getDeviceListCommaSeparated(): string
{
return implode(',', $this->device);
@trigger_error(sprintf('Since serhiy/pushover 1.7.0: Method %s() is deprecated and will be removed in 2.0. Use %s() instead.', __METHOD__, 'getDevicesCommaSeparated()'), \E_USER_DEPRECATED);

return $this->getDevicesCommaSeparated();
}

public function getDevicesCommaSeparated(): string
{
return implode(',', $this->devices);
}

public function isDisabled(): bool
Expand All @@ -116,7 +137,7 @@ public function getMemo(): ?string
public function setMemo(?string $memo): void
{
if (\is_string($memo) && $length = \strlen($memo) > 200) {
throw new InvalidArgumentException('Memo contained '.$length.' characters. Memos are limited to 200 characters.');
throw new InvalidArgumentException(sprintf('Memo contained %s characters. Memos are limited to 200 characters.', $length));
}

$this->memo = $memo;
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/Response/RetrieveGroupResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function testGetUsers(RetrieveGroupResponse $response): void
$this->assertSame('aaaa1111AAAA1111bbbb2222BBBB22', $recipient->getUserKey());
$this->assertFalse($recipient->isDisabled());
$this->assertSame('This is a test memo', $recipient->getMemo());
$this->assertSame(['test-device-1'], $recipient->getDevice());
$this->assertSame(['test-device-1'], $recipient->getDevices());
}
}
21 changes: 21 additions & 0 deletions tests/RecipientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* file that was distributed with this source code.
*/

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Exception\InvalidArgumentException;
use Serhiy\Pushover\Recipient;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function testGetUserKey(): void
$this->assertSame($userKey, $recipient->getUserKey());
}

#[Group('legacy')]
public function testGetDevice(): void
{
$recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22');
Expand All @@ -62,6 +64,16 @@ public function testGetDevice(): void
$this->assertSame(['test-device-1', 'test-device-2'], $recipient->getDevice());
}

public function testGetDevices(): void
{
$recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22');
$recipient->addDevice('test-device-1');
$recipient->addDevice('test-device-2');

$this->assertSame(['test-device-1', 'test-device-2'], $recipient->getDevices());
}

#[Group('legacy')]
public function testGetDeviceListCommaSeparated(): void
{
$recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22');
Expand All @@ -71,6 +83,15 @@ public function testGetDeviceListCommaSeparated(): void
$this->assertSame('test-device-1,test-device-2', $recipient->getDeviceListCommaSeparated());
}

public function testGetDevicesCommaSeparated(): void
{
$recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22');
$recipient->addDevice('test-device-1');
$recipient->addDevice('test-device-2');

$this->assertSame('test-device-1,test-device-2', $recipient->getDevicesCommaSeparated());
}

public function testIsDisabledReturnsFalse(): void
{
$recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22');
Expand Down

0 comments on commit e1f2b27

Please sign in to comment.