Skip to content

Commit

Permalink
Merge pull request #10 from genkgo/upgrade_81
Browse files Browse the repository at this point in the history
Upgrade 81
  • Loading branch information
frederikbosch authored Mar 11, 2022
2 parents 79e2413 + ae6147e commit b8205cb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0']
php: ['7.4', '8.0', '8.1']

name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v1
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => true,
'yoda_style' => false,
'native_function_invocation' => true,
'native_function_invocation' => ['include' => ['@all']],
'phpdoc_no_package' => true,
'no_empty_phpdoc' => true,
'no_blank_lines_after_class_opening' => true,
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name" : "genkgo/push",
"description": "Send push messages to Android and Apple using one interface.",
"require" : {
"php": "^7.4 || ~8.0.0",
"php": "^7.4 || ~8.0.0 || ~8.1.0",
"ext-json" : "*",
"apple/apn-push": "^3.0",
"guzzlehttp/guzzle": "^7.0",
Expand All @@ -12,7 +12,7 @@
"phpunit/phpunit": "^9",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"friendsofphp/php-cs-fixer": "^2.9"
"friendsofphp/php-cs-fixer": "^3.0"
},
"autoload" : {
"psr-4" : {
Expand Down
66 changes: 57 additions & 9 deletions src/Sender/FirebaseSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

namespace Genkgo\Push\Sender;

use Genkgo\Push\Exception\ForbiddenToSendMessageException;
use Genkgo\Push\Exception\InvalidMessageException;
use Genkgo\Push\Exception\InvalidRecipientException;
use Genkgo\Push\Exception\UnknownRecipientException;
use Genkgo\Push\Firebase\CloudMessaging;
use Genkgo\Push\Firebase\Notification;
use Genkgo\Push\Message;
use Genkgo\Push\Recipient\FirebaseRecipient;
use Genkgo\Push\RecipientInterface;
use Genkgo\Push\SenderInterface;
use GuzzleHttp\Exception\RequestException;

final class FirebaseSender implements SenderInterface
{
Expand Down Expand Up @@ -50,14 +55,57 @@ public function supports(Message $message, RecipientInterface $recipient): bool
*/
public function send(Message $message, RecipientInterface $recipient): void
{
$this->cloudMessaging->send(
$this->projectId,
$recipient->getToken(),
new Notification(
(string)$message->getBody(),
(string)$message->getTitle(),
$message->getExtra()
)
);
try {
$this->cloudMessaging->send(
$this->projectId,
$recipient->getToken(),
new Notification(
(string)$message->getBody(),
(string)$message->getTitle(),
$message->getExtra()
)
);
} catch (RequestException $e) {
$response = $e->getResponse();
if (!$response) {
throw $e;
}

$contentTypeHeader = $response->getHeaderLine('content-type');
if (\strpos($contentTypeHeader, 'application/json') === false) {
$error = $e->getMessage();
} else {
$responseText = (string)$response->getBody();
$responseJson = \json_decode($responseText, true);

if ($responseJson && isset($responseJson['error']['message'])) {
$error = $responseJson['error']['message'];
} else {
$error = $e->getMessage();
}
}

if ($response->getStatusCode() === 400) {
throw new InvalidRecipientException($error);
}

if ($response->getStatusCode() === 404) {
throw new UnknownRecipientException($error);
}

if ($response->getStatusCode() === 403) {
throw new ForbiddenToSendMessageException($error);
}

if ($response->getStatusCode() === 429) {
throw new ForbiddenToSendMessageException($error);
}

if ($response->getStatusCode() === 401) {
throw new InvalidMessageException($error);
}

throw $e;
}
}
}

0 comments on commit b8205cb

Please sign in to comment.