Skip to content

Commit

Permalink
Check if HTTP/2 is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Jun 28, 2024
1 parent 8b5ffe8 commit 55aac0a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"ext-ctype": "*",
"ext-curl": "*",
"ext-filter": "*",
"ext-json": "*",
"ext-mbstring": "*",
Expand Down
23 changes: 22 additions & 1 deletion src/Firebase/Messaging/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
*/
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
Expand All @@ -29,6 +32,10 @@ public function createRequest(Message $message, string $projectId, bool $validat
)
;

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) {
Expand All @@ -38,10 +45,24 @@ public function createRequest(Message $message, string $projectId, bool $validat
$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
->withProtocolVersion('2.0')
->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
*/
private static function environmentSupportsHTTP2(): bool
{
$features = curl_version()["features"] ?? null;

return
extension_loaded('curl')
&& defined('CURL_VERSION_HTTP2')
&& ($features & CURL_VERSION_HTTP2) === CURL_VERSION_HTTP2
;
}
}

0 comments on commit 55aac0a

Please sign in to comment.