-
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 branch 'master' of https://github.com/slunak/pushover-php
- Loading branch information
Showing
7 changed files
with
348 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?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; | ||
|
||
/** | ||
* Pushover messages can include an image attachment. | ||
* When received by a device, it will attempt to download the image and display it with the notification. | ||
* If this fails or takes too long, the notification will be displayed without it and the image download can be retried inside the Pushover app. | ||
* Note that, like messages, once attachments are downloaded by the device, they are deleted from our servers and only stored on the device going forward. | ||
* Attachments uploaded for devices that are not running at least version 3.0 of our apps will be discarded as they cannot be displayed by those devices. | ||
* | ||
* @author Serhiy Lunak | ||
*/ | ||
class Attachment | ||
{ | ||
/** | ||
* Windows OS/2 Bitmap Graphics. | ||
*/ | ||
const MIME_TYPE_JPEG = 'image/jpeg'; | ||
|
||
/** | ||
* Portable Network Graphics. | ||
*/ | ||
const MIME_TYPE_PNG = 'image/png'; | ||
|
||
/** | ||
* Graphics Interchange Format (GIF). | ||
*/ | ||
const MIME_TYPE_GIF = 'image/gif'; | ||
|
||
/** | ||
* Windows OS/2 Bitmap Graphics | ||
*/ | ||
const MIME_TYPE_BMP = 'image/bmp'; | ||
|
||
/** | ||
* Icon format | ||
*/ | ||
const MIME_TYPE_ICO = 'image/vnd.microsoft.icon'; | ||
|
||
/** | ||
* Scalable Vector Graphics (SVG) | ||
*/ | ||
const MIME_TYPE_SVG = 'image/svg+xml'; | ||
|
||
/** | ||
* Tagged Image File Format (TIFF) | ||
*/ | ||
const MIME_TYPE_TIFF = 'image/tiff'; | ||
|
||
/** | ||
* WEBP image | ||
*/ | ||
const MIME_TYPE_WEBP = 'image/webp'; | ||
|
||
/** | ||
* MIME type. | ||
* A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) is a standard | ||
* that indicates the nature and format of a document, file, or assortment of bytes. | ||
* In case of Pushover attachment only image MIME type is accepted. | ||
* | ||
* @var string | ||
*/ | ||
private $mimeType; | ||
|
||
/** | ||
* Path to the file. | ||
* Path and filename of the image file to be sent with notification. | ||
* | ||
* @var string | ||
*/ | ||
private $filename; | ||
|
||
public function __construct(string $filename, string $mimeType) | ||
{ | ||
$this->setMimeType($mimeType); | ||
$this->setFilename($filename); | ||
} | ||
|
||
/** | ||
* Generates array with all supported attachment types. Attachment types are taken from the constants of this class. | ||
* | ||
* @return array | ||
*/ | ||
public function getSupportedAttachmentTypes(): array | ||
{ | ||
$oClass = new \ReflectionClass(__CLASS__); | ||
return $oClass->getConstants(); | ||
} | ||
|
||
/** | ||
* Supported extensions. | ||
* Returns array of supported extensions. | ||
* | ||
* @return array | ||
*/ | ||
public function getSupportedAttachmentExtensions(): array | ||
{ | ||
return array( | ||
'bmp', 'gif', 'ico', 'jpeg', 'jpg', 'png', 'svg', 'tif', 'tiff', 'webp' | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMimeType(): string | ||
{ | ||
return $this->mimeType; | ||
} | ||
|
||
/** | ||
* @param string $mimeType | ||
*/ | ||
public function setMimeType(string $mimeType): void | ||
{ | ||
if (!in_array($mimeType, $this->getSupportedAttachmentTypes())) { | ||
throw new InvalidArgumentException(sprintf('Attachment type "%s" is not supported.', $mimeType)); | ||
} | ||
|
||
$this->mimeType = $mimeType; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFilename(): string | ||
{ | ||
return $this->filename; | ||
} | ||
|
||
/** | ||
* @param string $filename | ||
*/ | ||
public function setFilename(string $filename): void | ||
{ | ||
if (!in_array(pathinfo($filename, PATHINFO_EXTENSION), $this->getSupportedAttachmentExtensions())) { | ||
throw new InvalidArgumentException(sprintf('Attachment extension "%s" is not supported.', pathinfo($filename, PATHINFO_EXTENSION))); | ||
} | ||
|
||
$this->filename = $filename; | ||
} | ||
} |
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,112 @@ | ||
<?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 Serhiy\Pushover\Api\Message\Attachment; | ||
use PHPUnit\Framework\TestCase; | ||
use Serhiy\Pushover\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Serhiy Lunak | ||
*/ | ||
class AttachmentTest extends TestCase | ||
{ | ||
public function testCanBeCreated() | ||
{ | ||
$attachment = new Attachment('/images/test.jpeg', Attachment::MIME_TYPE_JPEG); | ||
$this->assertInstanceOf(Attachment::class, $attachment); | ||
|
||
return $attachment; | ||
} | ||
|
||
public function testCannotBeCreatedWithInvalidMimeType() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
new Attachment('/images/test.jpeg', 'image/invalid'); | ||
} | ||
|
||
public function testCannotBeCreatedWithInvalidExtension() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
new Attachment('/images/test.invalid', Attachment::MIME_TYPE_JPEG); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param Attachment $attachment | ||
*/ | ||
public function testGetMimeType(Attachment $attachment) | ||
{ | ||
$this->assertEquals(Attachment::MIME_TYPE_JPEG, $attachment->getMimeType()); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param Attachment $attachment | ||
*/ | ||
public function testGetFilename(Attachment $attachment) | ||
{ | ||
$this->assertEquals('/images/test.jpeg', $attachment->getFilename()); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param Attachment $attachment | ||
*/ | ||
public function testSetMimeType(Attachment $attachment) | ||
{ | ||
$attachment->setMimeType(Attachment::MIME_TYPE_JPEG); | ||
$this->assertEquals(Attachment::MIME_TYPE_JPEG, $attachment->getMimeType()); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$attachment->setMimeType('image/invalid'); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param Attachment $attachment | ||
*/ | ||
public function testSetFilename(Attachment $attachment) | ||
{ | ||
$attachment->setMimeType(Attachment::MIME_TYPE_JPEG); | ||
$this->assertEquals(Attachment::MIME_TYPE_JPEG, $attachment->getMimeType()); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$attachment->setMimeType('image/invalid'); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param Attachment $attachment | ||
*/ | ||
public function testSupportedAttachmentTypes(Attachment $attachment) | ||
{ | ||
$supportedAttachmentsTypes = new \ReflectionClass(Attachment::class); | ||
|
||
$this->assertEquals($supportedAttachmentsTypes->getConstants(), $attachment->getSupportedAttachmentTypes()); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param Attachment $attachment | ||
*/ | ||
public function testSupportedAttachmentExtensions(Attachment $attachment) | ||
{ | ||
$supportedAttachmentExtensions = array( | ||
'bmp', 'gif', 'ico', 'jpeg', 'jpg', 'png', 'svg', 'tif', 'tiff', 'webp' | ||
); | ||
|
||
$this->assertEquals($supportedAttachmentExtensions, $attachment->getSupportedAttachmentExtensions()); | ||
} | ||
} |
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
Oops, something went wrong.