<?php declare(strict_types=1); namespace Kreait\Firebase; use Beste\Json; use Kreait\Firebase\Exception\RemoteConfig\VersionNotFound; use Kreait\Firebase\RemoteConfig\ApiClient; use Kreait\Firebase\RemoteConfig\FindVersions; use Kreait\Firebase\RemoteConfig\Template; use Kreait\Firebase\RemoteConfig\Version; use Kreait\Firebase\RemoteConfig\VersionNumber; use Psr\Http\Message\ResponseInterface; use Traversable; use function array_shift; use function is_string; /** * @internal * * @phpstan-import-type RemoteConfigTemplateShape from Template */ final class RemoteConfig implements Contract\RemoteConfig { public function __construct(private readonly ApiClient $client) { } public function get(): Template { return $this->buildTemplateFromResponse($this->client->getTemplate()); } public function validate($template): void { $this->client->validateTemplate($this->ensureTemplate($template)); } public function publish($template): string { $etag = $this->client ->publishTemplate($this->ensureTemplate($template)) ->getHeader('ETag'); $etag = array_shift($etag); if (!is_string($etag)) { return '*'; } if ($etag === '') { return '*'; } return $etag; } public function getVersion(VersionNumber|int|string $versionNumber): Version { $versionNumber = $this->ensureVersionNumber($versionNumber); foreach ($this->listVersions() as $version) { if ($version->versionNumber()->equalsTo($versionNumber)) { return $version; } } throw VersionNotFound::withVersionNumber($versionNumber); } public function rollbackToVersion(VersionNumber|int|string $versionNumber): Template { $versionNumber = $this->ensureVersionNumber($versionNumber); return $this->buildTemplateFromResponse($this->client->rollbackToVersion($versionNumber)); } public function listVersions($query = null): Traversable { $query = $query instanceof FindVersions ? $query : FindVersions::fromArray((array) $query); $pageToken = null; $count = 0; $limit = $query->limit(); do { $response = $this->client->listVersions($query, $pageToken); $result = Json::decode((string) $response->getBody(), true); foreach ((array) ($result['versions'] ?? []) as $versionData) { ++$count; yield Version::fromArray($versionData); if ($count === $limit) { return; } } $pageToken = $result['nextPageToken'] ?? null; } while ($pageToken); } /** * @param Template|RemoteConfigTemplateShape $value */ private function ensureTemplate($value): Template { return $value instanceof Template ? $value : Template::fromArray($value); } /** * @param VersionNumber|positive-int|non-empty-string $value */ private function ensureVersionNumber(VersionNumber|int|string $value): VersionNumber { return $value instanceof VersionNumber ? $value : VersionNumber::fromValue($value); } private function buildTemplateFromResponse(ResponseInterface $response): Template { $etagHeader = $response->getHeader('ETag'); $etag = array_shift($etagHeader) ?: '*'; $data = Json::decode((string) $response->getBody(), true); return Template::fromArray($data, $etag); } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
AppCheck | Folder | 0755 |
|
|
Auth | Folder | 0755 |
|
|
Contract | Folder | 0755 |
|
|
Database | Folder | 0755 |
|
|
DynamicLink | Folder | 0755 |
|
|
Exception | Folder | 0755 |
|
|
Http | Folder | 0755 |
|
|
Messaging | Folder | 0755 |
|
|
RemoteConfig | Folder | 0755 |
|
|
Request | Folder | 0755 |
|
|
Util | Folder | 0755 |
|
|
Value | Folder | 0755 |
|
|
AppCheck.php | File | 1.32 KB | 0644 |
|
Auth.php | File | 22.53 KB | 0644 |
|
Database.php | File | 2.2 KB | 0644 |
|
DynamicLink.php | File | 2.04 KB | 0644 |
|
DynamicLinks.php | File | 5.26 KB | 0644 |
|
Factory.php | File | 20.57 KB | 0644 |
|
Firestore.php | File | 558 B | 0644 |
|
Messaging.php | File | 7.69 KB | 0644 |
|
RemoteConfig.php | File | 3.52 KB | 0644 |
|
Request.php | File | 131 B | 0644 |
|
Storage.php | File | 1.18 KB | 0644 |
|
Util.php | File | 1.55 KB | 0644 |
|