From ebfda10c2a93896b34b3df812e359a944610960a Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 09:35:56 +0100 Subject: [PATCH] #167 - updates to work with Buzz v0.17+, PSR-7 requests & responses, and middleware over listeners --- Service/OS/AndroidGCMNotification.php | 32 ++++++++++++++++++--------- Service/OS/AndroidNotification.php | 22 +++++++++--------- Service/OS/BlackberryNotification.php | 24 ++++++++++---------- Service/OS/MicrosoftNotification.php | 14 +++++++----- 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/Service/OS/AndroidGCMNotification.php b/Service/OS/AndroidGCMNotification.php index 69a1c1a..e8b82b6 100644 --- a/Service/OS/AndroidGCMNotification.php +++ b/Service/OS/AndroidGCMNotification.php @@ -2,14 +2,15 @@ namespace RMS\PushNotificationsBundle\Service\OS; -use Psr\Log\LoggerInterface; -use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, - RMS\PushNotificationsBundle\Message\AndroidMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; use Buzz\Browser, Buzz\Client\AbstractCurl, Buzz\Client\Curl, Buzz\Client\MultiCurl; +use Nyholm\Psr7\Request; +use Psr\Log\LoggerInterface; +use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, + RMS\PushNotificationsBundle\Message\AndroidMessage, + RMS\PushNotificationsBundle\Message\MessageInterface; class AndroidGCMNotification implements OSNotificationServiceInterface { @@ -63,6 +64,13 @@ class AndroidGCMNotification implements OSNotificationServiceInterface */ protected $logger; + /** + * Buzz request client options as associative array. + * + * @var array + */ + protected $clientOptions; + /** * Constructor * @@ -77,13 +85,15 @@ public function __construct($apiKey, $useMultiCurl, $timeout, $logger, AbstractC { $this->useDryRun = $dryRun; $this->apiKey = $apiKey; + // We'll need to set this per-request if `$client` was provided in constructor. + $this->clientOptions = array( + 'timeout' => $timeout, + 'verify_peer' => false, + ); if (!$client) { - $client = ($useMultiCurl ? new MultiCurl() : new Curl()); + $client = ($useMultiCurl ? new MultiCurl($this->clientOptions) : new Curl($this->clientOptions)); } - $client->setTimeout($timeout); - $this->browser = new Browser($client); - $this->browser->getClient()->setVerifyPeer(false); $this->logger = $logger; } @@ -122,14 +132,16 @@ public function send(MessageInterface $message) if (count($message->getGCMIdentifiers()) == 1) { $data['to'] = $gcmIdentifiers[0]; - $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); + $request = new Request('POST', $this->apiURL, $headers, json_encode($data)); + $this->responses[] = $this->browser->getClient()->sendRequest($request, $this->clientOptions); } else { // Chunk number of registration IDs according to the maximum allowed by GCM $chunks = array_chunk($message->getGCMIdentifiers(), $this->registrationIdMaxCount); foreach ($chunks as $registrationIDs) { $data['registration_ids'] = $registrationIDs; - $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); + $request = new Request('POST', $this->apiURL, $headers, json_encode($data)); + $this->responses[] = $this->browser->getClient()->sendRequest($request, $this->clientOptions); } } diff --git a/Service/OS/AndroidNotification.php b/Service/OS/AndroidNotification.php index 2e35ea4..c19e40d 100644 --- a/Service/OS/AndroidNotification.php +++ b/Service/OS/AndroidNotification.php @@ -2,6 +2,7 @@ namespace RMS\PushNotificationsBundle\Service\OS; +use Buzz\Client\Curl; use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, RMS\PushNotificationsBundle\Message\AndroidMessage, RMS\PushNotificationsBundle\Message\MessageInterface; @@ -32,11 +33,11 @@ class AndroidNotification implements OSNotificationServiceInterface protected $source; /** - * Timeout in seconds for the connecting client + * Buzz request client options as associative array. * - * @var int + * @var array */ - protected $timeout; + protected $clientOptions; /** * Authentication token @@ -51,14 +52,17 @@ class AndroidNotification implements OSNotificationServiceInterface * @param $username * @param $password * @param $source - * @param $timeout + * @param int $timeout Timeout in seconds */ public function __construct($username, $password, $source, $timeout) { $this->username = $username; $this->password = $password; $this->source = $source; - $this->timeout = $timeout; + $this->clientOptions = array( + 'timeout' => $timeout, + 'verify_peer' => false, + ); $this->authToken = ""; } @@ -80,9 +84,7 @@ public function send(MessageInterface $message) $headers[] = "Authorization: GoogleLogin auth=" . $this->authToken; $data = $message->getMessageBody(); - $buzz = new Browser(); - $buzz->getClient()->setVerifyPeer(false); - $buzz->getClient()->setTimeout($this->timeout); + $buzz = new Browser(new Curl($this->clientOptions)); $response = $buzz->post("https://android.apis.google.com/c2dm/send", $headers, http_build_query($data)); return preg_match("/^id=/", $response->getContent()) > 0; @@ -106,9 +108,7 @@ protected function getAuthToken() "service" => "ac2dm" ); - $buzz = new Browser(); - $buzz->getClient()->setVerifyPeer(false); - $buzz->getClient()->setTimeout($this->timeout); + $buzz = new Browser(new Curl($this->clientOptions)); $response = $buzz->post("https://www.google.com/accounts/ClientLogin", array(), http_build_query($data)); if ($response->getStatusCode() !== 200) { return false; diff --git a/Service/OS/BlackberryNotification.php b/Service/OS/BlackberryNotification.php index acc4b4c..49ec354 100644 --- a/Service/OS/BlackberryNotification.php +++ b/Service/OS/BlackberryNotification.php @@ -2,13 +2,14 @@ namespace RMS\PushNotificationsBundle\Service\OS; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerInterface, + Psr\Http\Message\ResponseInterface; use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, RMS\PushNotificationsBundle\Message\BlackberryMessage, RMS\PushNotificationsBundle\Message\MessageInterface; use Buzz\Browser, - Buzz\Listener\BasicAuthListener, - Buzz\Client\Curl; + Buzz\Client\Curl, + Buzz\Middleware\BasicAuthMiddleware; class BlackberryNotification implements OSNotificationServiceInterface { @@ -91,10 +92,8 @@ protected function doSend(BlackberryMessage $message) { $separator = "mPsbVQo0a68eIL3OAxnm"; $body = $this->constructMessageBody($message, $separator); - $browser = new Browser(new Curl()); - $browser->getClient()->setTimeout($this->timeout); - $listener = new BasicAuthListener($this->appID, $this->password); - $browser->addListener($listener); + $browser = new Browser(new Curl(array('timeout' => $this->timeout))); + $browser->addMiddleware(new BasicAuthMiddleware($this->appID, $this->password)); $url = "https://pushapi.na.blackberry.com/mss/PD_pushRequest"; if ($this->evaluation) { @@ -113,7 +112,7 @@ protected function doSend(BlackberryMessage $message) /** * Builds the actual body of the message * - * @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message + * @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message * @param $separator * @return string */ @@ -143,16 +142,17 @@ protected function constructMessageBody(BlackberryMessage $message, $separator) * Handles and parses the response * Returns a value indicating success/fail * - * @param \Buzz\Message\Response $response + * @param ResponseInterface $response * @return bool */ - protected function parseResponse(\Buzz\Message\Response $response) + protected function parseResponse(ResponseInterface $response) { - if (null !== $response->getStatusCode() && $response->getStatusCode() != 200) { + if (null !== $response->getStatusCode() && $response->getStatusCode() !== 200) { return false; } + $response->getBody()->rewind(); $doc = new \DOMDocument(); - $doc->loadXML($response->getContent()); + $doc->loadXML($response->getBody()->getContents()); $elems = $doc->getElementsByTagName("response-result"); if (!$elems->length) { $this->logger->error('Response is empty'); diff --git a/Service/OS/MicrosoftNotification.php b/Service/OS/MicrosoftNotification.php index 204b1f0..5d9e103 100644 --- a/Service/OS/MicrosoftNotification.php +++ b/Service/OS/MicrosoftNotification.php @@ -31,9 +31,11 @@ class MicrosoftNotification implements OSNotificationServiceInterface */ public function __construct($timeout, $logger) { - $this->browser = new Browser(new Curl()); - $this->browser->getClient()->setVerifyPeer(false); - $this->browser->getClient()->setTimeout($timeout); + $options = array( + 'timeout' => $timeout, + 'verify_peer' => false, + ); + $this->browser = new Browser(new Curl($options)); $this->logger = $logger; } @@ -61,10 +63,12 @@ public function send(MessageInterface $message) $response = $this->browser->post($message->getDeviceIdentifier(), $headers, $xml->asXML()); - if (!$response->isSuccessful()) { + if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { $this->logger->error($response->getStatusCode(). ' : '. $response->getReasonPhrase()); + + return false; } - return $response->isSuccessful(); + return true; } }