-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from slunak/feature-add-sound-support
Add sound support
- Loading branch information
Showing
6 changed files
with
271 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |