update v1.0.7.9 R.C.
This is a Release Candidate. We are still testing.
This commit is contained in:
71
vendor/brozot/laravel-fcm/src/Response/BaseResponse.php
vendored
Normal file
71
vendor/brozot/laravel-fcm/src/Response/BaseResponse.php
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
use LaravelFCM\Response\Exceptions\ServerResponseException;
|
||||
use LaravelFCM\Response\Exceptions\InvalidRequestException;
|
||||
use LaravelFCM\Response\Exceptions\UnauthorizedRequestException;
|
||||
|
||||
/**
|
||||
* Class BaseResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
abstract class BaseResponse {
|
||||
|
||||
const SUCCESS = 'success';
|
||||
const FAILURE = 'failure';
|
||||
const ERROR = "error";
|
||||
const MESSAGE_ID = "message_id";
|
||||
|
||||
/**
|
||||
* BaseResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$this->isJsonResponse($response);
|
||||
|
||||
$responseInJson = json_decode($response->getBody(), true);
|
||||
$this->parseResponse($responseInJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the response given by fcm is parsable
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*
|
||||
* @throws InvalidRequestException
|
||||
* @throws ServerResponseException
|
||||
* @throws UnauthorizedRequestException
|
||||
*/
|
||||
private function isJsonResponse(GuzzleResponse $response)
|
||||
{
|
||||
if ($response->getStatusCode() == 200) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() == 400) {
|
||||
throw new InvalidRequestException($response);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() == 401) {
|
||||
throw new UnauthorizedRequestException($response);
|
||||
}
|
||||
|
||||
throw new ServerResponseException($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the response
|
||||
*
|
||||
* @param array $responseInJson
|
||||
*/
|
||||
protected abstract function parseResponse($responseInJson);
|
||||
|
||||
/**
|
||||
* Log the response
|
||||
*/
|
||||
protected abstract function logResponse();
|
||||
|
||||
}
|
||||
410
vendor/brozot/laravel-fcm/src/Response/DownstreamResponse.php
vendored
Normal file
410
vendor/brozot/laravel-fcm/src/Response/DownstreamResponse.php
vendored
Normal file
@@ -0,0 +1,410 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class DownstreamResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
class DownstreamResponse extends BaseResponse {
|
||||
|
||||
const MULTICAST_ID = 'multicast_id';
|
||||
const CANONICAL_IDS = "canonical_ids";
|
||||
const RESULTS = "results";
|
||||
|
||||
const MISSING_REGISTRATION = "MissingRegistration";
|
||||
const MESSAGE_ID = "message_id";
|
||||
const REGISTRATION_ID = "registration_id";
|
||||
const NOT_REGISTERED = "NotRegistered";
|
||||
const INVALID_REGISTRATION = "InvalidRegistration";
|
||||
const UNAVAILABLE = "Unavailable";
|
||||
const DEVICE_MESSAGE_RATE_EXCEEDED = "DeviceMessageRateExceeded";
|
||||
const INTERNAL_SERVER_ERROR = "InternalServerError";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensSuccess = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensFailure = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokenModify = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var
|
||||
*/
|
||||
protected $messageId;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tokensToDelete = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
|
||||
protected $tokensToModify = [];
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
|
||||
protected $tokensToRetry = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tokensWithError = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $hasMissingToken = false;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $tokens;
|
||||
|
||||
/**
|
||||
* DownstreamResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
* @param $tokens
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response, $tokens)
|
||||
{
|
||||
$this->tokens = is_string($tokens) ? [$tokens] : $tokens;
|
||||
|
||||
parent::__construct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the response
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
protected function parseResponse($responseInJson)
|
||||
{
|
||||
$this->parse($responseInJson);
|
||||
|
||||
if ($this->needResultParsing($responseInJson)) {
|
||||
$this->parseResult($responseInJson);
|
||||
}
|
||||
|
||||
$this->logResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parse($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::MULTICAST_ID, $responseInJson)) {
|
||||
$this->messageId;
|
||||
}
|
||||
|
||||
if (array_key_exists(self::SUCCESS, $responseInJson)) {
|
||||
$this->numberTokensSuccess = $responseInJson[self::SUCCESS];
|
||||
}
|
||||
|
||||
if (array_key_exists(self::FAILURE, $responseInJson)) {
|
||||
$this->numberTokensFailure = $responseInJson[self::FAILURE];
|
||||
}
|
||||
|
||||
if (array_key_exists(self::CANONICAL_IDS, $responseInJson)) {
|
||||
$this->numberTokenModify = $responseInJson[self::CANONICAL_IDS];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseResult($responseInJson)
|
||||
{
|
||||
foreach ($responseInJson[self::RESULTS] as $index => $result) {
|
||||
|
||||
if (!$this->isSent($result)) {
|
||||
if (!$this->needToBeModify($index, $result)) {
|
||||
if (!$this->needToBeDeleted($index, $result) && !$this->needToResend($index, $result) && !$this->checkMissingToken($result)) {
|
||||
$this->needToAddError($index, $result);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $responseInJson
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needResultParsing($responseInJson)
|
||||
{
|
||||
return array_key_exists(self::RESULTS, $responseInJson) && ($this->numberTokensFailure > 0 || $this->numberTokenModify > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $results
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isSent($results)
|
||||
{
|
||||
return (array_key_exists(self::MESSAGE_ID, $results) && !array_key_exists(self::REGISTRATION_ID, $results));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needToBeModify($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::MESSAGE_ID, $result) && array_key_exists(self::REGISTRATION_ID, $result)) {
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensToModify[$this->tokens[$index]] = $result[self::REGISTRATION_ID];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needToBeDeleted($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $result) &&
|
||||
(in_array(self::NOT_REGISTERED, $result) || in_array(self::INVALID_REGISTRATION, $result))) {
|
||||
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensToDelete[] = $this->tokens[$index];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needToResend($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $result) && (in_array(self::UNAVAILABLE, $result) || in_array(self::DEVICE_MESSAGE_RATE_EXCEEDED, $result) || in_array(self::INTERNAL_SERVER_ERROR, $result))) {
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensToRetry[] = $this->tokens[$index];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function checkMissingToken($result)
|
||||
{
|
||||
$hasMissingToken = (array_key_exists(self::ERROR, $result) && in_array(self::MISSING_REGISTRATION, $result));
|
||||
|
||||
$this->hasMissingToken = (boolean) ($this->hasMissingToken | $hasMissingToken);
|
||||
|
||||
return $hasMissingToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*/
|
||||
private function needToAddError($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $result)) {
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensWithError[$this->tokens[$index]] = $result[self::ERROR];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function logResponse()
|
||||
{
|
||||
$logger = new Logger('Laravel-FCM');
|
||||
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
|
||||
|
||||
$logMessage = "notification send to ".count($this->tokens)." devices".PHP_EOL;
|
||||
$logMessage .= "success: ".$this->numberTokensSuccess.PHP_EOL;
|
||||
$logMessage .= "failures: ".$this->numberTokensFailure.PHP_EOL;
|
||||
$logMessage .= "number of modified token : ".$this->numberTokenModify.PHP_EOL;
|
||||
|
||||
$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge two response
|
||||
*
|
||||
* @param DownstreamResponse $response
|
||||
*/
|
||||
public function merge(DownstreamResponse $response)
|
||||
{
|
||||
$this->numberTokensSuccess += $response->numberSuccess();
|
||||
$this->numberTokensFailure += $response->numberFailure();
|
||||
$this->numberTokenModify += $response->numberModification();
|
||||
|
||||
$this->tokensToDelete = array_merge($this->tokensToDelete, $response->tokensToDelete());
|
||||
$this->tokensToModify = array_merge($this->tokensToModify, $response->tokensToModify());
|
||||
$this->tokensToRetry = array_merge($this->tokensToRetry, $response->tokensToRetry());
|
||||
$this->tokensWithError = array_merge($this->tokensWithError, $response->tokensWithError());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device reached with success
|
||||
* @return int
|
||||
*/
|
||||
public function numberSuccess()
|
||||
{
|
||||
return $this->numberTokensSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device which thrown an error
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numberFailure()
|
||||
{
|
||||
return $this->numberTokensFailure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device that you need to modify their token
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numberModification()
|
||||
{
|
||||
return $this->numberTokenModify;
|
||||
}
|
||||
|
||||
/**
|
||||
* get token to delete
|
||||
*
|
||||
* remove all tokens returned by this method in your database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensToDelete()
|
||||
{
|
||||
return $this->tokensToDelete;
|
||||
}
|
||||
|
||||
/**
|
||||
* get token to modify
|
||||
*
|
||||
* key: oldToken
|
||||
* value: new token
|
||||
*
|
||||
* find the old token in your database and replace it with the new one
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensToModify()
|
||||
{
|
||||
return $this->tokensToModify;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tokens that you should resend using exponential backoof
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensToRetry()
|
||||
{
|
||||
return $this->tokensToRetry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tokens that thrown an error
|
||||
*
|
||||
* key : token
|
||||
* value : error
|
||||
*
|
||||
* In production, remove these tokens from you database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensWithError()
|
||||
{
|
||||
return $this->tokensWithError;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if missing tokens was given to the request
|
||||
* If true, remove all the empty token in your database
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMissingToken()
|
||||
{
|
||||
return $this->hasMissingToken;
|
||||
}
|
||||
}
|
||||
25
vendor/brozot/laravel-fcm/src/Response/Exceptions/InvalidRequestException.php
vendored
Normal file
25
vendor/brozot/laravel-fcm/src/Response/Exceptions/InvalidRequestException.php
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php namespace LaravelFCM\Response\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class InvalidRequestException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class InvalidRequestException extends Exception {
|
||||
|
||||
/**
|
||||
* InvalidRequestException constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$code = $response->getStatusCode();
|
||||
$responseBody = $response->getBody()->getContents();
|
||||
|
||||
parent::__construct($responseBody, $code);
|
||||
}
|
||||
}
|
||||
36
vendor/brozot/laravel-fcm/src/Response/Exceptions/ServerResponseException.php
vendored
Normal file
36
vendor/brozot/laravel-fcm/src/Response/Exceptions/ServerResponseException.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php namespace LaravelFCM\Response\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class ServerResponseException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class ServerResponseException extends Exception {
|
||||
|
||||
/**
|
||||
* retry after
|
||||
* @var int
|
||||
*/
|
||||
public $retryAfter;
|
||||
|
||||
/**
|
||||
* ServerResponseException constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$code = $response->getStatusCode();
|
||||
$responseHeader = $response->getHeaders();
|
||||
$responseBody = $response->getBody()->getContents();
|
||||
|
||||
if (array_keys($responseHeader, "Retry-After")) {
|
||||
$this->retryAfter = $responseHeader["Retry-After"];
|
||||
}
|
||||
|
||||
parent::__construct($responseBody, $code);
|
||||
}
|
||||
}
|
||||
24
vendor/brozot/laravel-fcm/src/Response/Exceptions/UnauthorizedRequestException.php
vendored
Normal file
24
vendor/brozot/laravel-fcm/src/Response/Exceptions/UnauthorizedRequestException.php
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php namespace LaravelFCM\Response\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class UnauthorizedRequestException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class UnauthorizedRequestException extends Exception {
|
||||
|
||||
/**
|
||||
* UnauthorizedRequestException constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$code = $response->getStatusCode();
|
||||
|
||||
parent::__construct('FCM_SENDER_ID or FCM_SERVER_KEY are invalid', $code);
|
||||
}
|
||||
}
|
||||
137
vendor/brozot/laravel-fcm/src/Response/GroupResponse.php
vendored
Normal file
137
vendor/brozot/laravel-fcm/src/Response/GroupResponse.php
vendored
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class GroupResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
class GroupResponse extends BaseResponse {
|
||||
|
||||
const FAILED_REGISTRATION_IDS = "failed_registration_ids";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensSuccess = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensFailure = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $tokensFailed = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $to;
|
||||
|
||||
/**
|
||||
* GroupResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
* @param $to
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response, $to)
|
||||
{
|
||||
$this->to = $to;
|
||||
parent::__construct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the response
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
protected function parseResponse($responseInJson)
|
||||
{
|
||||
if ($this->parse($responseInJson)) {
|
||||
$this->parseFailed($responseInJson);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the response
|
||||
*/
|
||||
protected function logResponse()
|
||||
{
|
||||
$logger = new Logger('Laravel-FCM');
|
||||
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
|
||||
|
||||
$logMessage = "notification send to group: $this->to";
|
||||
$logMessage .= "with $this->numberTokensSuccess success and $this->Failure failure";
|
||||
|
||||
$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function parse($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::SUCCESS, $responseInJson)) {
|
||||
$this->numberTokensSuccess = $responseInJson[self::SUCCESS];
|
||||
}
|
||||
if (array_key_exists(self::FAILURE, $responseInJson)) {
|
||||
$this->numberTokensFailure = $responseInJson[self::FAILURE];
|
||||
}
|
||||
|
||||
return $this->numberTokensFailure > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseFailed($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::FAILED_REGISTRATION_IDS, $responseInJson)) {
|
||||
foreach ($responseInJson[self::FAILED_REGISTRATION_IDS] as $registrationId) {
|
||||
$this->tokensFailed[] = $registrationId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device reached with success
|
||||
* @return int
|
||||
*/
|
||||
public function numberSuccess()
|
||||
{
|
||||
return $this->numberTokensSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device which thrown an error
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numberFailure()
|
||||
{
|
||||
return $this->numberTokensFailure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all token in group that fcm cannot reach
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensFailed()
|
||||
{
|
||||
return $this->tokensFailed;
|
||||
}
|
||||
}
|
||||
142
vendor/brozot/laravel-fcm/src/Response/TopicResponse.php
vendored
Normal file
142
vendor/brozot/laravel-fcm/src/Response/TopicResponse.php
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use LaravelFCM\Message\Topics;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class TopicResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
class TopicResponse extends BaseResponse {
|
||||
|
||||
const LIMIT_RATE_TOPICS_EXCEEDED = "TopicsMessageRateExceeded";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $topic;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $messageId;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $needRetry = false;
|
||||
|
||||
/**
|
||||
* TopicResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
* @param Topics $topic
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response, Topics $topic)
|
||||
{
|
||||
$this->topic = $topic;
|
||||
parent::__construct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the response
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
protected function parseResponse($responseInJson)
|
||||
{
|
||||
if (!$this->parseSuccess($responseInJson)) {
|
||||
$this->parseError($responseInJson);
|
||||
}
|
||||
|
||||
$this->logResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseSuccess($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::MESSAGE_ID, $responseInJson)) {
|
||||
$this->messageId = $responseInJson[ self::MESSAGE_ID ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseError($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $responseInJson)) {
|
||||
if (in_array(self::LIMIT_RATE_TOPICS_EXCEEDED, $responseInJson)) {
|
||||
$this->needRetry = true;
|
||||
}
|
||||
|
||||
$this->error = $responseInJson[ self::ERROR ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the response
|
||||
*/
|
||||
protected function logResponse()
|
||||
{
|
||||
$logger = new Logger('Laravel-FCM');
|
||||
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
|
||||
|
||||
$topic = $this->topic->build();
|
||||
$logMessage = "notification send to topic: $topic";
|
||||
if ($this->messageId) {
|
||||
$logMessage .= "with success (message-id : $this->messageId)";
|
||||
}
|
||||
else {
|
||||
$logMessage .= "with error (error : $this->error)";
|
||||
}
|
||||
|
||||
$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* true if topic sent with success
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuccess()
|
||||
{
|
||||
return (bool) $this->messageId;
|
||||
}
|
||||
|
||||
/**
|
||||
* return error message
|
||||
* you should test if it's necessary to resent it
|
||||
*
|
||||
* @return string error
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if it's necessary resent it using exponential backoff
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldRetry()
|
||||
{
|
||||
return $this->needRetry;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user