Skip to content

Commit

Permalink
Merge pull request #2 from slunak/feature-add-sound-support
Browse files Browse the repository at this point in the history
Add sound support
  • Loading branch information
slunak authored Jun 7, 2020
2 parents ad30d27 + 1e9668b commit 4800d41
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Example/CompleteNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Serhiy\Pushover\Api\Message\Priority;
use Serhiy\Pushover\Api\Message\Recipient;
use Serhiy\Pushover\Api\Message\Response;
use Serhiy\Pushover\Api\Message\Sound;

/**
* Complete Notification Example.
Expand Down Expand Up @@ -48,6 +49,8 @@ public function completeNotification()

// create notification
$notification = new Notification($application, $recipient, $message);
// set notification sound
$notification->setSound(new Sound(Sound::PUSHOVER));

// push notification
/** @var Response $response */
Expand Down
4 changes: 4 additions & 0 deletions src/Api/Message/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ private function buildCurlPostFields(Notification $notification)
$curlPostFields['html'] = 1;
}

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

return $curlPostFields;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Api/Message/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Notification
*/
private $message;

/**
* @var Sound
*/
private $sound;

public function __construct(Application $application, Recipient $recipient, Message $message)
{
Expand Down Expand Up @@ -90,4 +94,20 @@ public function setMessage(Message $message): void
{
$this->message = $message;
}

/**
* @return Sound|null
*/
public function getSound(): ?Sound
{
return $this->sound;
}

/**
* @param Sound|null $sound
*/
public function setSound(?Sound $sound): void
{
$this->sound = $sound;
}
}
151 changes: 151 additions & 0 deletions src/Api/Message/Sound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?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 Sound
{
/** Pushover (default) */
const PUSHOVER = 'pushover';

/** Bike */
const BIKE = 'bike';

/** Bugle */
const BUGLE = 'bugle';

/** Cash Register */
const CASHREGISTER = 'cashregister';

/** Classical */
const CLASSICAL = 'classical';

/** Cosmic */
const COSMIC = 'cosmic';

/** Falling */
const FALLING = 'falling';

/** Gamelan */
const GAMELAN = 'gamelan';

/** Incoming */
const INCOMING = 'incoming';

/** Intermission */
const INTERMISSION = 'intermission';

/** Magic */
const MAGIC = 'magic';

/** Mechanical */
const MECHANICAL = 'mechanical';

/** Piano Bar */
const PIANOBAR = 'pianobar';

/** Siren */
const SIREN = 'siren';

/** Space Alarm */
const SPACEALARM = 'spacealarm';

/** Tug Boat */
const TUGBOAT = 'tugboat';

/** Alien Alarm (long) */
const ALIEN = 'alien';

/** Climb (long) */
const CLIMB = 'climb';

/** Persistent (long) */
const PERSISTENT = 'persistent';

/** Pushover Echo (long) */
const ECHO = 'echo';

/** Up Down (long) */
const UPDOWN = 'updown';

/** Vibrate Only */
const VIBRATE = 'vibrate';

/** None (silent) */
const NONE = 'none';

/**
* Notification Sound
* Users can choose from 22 different default sounds to play when receiving notifications, rather than our standard Pushover tone.
* Applications can override a user's default tone choice on a per-notification basis.
*
* @var string
*/
private $sound;

/**
* Array of all available sounds, auto generated from constants in this class when Sound object is instantiated.
*
* @var array
*/
private $availableSounds;


public function __construct(string $sound)
{
$this->availableSounds = $this->generateAvailableSounds();
$this->setSound($sound);
}

/**
* Generates array with all available sounds. Sounds are taken from the constants of this class.
*
* @return array
*/
private function generateAvailableSounds()
{
$oClass = new \ReflectionClass(__CLASS__);
return $oClass->getConstants();
}

/**
* @return array
*/
public function getAvailableSounds(): array
{
return $this->availableSounds;
}

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

/**
* @param string $sound
*/
public function setSound(string $sound): void
{
if (!in_array($sound, $this->availableSounds)) {
throw new InvalidArgumentException(sprintf('Sound "%s" is not available.', $sound));
}

$this->sound = $sound;
}
}
27 changes: 26 additions & 1 deletion tests/Api/Message/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
use Serhiy\Pushover\Api\Message\Message;
use Serhiy\Pushover\Api\Message\Notification;
use Serhiy\Pushover\Api\Message\Recipient;
use Serhiy\Pushover\Api\Message\Sound;

/**
* @author Serhiy Lunak
*/
class NotificationTest extends TestCase
{
public function testNotification()
public function testCanBeCreated()
{
$application = new Application("azGDORePK8gMaC0QOYAMyEEuzJnyUi"); // using dummy token
$recipient = new Recipient("uQiRzpo4DXghDmr9QzzfQu27cmVRsG"); // using dummy user key
Expand All @@ -32,5 +33,29 @@ public function testNotification()
$notification = new Notification($application, $recipient, $message);

$this->assertInstanceOf(Notification::class, $notification);

return $notification;
}

/**
* @depends testCanBeCreated
* @param Notification $notification
*/
public function testNotificationSound(Notification $notification)
{
$notification->setSound(new Sound(Sound::PUSHOVER));

$this->assertEquals('pushover', $notification->getSound()->getSound());
}

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

$this->assertNull($notification->getSound());
}
}
67 changes: 67 additions & 0 deletions tests/Api/Message/SoundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Sound;
use Serhiy\Pushover\Exception\InvalidArgumentException;

/**
* @author Serhiy Lunak
*/
class SoundTest extends TestCase
{
public function testCannotBeCreatedFromInvalidSound()
{
$this->expectException(InvalidArgumentException::class);

new Sound('invalid_sound');
}

public function testCanBeCreated()
{
$this->assertInstanceOf(Sound::class, $sound = new Sound(Sound::PUSHOVER));

return $sound;
}

/**
* @depends testCanBeCreated
* @param Sound $sound
*/
public function testGetSound(Sound $sound)
{
$this->assertEquals('pushover', $sound->getSound());
}

/**
* @depends testCanBeCreated
* @param Sound $sound
*/
public function testAvailableSounds(Sound $sound)
{
$availableSounds = new \ReflectionClass(Sound::class);

$this->assertEquals($availableSounds->getConstants(), $sound->getAvailableSounds());
}

/**
* @depends testCanBeCreated
* @param Sound $sound
*/
public function testSetSound(Sound $sound)
{
$sound->setSound(Sound::ECHO);

$this->assertEquals('echo', $sound->getSound());
}
}

0 comments on commit 4800d41

Please sign in to comment.