From 99291572d61e84400dd42b9906a901dbc564f9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gamez?= Date: Mon, 26 Jun 2023 14:37:25 +0200 Subject: [PATCH] Remove obsolete internal classes, methods and dependencies --- CHANGELOG.md | 21 +++ composer.json | 3 +- src/Firebase/Factory.php | 2 - src/Firebase/Http/HasSubRequests.php | 13 -- src/Firebase/Http/HasSubResponses.php | 13 -- src/Firebase/Http/Middleware.php | 21 --- src/Firebase/Http/RequestWithSubRequests.php | 101 ------------- src/Firebase/Http/Requests.php | 53 ------- .../Http/ResponseWithSubResponses.php | 94 ------------- src/Firebase/Http/Responses.php | 35 ----- src/Firebase/Http/WrappedPsr7Request.php | 133 ------------------ src/Firebase/Http/WrappedPsr7Response.php | 104 -------------- .../Messaging/Http/Request/MessageRequest.php | 15 -- .../Messaging/Http/Request/SendMessage.php | 46 ------ .../Http/Request/SendMessageToTokens.php | 44 ------ .../Messaging/Http/Request/SendMessages.php | 47 ------- .../Messaging/MulticastSendReport.php | 2 - 17 files changed, 22 insertions(+), 725 deletions(-) delete mode 100644 src/Firebase/Http/HasSubRequests.php delete mode 100644 src/Firebase/Http/HasSubResponses.php delete mode 100644 src/Firebase/Http/RequestWithSubRequests.php delete mode 100644 src/Firebase/Http/Requests.php delete mode 100644 src/Firebase/Http/ResponseWithSubResponses.php delete mode 100644 src/Firebase/Http/Responses.php delete mode 100644 src/Firebase/Http/WrappedPsr7Request.php delete mode 100644 src/Firebase/Http/WrappedPsr7Response.php delete mode 100644 src/Firebase/Messaging/Http/Request/MessageRequest.php delete mode 100644 src/Firebase/Messaging/Http/Request/SendMessage.php delete mode 100644 src/Firebase/Messaging/Http/Request/SendMessageToTokens.php delete mode 100644 src/Firebase/Messaging/Http/Request/SendMessages.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fd0977146..497d9747d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,27 @@ * Simplified convoluted Dynamic Link operations ([#810](https://github.com/kreait/firebase-php/pull/810) +### Removed + +* Removed obsolete internal classes + * `Kreait\Firebase\Http\HasSubRequests` + * `Kreait\Firebase\Http\HasSubResponses` + * `Kreait\Firebase\Http\Requests` + * `Kreait\Firebase\Http\RequestWithSubRequests` + * `Kreait\Firebase\Http\Responses` + * `Kreait\Firebase\Http\ResponseWithSubResponses` + * `Kreait\Firebase\Http\WrappedPsr7Response` + * `Kreait\Firebase\Http\WrappedPsr7Request` + * `Kreait\Firebase\Messaging\Http\Request\MessageRequest` + * `Kreait\Firebase\Messaging\Http\Request\SendMessage` + * `Kreait\Firebase\Messaging\Http\Request\SendMessageToTokens` + * `Kreait\Firebase\Messaging\Http\Request\SendMessages` + +* Removed obsolete internal methods + * `Kreait\Firebase\Http\Middleware::responseWithSubResponses()` + +* Removed obsolete Composer dependency `riverline/multipart-parser` + ## [7.4.0] - 2023-06-18 ### Added diff --git a/composer.json b/composer.json index 43638e47f..899b4e8e4 100644 --- a/composer.json +++ b/composer.json @@ -40,8 +40,7 @@ "lcobucci/jwt": "^4.3.0|^5.0", "mtdowling/jmespath.php": "^2.6.1", "psr/cache": "^1.0.1|^2.0|^3.0", - "psr/log": "^1.1|^2.0|^3.0", - "riverline/multipart-parser": "^2.0.9" + "psr/log": "^1.1|^2.0|^3.0" }, "require-dev": { "beste/php-cs-fixer-config": "^2.2", diff --git a/src/Firebase/Factory.php b/src/Firebase/Factory.php index 802422e0a..d8304a43d 100644 --- a/src/Firebase/Factory.php +++ b/src/Firebase/Factory.php @@ -549,8 +549,6 @@ public function createApiClient(?array $config = null, ?array $middlewares = nul $handler->push(new AuthTokenMiddleware($credentials, $authTokenHandler)); - $handler->push(Middleware::responseWithSubResponses()); - $config['handler'] = $handler; $config['auth'] = 'google_auth'; diff --git a/src/Firebase/Http/HasSubRequests.php b/src/Firebase/Http/HasSubRequests.php deleted file mode 100644 index 3b5f1ff97..000000000 --- a/src/Firebase/Http/HasSubRequests.php +++ /dev/null @@ -1,13 +0,0 @@ - static fn (RequestInterface $request, ?array $options = null) => $handler($request, $options ?: []) - ->then(static function (ResponseInterface $response) { - $isMultiPart = mb_stristr($response->getHeaderLine('Content-Type'), 'multipart') !== false; - $hasMultipleStartLines = ((int) preg_match_all('@http/[\S]+\s@i', (string) $response->getBody())) >= 1; - - if ($isMultiPart && $hasMultipleStartLines) { - return new ResponseWithSubResponses($response); - } - - return $response; - }) - ; - } - public static function log(LoggerInterface $logger, MessageFormatter $formatter, string $logLevel, string $errorLogLevel): callable { return static fn (callable $handler) => static fn ($request, array $options) => $handler($request, $options)->then( diff --git a/src/Firebase/Http/RequestWithSubRequests.php b/src/Firebase/Http/RequestWithSubRequests.php deleted file mode 100644 index 06bcad84f..000000000 --- a/src/Firebase/Http/RequestWithSubRequests.php +++ /dev/null @@ -1,101 +0,0 @@ -boundary = sha1(uniqid('', true)); - - $headers = [ - 'Content-Type' => 'multipart/mixed; boundary='.$this->boundary, - ]; - - $this->body = new AppendStream(); - - foreach ($subRequests as $request) { - $this->appendPartForSubRequest($request); - } - $this->appendStream("--{$this->boundary}--"); - - $request = new Request($this->method, $uri, $headers, $this->body, $version); - $contentLength = $request->getBody()->getSize(); - - if ($contentLength !== null) { - $request = $request->withHeader('Content-Length', (string) $contentLength); - } - - $this->wrappedRequest = $request; - } - - public function subRequests(): Requests - { - return $this->subRequests; - } - - private function appendPartForSubRequest(RequestInterface $subRequest): void - { - $this->appendStream("--{$this->boundary}\r\n"); - $this->appendStream($this->subRequestHeadersAsString($subRequest)."\r\n\r\n"); - $this->appendStream("{$subRequest->getMethod()} {$subRequest->getRequestTarget()} HTTP/{$subRequest->getProtocolVersion()}\r\n\r\n"); - $this->appendStream($subRequest->getBody()."\r\n"); - } - - private function appendStream(string $value): void - { - $this->body->addStream(Utils::streamFor($value)); - } - - private function subRequestHeadersAsString(RequestInterface $request): string - { - $headerNames = array_keys($request->getHeaders()); - - $headers = []; - - foreach ($headerNames as $name) { - if (mb_strtolower($name) === 'host') { - continue; - } - $headers[] = "{$name}: {$request->getHeaderLine($name)}"; - } - - return implode("\r\n", $headers); - } -} diff --git a/src/Firebase/Http/Requests.php b/src/Firebase/Http/Requests.php deleted file mode 100644 index 4d0ec0339..000000000 --- a/src/Firebase/Http/Requests.php +++ /dev/null @@ -1,53 +0,0 @@ - - */ -final class Requests implements IteratorAggregate -{ - /** - * @var RequestInterface[] - */ - private readonly array $requests; - - public function __construct(RequestInterface ...$requests) - { - $this->requests = $requests; - } - - public function findByContentId(string $contentId): ?RequestInterface - { - foreach ($this->requests as $request) { - $contentIdHeader = $request->getHeaderLine('Content-ID'); - $contentIdHeaderParts = explode('-', $contentIdHeader); - $requestContentId = array_pop($contentIdHeaderParts); - - if ($contentId === $requestContentId) { - return $request; - } - } - - return null; - } - - /** - * @return Traversable|RequestInterface[] - */ - public function getIterator(): Traversable - { - yield from $this->requests; - } -} diff --git a/src/Firebase/Http/ResponseWithSubResponses.php b/src/Firebase/Http/ResponseWithSubResponses.php deleted file mode 100644 index 7f257f198..000000000 --- a/src/Firebase/Http/ResponseWithSubResponses.php +++ /dev/null @@ -1,94 +0,0 @@ -wrappedResponse = $response; - $this->subResponses = $this->getSubResponsesFromResponse($response); - } - - public function subResponses(): Responses - { - return $this->subResponses; - } - - private function getSubResponsesFromResponse(ResponseInterface $response): Responses - { - try { - $parser = PSR7::convert($response); - } catch (Throwable) { - return new Responses(); - } - - if (!$parser->isMultiPart()) { - return new Responses(); - } - - $subResponses = []; - - foreach ($parser->getParts() as $part) { - $partHeaders = $part->getHeaders(); - - $realPartStream = fopen('php://temp', 'rwb'); - - if (!$realPartStream) { - continue; - } - - fwrite($realPartStream, $part->getBody()); - rewind($realPartStream); - $realPart = new StreamedPart($realPartStream); - - $headers = $realPart->getHeaders(); - $headerKeys = array_keys($headers); - // The first header is not a header, it's the start line of a HTTP response - $startLine = (string) array_shift($headerKeys); - array_shift($headers); - - if (preg_match('@^http/(?P[\S]+)\s(?P\d{3})\s(?P.+)$@i', $startLine, $startLineMatches) !== 1) { - throw new InvalidArgumentException('At least one sub response does not contain a start line'); - } - - $subResponse = new Response( - (int) $startLineMatches['status'], - $headers, - $realPart->getBody(), - $startLineMatches['version'], - $startLineMatches['reason'], - ); - - foreach ($partHeaders as $name => $value) { - $subResponse = $subResponse->withAddedHeader($name, $value); - } - - $subResponses[] = $subResponse; - } - - return new Responses(...$subResponses); - } -} diff --git a/src/Firebase/Http/Responses.php b/src/Firebase/Http/Responses.php deleted file mode 100644 index b99ca40ef..000000000 --- a/src/Firebase/Http/Responses.php +++ /dev/null @@ -1,35 +0,0 @@ - - */ -final class Responses implements IteratorAggregate -{ - /** - * @var ResponseInterface[] - */ - private readonly array $responses; - - public function __construct(ResponseInterface ...$responses) - { - $this->responses = $responses; - } - - /** - * @return Traversable|ResponseInterface[] - */ - public function getIterator(): Traversable - { - yield from $this->responses; - } -} diff --git a/src/Firebase/Http/WrappedPsr7Request.php b/src/Firebase/Http/WrappedPsr7Request.php deleted file mode 100644 index 35a8f71b7..000000000 --- a/src/Firebase/Http/WrappedPsr7Request.php +++ /dev/null @@ -1,133 +0,0 @@ -wrappedRequest->getProtocolVersion(); - } - - public function withProtocolVersion($version): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withProtocolVersion($version); - - return $request; - } - - public function getHeaders(): array - { - return $this->wrappedRequest->getHeaders(); - } - - public function hasHeader($name): bool - { - return $this->wrappedRequest->hasHeader($name); - } - - public function getHeader($name): array - { - return $this->wrappedRequest->getHeader($name); - } - - public function getHeaderLine($name): string - { - return $this->wrappedRequest->getHeaderLine($name); - } - - public function withHeader($name, $value): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withHeader($name, $value); - - return $request; - } - - public function withAddedHeader($name, $value): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withAddedHeader($name, $value); - - return $request; - } - - public function withoutHeader($name): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withoutHeader($name); - - return $request; - } - - public function getBody(): StreamInterface - { - return $this->wrappedRequest->getBody(); - } - - public function withBody(StreamInterface $body): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withBody($body); - - return $request; - } - - public function getRequestTarget(): string - { - return $this->wrappedRequest->getRequestTarget(); - } - - public function withRequestTarget($requestTarget): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withRequestTarget($requestTarget); - - return $request; - } - - public function getMethod(): string - { - return $this->wrappedRequest->getMethod(); - } - - public function withMethod($method): self - { - $request = clone $this; - $request->wrappedRequest = $this->wrappedRequest->withMethod($method); - - return $request; - } - - public function getUri(): UriInterface - { - return $this->wrappedRequest->getUri(); - } - - public function withUri(UriInterface $uri, $preserveHost = false): self - { - $request = clone $this; - $request->wrappedRequest->withUri($uri, $preserveHost); - - return $request; - } - - public function subRequests(): Requests - { - return $this->wrappedRequest instanceof HasSubRequests - ? $this->wrappedRequest->subRequests() - : new Requests(); - } -} diff --git a/src/Firebase/Http/WrappedPsr7Response.php b/src/Firebase/Http/WrappedPsr7Response.php deleted file mode 100644 index 2cf6ad89e..000000000 --- a/src/Firebase/Http/WrappedPsr7Response.php +++ /dev/null @@ -1,104 +0,0 @@ -wrappedResponse->getProtocolVersion(); - } - - public function withProtocolVersion($version): self - { - $response = clone $this; - $response->wrappedResponse = $this->wrappedResponse->withProtocolVersion($version); - - return $response; - } - - public function getHeaders(): array - { - return $this->wrappedResponse->getHeaders(); - } - - public function hasHeader($name): bool - { - return $this->wrappedResponse->hasHeader($name); - } - - public function getHeader($name): array - { - return $this->wrappedResponse->getHeader($name); - } - - public function getHeaderLine($name): string - { - return $this->wrappedResponse->getHeaderLine($name); - } - - public function withHeader($name, $value): self - { - $response = clone $this; - $response->wrappedResponse = $this->wrappedResponse->withHeader($name, $value); - - return $response; - } - - public function withAddedHeader($name, $value): self - { - $response = clone $this; - $response->wrappedResponse = $this->wrappedResponse->withAddedHeader($name, $value); - - return $response; - } - - public function withoutHeader($name): self - { - $response = clone $this; - $response->wrappedResponse = $this->wrappedResponse->withoutHeader($name); - - return $response; - } - - public function getBody(): StreamInterface - { - return $this->wrappedResponse->getBody(); - } - - public function withBody(StreamInterface $body): self - { - $response = clone $this; - $response->wrappedResponse = $this->wrappedResponse->withBody($body); - - return $response; - } - - public function getStatusCode(): int - { - return $this->wrappedResponse->getStatusCode(); - } - - public function withStatus($code, $reasonPhrase = ''): self - { - $response = clone $this; - $response->wrappedResponse = $this->wrappedResponse->withStatus($code, $reasonPhrase); - - return $response; - } - - public function getReasonPhrase(): string - { - return $this->wrappedResponse->getReasonPhrase(); - } -} diff --git a/src/Firebase/Messaging/Http/Request/MessageRequest.php b/src/Firebase/Messaging/Http/Request/MessageRequest.php deleted file mode 100644 index 9482e77b2..000000000 --- a/src/Firebase/Messaging/Http/Request/MessageRequest.php +++ /dev/null @@ -1,15 +0,0 @@ - $message]; - - if ($validateOnly === true) { - $payload['validate_only'] = true; - } - - $body = Utils::streamFor(Json::encode($payload)); - $headers = [ - 'Content-Type' => 'application/json; charset=UTF-8', - 'Content-Length' => (string) $body->getSize(), - ]; - - $this->wrappedRequest = new Request('POST', $uri, $headers, $body); - $this->message = $message; - } - - public function message(): Message - { - return $this->message; - } -} diff --git a/src/Firebase/Messaging/Http/Request/SendMessageToTokens.php b/src/Firebase/Messaging/Http/Request/SendMessageToTokens.php deleted file mode 100644 index 9291e9ebb..000000000 --- a/src/Firebase/Messaging/Http/Request/SendMessageToTokens.php +++ /dev/null @@ -1,44 +0,0 @@ -count() > Messaging::BATCH_MESSAGE_LIMIT) { - throw new InvalidArgument('A multicast message can be sent to a maximum amount of '.Messaging::BATCH_MESSAGE_LIMIT.' tokens.'); - } - - $messageData = Json::decode(Json::encode($message), true); - unset($messageData['topic'], $messageData['condition']); - - $messages = []; - - foreach ($registrationTokens as $token) { - $messageData['token'] = $token->value(); - - $messages[] = new RawMessageFromArray($messageData); - } - - $this->wrappedRequest = new SendMessages($projectId, new Messages(...$messages), $validateOnly); - } -} diff --git a/src/Firebase/Messaging/Http/Request/SendMessages.php b/src/Firebase/Messaging/Http/Request/SendMessages.php deleted file mode 100644 index 10be2d1c1..000000000 --- a/src/Firebase/Messaging/Http/Request/SendMessages.php +++ /dev/null @@ -1,47 +0,0 @@ -count() > Messaging::BATCH_MESSAGE_LIMIT) { - throw new InvalidArgument('Only '.Messaging::BATCH_MESSAGE_LIMIT.' can be sent at a time.'); - } - - $subRequests = []; - - $index = 0; - - foreach ($messages as $message) { - $subRequests[] = (new SendMessage($projectId, $message, $validateOnly)) - // see https://github.com/firebase/firebase-admin-node/blob/master/src/messaging/batch-request.ts#L104 - ->withHeader('Content-ID', (string) ++$index) - ->withHeader('Content-Transfer-Encoding', 'binary') - ->withHeader('Content-Type', 'application/http') - ; - } - - $this->wrappedRequest = new RequestWithSubRequests( - 'https://fcm.googleapis.com/batch', - new Requests(...$subRequests), - ); - } -} diff --git a/src/Firebase/Messaging/MulticastSendReport.php b/src/Firebase/Messaging/MulticastSendReport.php index 12c2bde0a..8cb4e0459 100644 --- a/src/Firebase/Messaging/MulticastSendReport.php +++ b/src/Firebase/Messaging/MulticastSendReport.php @@ -8,10 +8,8 @@ use function array_filter; use function array_map; -use function array_pop; use function array_values; use function count; -use function explode; final class MulticastSendReport implements Countable {