Skip to content

Commit

Permalink
Merge pull request #3 from slunak/refactor-response
Browse files Browse the repository at this point in the history
Refactor response
  • Loading branch information
slunak authored Jun 18, 2020
2 parents 5da6a88 + 7237469 commit e1e559d
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 81 deletions.
28 changes: 26 additions & 2 deletions Example/ResponseExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Serhiy\Pushover\Api\Message\Message;
use Serhiy\Pushover\Api\Message\Notification;
use Serhiy\Pushover\Api\Message\Recipient;
use Serhiy\Pushover\Api\Message\Request;
use Serhiy\Pushover\Api\Message\Response;

/**
Expand Down Expand Up @@ -46,21 +47,29 @@ public function responseExample()
*/
$response = $client->push($notification);

/**
* True if request was successful, false otherwise. Reflects $requestStatus property.
*
* @var bool
*/
$response->isSuccessful();

/**
* Status returned by Pushover API.
* Either 1 if successful or something other than 1 if unsuccessful.
* Reflects $isSuccessful property.
*
* @var int
*/
$response->getStatus();
$response->getRequestStatus();

/**
* Request returned by Pushover API.
* Randomly-generated unique token that associated with your Pushover request.
*
* @var string
*/
$response->getRequest();
$response->getRequestToken();

/**
* Receipt.
Expand All @@ -87,5 +96,20 @@ public function responseExample()
* @var mixed
*/
$response->getCurlResponse();

/**
* Object Containing request.
* It contains notification object, which in turn contains application, recipient and message objects.
* It also contains array for CURLOPT_POSTFIELDS curl argument and API URL.
*
* @var Request
*/
$request = $response->getRequest();
$request->getNotification(); // Notification object
$request->getNotification()->getApplication();
$request->getNotification()->getRecipient();
$request->getNotification()->getMessage();
$request->getCurlPostFields(); // array, array for CURLOPT_POSTFIELDS curl argument
$request->getFullUrl(); // string, API URL
}
}
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ composer require "serhiy/pushover"
Instantiate the client, application and recipient of the notification:

```php
use Serhiy\Pushover\Api\Message\Client;
use Serhiy\Pushover\Api\Message\Application;
use Serhiy\Pushover\Api\Message\Recipient;

$client = new Client();
$application = new Application("replace_with_pushover_application_api_token");
$recipient = new Recipient("replace_with_pushover_user_key");
Expand All @@ -47,26 +51,73 @@ Or use Dependency Injection to inject them into the services of your app.
Compose a message:

```php
use Serhiy\Pushover\Api\Message\Message;

$message = new Message("This is a test message", "This is a title of the message");
```

Create notification:

```php
use Serhiy\Pushover\Api\Message\Notification;

$notification = new Notification($application, $recipient, $message);
```
And push it:
Push it:

```php
/** @var Response $response */
/** @var \Serhiy\Pushover\Api\Message\Response $response */
$response = $client->push($notification);
```

Client returns Response object which contains the status, request ID, receipt if any and errors array.
## Working with response

*Note: For complete example refer to [ResponseExample.php](Example/ResponseExample.php)*

Client returns Response object. Checking if the message was accepted is easy:

```php
if ($response->isSuccessful()) {
// ...
}
```

One can get status and token returned by Pushover:

```php
$response->getRequestStatus();
$response->getRequestToken();
```

Or even unmodified json response from the API (json_decode into an array if needed):

```php
$response->getCurlResponse();
```

Response also contains original Request object:

```php
/** @var \Serhiy\Pushover\Api\Message\Request $request */
$request = $response->getRequest();
```

Request contains original notification object, which in turn contains application, recipient and message objects.

```php
/** @var \Serhiy\Pushover\Api\Message\Notification $notification */
$notification = $request->getNotification(); // Notification object
$notification->getApplication();
$notification->getRecipient();
$notification->getMessage();
```

As well as an array for CURLOPT_POSTFIELDS curl argument and full API URL.

```php
var_dump($response);
$request->getCurlPostFields();
$request->getFullUrl();
```

For more code examples, see `Example` folder in the root of the project. You may also generate and see code documentation.
Expand Down
107 changes: 45 additions & 62 deletions src/Api/Message/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ class Client extends ApiClient
*/
const API_PATH = 'messages.json';

/**
* @var Request
*/
private $request;

/**
* @var Response
*/
private $response;

public function __construct()
{
$this->request = new Request(self::API_BASE_URL, self::API_VERSION, self::API_PATH);
$this->response = new Response();
}

/**
Expand All @@ -37,10 +49,27 @@ public function __construct()
* @return Response
*/
public function push(Notification $notification)
{
$this->request->setCurlPostFields($notification);
$this->request->setNotification($notification);

$curlResponse = $this->doCurl();

$this->processCurlResponse($curlResponse);

$this->response->setRequest($this->request);

return $this->response;
}

/**
* @return mixed
*/
private function doCurl()
{
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => self::API_BASE_URL.'/'.self::API_VERSION.'/'.self::API_PATH,
CURLOPT_POSTFIELDS => $this->buildCurlPostFields($notification),
CURLOPT_URL => $this->request->getFullUrl(),
CURLOPT_POSTFIELDS => $this->request->getCurlPostFields(),
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_RETURNTRANSFER => true,
));
Expand All @@ -56,82 +85,36 @@ public function push(Notification $notification)
throw new LogicException('Curl should return json encoded string because CURLOPT_RETURNTRANSFER is set, "true" returned instead.');
}

return $this->processResponse($curlResponse);
}

/**
* Builds array for CURLOPT_POSTFIELDS curl argument.
*
* @param Notification $notification
* @return array
*/
private function buildCurlPostFields(Notification $notification)
{
$curlPostFields = array(
"token" => $notification->getApplication()->getToken(),
"user" => $notification->getRecipient()->getUserKey(),
"message" => $notification->getMessage()->getMessage(),
"timestamp" => $notification->getMessage()->getTimestamp(),
);

if (null !== $notification->getRecipient()->getDevice()) {
$curlPostFields['device'] = $notification->getRecipient()->getDeviceListCommaSeparated();
}

if (null !== $notification->getMessage()->getTitle()) {
$curlPostFields['title'] = $notification->getMessage()->getTitle();
}

if (null !== $notification->getMessage()->getUrl()) {
$curlPostFields['url'] = $notification->getMessage()->getUrl();
}

if (null !== $notification->getMessage()->getUrlTitle()) {
$curlPostFields['url_title'] = $notification->getMessage()->getUrlTitle();
}

if (null !== $notification->getMessage()->getPriority()) {
$curlPostFields['priority'] = $notification->getMessage()->getPriority()->getPriority();

if (Priority::EMERGENCY == $notification->getMessage()->getPriority()->getPriority()) {
$curlPostFields['retry'] = $notification->getMessage()->getPriority()->getRetry();
$curlPostFields['expire'] = $notification->getMessage()->getPriority()->getExpire();
}
}

if (true === $notification->getMessage()->getIsHtml()) {
$curlPostFields['html'] = 1;
}

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

return $curlPostFields;
return $curlResponse;
}

/**
* Processes curl response and returns Response object.
*
* @param mixed $curlResponse
* @return Response
* @return void
*/
private function processResponse($curlResponse)
private function processCurlResponse($curlResponse)
{
$decodedCurlResponse = json_decode($curlResponse);

$response = new Response($decodedCurlResponse->status, $decodedCurlResponse->request);
$this->response->setRequestStatus($decodedCurlResponse->status);
$this->response->setRequestToken($decodedCurlResponse->request);
$this->response->setCurlResponse($curlResponse);

$response->setCurlResponse($curlResponse);
if ($this->response->getRequestStatus() == 1) {
$this->response->setIsSuccessful(true);
}

if ($response->getStatus() != 1) {
$response->setErrors($decodedCurlResponse->errors);
if ($this->response->getRequestStatus() != 1) {
$this->response->setErrors($decodedCurlResponse->errors);
$this->response->setIsSuccessful(false);
}

if (isset($decodedCurlResponse->receipt)) {
$response->setReceipt($curlResponse->receipt);
$this->response->setReceipt($curlResponse->receipt);
}

return $response;
return;
}
}
Loading

0 comments on commit e1e559d

Please sign in to comment.