Skip to content

Commit

Permalink
Add custom sound support
Browse files Browse the repository at this point in the history
  • Loading branch information
slunak committed Aug 2, 2021
1 parent fe65047 commit ef20814
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Example/CompleteNotificationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Serhiy\Pushover\Example;

use Serhiy\Pushover\Api\Message\Attachment;
use Serhiy\Pushover\Api\Message\CustomSound;
use Serhiy\Pushover\Api\Message\Message;
use Serhiy\Pushover\Api\Message\Notification;
use Serhiy\Pushover\Api\Message\Priority;
Expand Down Expand Up @@ -48,8 +49,10 @@ public function completeNotification()

// create notification
$notification = new Notification($application, $recipient, $message);
// set notification sound
// set notification built-in sound
$notification->setSound(new Sound(Sound::PUSHOVER));
// or set notification custom sound
$notification->setCustomSound(new CustomSound("door_open"));
// add attachment
$notification->setAttachment(new Attachment("/path/to/file.jpg", Attachment::MIME_TYPE_JPEG));

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Light, simple and fast, yet comprehensive wrapper for the [Pushover](https://pus
- HTML messages
- Supplementary URL and its title
- Notification priority
- Notification sound
- Notification sound (including custom sound)
- Message time
- User/Group Validation API ([Example](Example/UserGroupValidationExample.php))
- Validation by user or group key
Expand Down
60 changes: 60 additions & 0 deletions src/Api/Message/CustomSound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\Api\Message;

use Serhiy\Pushover\Exception\InvalidArgumentException;

/**
* @author Serhiy Lunak
*/
class CustomSound
{
/**
* Custom Sound Notification
* Admins can now upload custom sounds through Pushover website and have them played on devices
* without having to copy sound files to each device. Can only contain letters, numbers, underscores, and dashes,
* and is limited to 20 characters, such as "warning", "door_open", or "long_siren2". It cannot match the name
* of any built-in sound and will be specified as the sound parameter through our API when requesting this sound.
*
* @var string
*/
private $customSound;

public function __construct(string $customSound)
{
$this->setCustomSound($customSound);
}

/**
* @return string
*/
public function getCustomSound(): string
{
return $this->customSound;
}

/**
* @param string $customSound
*/
public function setCustomSound(string $customSound): void
{
if (in_array($customSound, Sound::getAvailableSounds())) {
throw new InvalidArgumentException(sprintf('Sound "%s" is not a valid custom sound because it matches the name of a built-in sound.', $customSound));
}

if (1 !== preg_match("/^[a-zA-Z0-9_-]{1,20}$/", $customSound)) {
throw new InvalidArgumentException(sprintf('Sound "%s" is not a valid custom sound. Custom sound name can only contain letters, numbers, underscores, and dashes, and is limited to 20 characters, such as "warning", "door_open", or "long_siren2".', $customSound));
}

$this->customSound = $customSound;
}
}
21 changes: 21 additions & 0 deletions src/Api/Message/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Notification
*/
private $sound;

/**
* @var CustomSound|null
*/
private $customSound;

/**
* @var Attachment|null
*/
Expand Down Expand Up @@ -123,6 +128,22 @@ public function setSound(?Sound $sound): void
$this->sound = $sound;
}

/**
* @return CustomSound|null
*/
public function getCustomSound(): ?CustomSound
{
return $this->customSound;
}

/**
* @param CustomSound|null $customSound
*/
public function setCustomSound(?CustomSound $customSound): void
{
$this->customSound = $customSound;
}

/**
* @return Attachment|null
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Client/MessageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function buildCurlPostFields(Notification $notification): array
$curlPostFields['sound'] = $notification->getSound()->getSound();
}

if (null !== $notification->getCustomSound()) {
$curlPostFields['sound'] = $notification->getCustomSound()->getCustomSound();
}

if (null !== $notification->getAttachment()) {
if (! is_readable($notification->getAttachment()->getFilename())) {
throw new LogicException(sprintf('File "%s" does not exist or is not readable.', $notification->getAttachment()->getFilename()));
Expand Down
90 changes: 90 additions & 0 deletions tests/Api/Message/CustomSoundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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 Api\Message;

use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Api\Message\CustomSound;
use Serhiy\Pushover\Exception\InvalidArgumentException;

/**
* @author Serhiy Lunak
*/
class CustomSoundTest extends TestCase
{
public function testCanBeCreated()
{
$this->assertInstanceOf(CustomSound::class, $customSound = new CustomSound("door_open"));

return $customSound;
}

/**
* @depends testCanBeCreated
* @param CustomSound $customSound
*/
public function testGetCustomSound(CustomSound $customSound)
{
$this->assertEquals('door_open', $customSound->getCustomSound());
}

/**
* @depends testCanBeCreated
* @param CustomSound $customSound
*/
public function testSetCustomSound(CustomSound $customSound)
{
$customSound->setCustomSound("warning");

$this->assertEquals('warning', $customSound->getCustomSound());

$customSound->setCustomSound("door_open");

$this->assertEquals('door_open', $customSound->getCustomSound());

$customSound->setCustomSound("bell-sound");

$this->assertEquals('bell-sound', $customSound->getCustomSound());
}

/**
* @depends testCanBeCreated
* @param CustomSound $customSound
*/
public function testSetExistingCustomSound(CustomSound $customSound)
{
$this->expectException(InvalidArgumentException::class);

$customSound->setCustomSound("echo");
}

/**
* @depends testCanBeCreated
* @param CustomSound $customSound
*/
public function testSetInvalidCustomSound(CustomSound $customSound)
{
$this->expectException(InvalidArgumentException::class);

$customSound->setCustomSound("warning+door_open");
}

/**
* @depends testCanBeCreated
* @param CustomSound $customSound
*/
public function testSetLongCustomSound(CustomSound $customSound)
{
$this->expectException(InvalidArgumentException::class);

$customSound->setCustomSound("warning_door_open_warning_door_open");
}
}
23 changes: 23 additions & 0 deletions tests/Api/Message/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Serhiy\Pushover\Api\Message\Attachment;
use Serhiy\Pushover\Api\Message\CustomSound;
use Serhiy\Pushover\Api\Message\Message;
use Serhiy\Pushover\Api\Message\Notification;
use Serhiy\Pushover\Api\Message\Sound;
Expand Down Expand Up @@ -61,6 +62,28 @@ public function testSetSoundNull(Notification $notification)
$this->assertNull($notification->getSound());
}

/**
* @depends testCanBeCreated
* @param Notification $notification
*/
public function testSetCustomSound(Notification $notification)
{
$notification->setCustomSound(new CustomSound("door_open"));

$this->assertEquals('door_open', $notification->getCustomSound()->getCustomSound());
}

/**
* @depends testCanBeCreated
* @param Notification $notification
*/
public function testSetCustomSoundNull(Notification $notification)
{
$notification->setCustomSound(null);

$this->assertNull($notification->getCustomSound());
}

/**
* @depends testCanBeCreated
* @param Notification $notification
Expand Down

0 comments on commit ef20814

Please sign in to comment.