Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require 'vendor/autoload.php';

use LeonardoTeixeira\Pushover\Client;
use LeonardoTeixeira\Pushover\Message;
use LeonardoTeixeira\Pushover\Glances;
use LeonardoTeixeira\Pushover\Exceptions\PushoverException;

$client = new Client('YOUR_USER_CODE_HERE', 'YOUR_TOKEN_HERE');
Expand All @@ -52,6 +53,17 @@ try {
} catch (PushoverException $e) {
echo 'ERROR: ', $e->getMessage(), PHP_EOL;
}

$glances = new Glances();

$glances.setPercent(50); // Update percent field

try {
$client->updateGlances($glances);
echo 'Glances has been updated!', PHP_EOL;
} catch (PushoverException $e) {
echo 'ERROR: ', $e->getMessage(), PHP_EOL;
}
```
You also can pass a title and the priority on constructor:

Expand All @@ -69,6 +81,7 @@ date_default_timezone_set('UTC');

use LeonardoTeixeira\Pushover\Client;
use LeonardoTeixeira\Pushover\Message;
use LeonardoTeixeira\Pushover\Glances;
use LeonardoTeixeira\Pushover\Priority;
use LeonardoTeixeira\Pushover\Sound;
use LeonardoTeixeira\Pushover\Exceptions\PushoverException;
Expand All @@ -95,6 +108,20 @@ try {
} catch (PushoverException $e) {
echo 'ERROR: ', $e->getMessage(), PHP_EOL;
}
$glances = new Glances();
$glances->setTitle('Title here');
$glances->setText('Text here');
$glances->setSubtext('Subtext here');
$glances->setCount(123);
$glances->setPercent(100);

try {
$receipt = $client->updateGlances($glances);
echo 'Glances has been updated!', PHP_EOL;
$status = $client->poll($receipt);
} catch (PushoverException $e) {
echo 'ERROR: ', $e->getMessage(), PHP_EOL;
}
```

### Emergency Priority
Expand Down
84 changes: 76 additions & 8 deletions src/LeonardoTeixeira/Pushover/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Client
private $token;

const API_MESSAGE_URL = 'https://api.pushover.net/1/messages.json';
const API_GLANCES_URL = 'https://api.pushover.net/1/glances.json';
const API_RECEIPTS_URL = 'https://api.pushover.net/1/receipts';

public function __construct($user = null, $token = null)
Expand Down Expand Up @@ -150,15 +151,15 @@ public function poll(Receipt $receipt)
if (! $receipt instanceof Receipt ) {
throw new PushoverException('The parameter \'$receipt\' must be a Receipt instance.');
}

if(is_null($receipt->getReceipt())) {
throw new PushoverException('The receipt content was not set.');
throw new PushoverException('The receipt content was not set.');
}

try {
$request = Requests::get(self::API_RECEIPTS_URL.'/'.$receipt->getReceipt().'.json?token='.$this->token, []);
$responseJson = json_decode($request->body, true);

if (!isset($responseJson['status']) || $responseJson['status'] != 1) {
if (isset($responseJson['errors'])) {
throw new PushoverException($responseJson['errors'][0]);
Expand All @@ -177,15 +178,15 @@ public function cancel(Receipt $receipt)
if (! $receipt instanceof Receipt ) {
throw new PushoverException('The parameter \'$receipt\' must be a Receipt instance.');
}

if(is_null($receipt->getReceipt())) {
throw new PushoverException('The receipt content was not set.');
throw new PushoverException('The receipt content was not set.');
}

try {
$request = Requests::post(self::API_RECEIPTS_URL.'/'.$receipt->getReceipt().'/cancel.json', [], ['token' => $this->token]);
$responseJson = json_decode($request->body, true);

if (!isset($responseJson['status']) || $responseJson['status'] != 1) {
if (isset($responseJson['errors'])) {
throw new PushoverException($responseJson['errors'][0]);
Expand All @@ -195,6 +196,73 @@ public function cancel(Receipt $receipt)
}
} catch (\Exception $e) {
throw new PushoverException($e->getMessage());
}
}
}
}

public function updateGlances(Glances $glances, $device = null)
{
if (!$glances->hasTitle() && !$glances->hasText() && !$glances->hasSubtext() && !$glances->hasCount() && !$glances->hasPercent()) {
throw new PushoverException('The parameter \'$glances\' must have at least one parameter');
}

$postData = [
'user' => $this->user,
'token' => $this->token
];

if ($device != null) {
$postData['device'] = $device;
}

if ($glances->hasTitle()) {
$postData['title'] = $glances->getTitle();
}

if ($glances->hasText()) {
$postData['text'] = $glances->getText();
}

if ($glances->hasSubtext()) {
$postData['subtext'] = $glances->getSubtext();
}

if ($glances->hasCount()) {
$postData['count'] = $glances->getCount();
}

if ($glances->hasPercent()) {
$postData['percent'] = $glances->getPercent();
}

try {
// Using hooks is an ugly hack since we bypass the fancy request API
// Up to no, Requests doesn't support multi-part headers :-/
//
// Should we switch to guzzle/guzzle ?
$hooks = new Requests_Hooks();
$hooks->register('curl.before_send', function($fp) use ($postData) {
curl_setopt($fp, CURLOPT_POSTFIELDS, $postData);
$postData = [];
});
$hooks = ['hooks' => $hooks];

$request = Requests::post(self::API_GLANCES_URL, [], $postData, $hooks);
$responseJson = json_decode($request->body);

if (!isset($responseJson->status) || $responseJson->status != 1) {
if (isset($responseJson->errors)) {
throw new PushoverException($responseJson->errors[0]);
} else {
throw new PushoverException('Unable to access the Pushover API.');
}
}
if(isset($responseJson->receipt)) {
return new Receipt($responseJson->receipt);
}
return new Receipt();

} catch (\Exception $e) {
throw new PushoverException($e->getMessage());
}
}
}
101 changes: 101 additions & 0 deletions src/LeonardoTeixeira/Pushover/Glances.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace LeonardoTeixeira\Pushover;

use LeonardoTeixeira\Pushover\Exceptions\InvalidArgumentException;

class Glances
{
private $title; //(100 characters) - a description of the data being shown, such as "Widgets Sold"
private $text; //(100 characters) - the main line of data, used on most screens
private $subtext; //(100 characters) - a second line of data
private $count; //(integer, may be negative) - shown on smaller screens; useful for simple counts
private $percent; //(integer 0 through 100, inclusive) - shown on some screens as a progress bar/circle

public function __construct()
{
$this->title = null;
$this->text = null;
$this->subtext = null;
$this->count = null;
$this->percent = null;
}

public function getTitle()
{
return $this->title;
}

public function getText()
{
return $this->text;
}

public function getSubtext()
{
return $this->subtext;
}

public function getCount()
{
return $this->count;
}

public function getPercent()
{
return $this->percent;
}

public function setTitle($title)
{
$this->title = $title;
}

public function setText($text)
{
$this->text = $text;
}

public function setSubtext($subtext)
{
$this->subtext = $subtext;
}

public function setCount($count)
{
$this->count = $count;
}

public function setPercent($percent)
{
if ($percent < 0 || $percent > 100) {
throw new InvalidArgumentException('The percent value \'' . $percent . '\' is out of range.');
}
$this->percent = $percent;
}

public function hasTitle()
{
return !is_null($this->title);
}

public function hasText()
{
return !is_null($this->text);
}

public function hasSubtext()
{
return !is_null($this->subtext);
}

public function hasCount()
{
return !is_null($this->count);
}

public function hasPercent()
{
return !is_null($this->percent);
}
}