Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Messaging for outdated environments #908

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
7 changes: 5 additions & 2 deletions src/Firebase/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,15 @@ public function createMessaging(): Contract\Messaging
$projectId = $this->getProjectId();

$errorHandler = new MessagingApiExceptionConverter($this->clock);
$requestFactory = new Messaging\RequestFactory(
requestFactory: $this->httpFactory,
streamFactory: $this->httpFactory,
);

$messagingApiClient = new Messaging\ApiClient(
$this->createApiClient(),
$projectId,
$this->httpFactory,
$this->httpFactory,
$requestFactory,
);

$appInstanceApiClient = new AppInstanceApiClient(
Expand Down
28 changes: 2 additions & 26 deletions src/Firebase/Messaging/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

namespace Kreait\Firebase\Messaging;

use Beste\Json;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Pool;
use GuzzleHttp\Promise\PromiseInterface;
use Iterator;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* @internal
Expand All @@ -21,34 +18,13 @@
public function __construct(
private readonly ClientInterface $client,
private readonly string $projectId,
private readonly RequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly RequestFactory $requestFactory,
) {
}

public function createSendRequestForMessage(Message $message, bool $validateOnly): RequestInterface
{
$request = $this->requestFactory
->createRequest(
'POST',
'https://fcm.googleapis.com/v1/projects/'.$this->projectId.'/messages:send',
)
;

$payload = ['message' => $message];

if ($validateOnly === true) {
$payload['validate_only'] = true;
}

$body = $this->streamFactory->createStream(Json::encode($payload));

return $request
->withProtocolVersion('2.0')
->withBody($body)
->withHeader('Content-Type', 'application/json; charset=UTF-8')
->withHeader('Content-Length', (string) $body->getSize())
;
return $this->requestFactory->createRequest($message, $this->projectId, $validateOnly);

Check warning on line 27 in src/Firebase/Messaging/ApiClient.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/ApiClient.php#L27

Added line #L27 was not covered by tests
}

/**
Expand Down
74 changes: 74 additions & 0 deletions src/Firebase/Messaging/RequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Kreait\Firebase\Messaging;

use Beste\Json;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* @internal
*/
final class RequestFactory
{
private bool $environmentSupportsHTTP2;

public function __construct(
private readonly RequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
) {
$this->environmentSupportsHTTP2 = self::environmentSupportsHTTP2();
}

public function createRequest(Message $message, string $projectId, bool $validateOnly): RequestInterface

Check warning on line 26 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L26

Added line #L26 was not covered by tests
{
$request = $this->requestFactory
->createRequest(
'POST',
'https://fcm.googleapis.com/v1/projects/'.$projectId.'/messages:send',
)
;

Check warning on line 33 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L28-L33

Added lines #L28 - L33 were not covered by tests

if ($this->environmentSupportsHTTP2) {
$request = $request->withProtocolVersion('2.0');

Check warning on line 36 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L35-L36

Added lines #L35 - L36 were not covered by tests
}

$payload = ['message' => $message];

Check warning on line 39 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L39

Added line #L39 was not covered by tests

if ($validateOnly === true) {
$payload['validate_only'] = true;

Check warning on line 42 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L41-L42

Added lines #L41 - L42 were not covered by tests
}

$body = $this->streamFactory->createStream(Json::encode($payload));

Check warning on line 45 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L45

Added line #L45 was not covered by tests

return $request
->withBody($body)
->withHeader('Content-Type', 'application/json; charset=UTF-8')
->withHeader('Content-Length', (string) $body->getSize())
;

Check warning on line 51 in src/Firebase/Messaging/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/RequestFactory.php#L47-L51

Added lines #L47 - L51 were not covered by tests
}

/**
* @see https://github.com/microsoftgraph/msgraph-sdk-php/issues/854
* @see https://github.com/microsoftgraph/msgraph-sdk-php/pull/1120
*
* @codeCoverageIgnore
*/
private static function environmentSupportsHTTP2(): bool
{
if (!extension_loaded('curl')) {
return false;
}

if (!defined('CURL_VERSION_HTTP2')) {
return false;
}

$features = curl_version()["features"] ?? null;

return ($features & CURL_VERSION_HTTP2) === CURL_VERSION_HTTP2;
}
}