Skip to content

Commit

Permalink
Add support for rolloutValue fields in remote config parameters (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez authored Sep 10, 2024
1 parent dd9560b commit 603bcc4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ Please read about the future of the Firebase Admin PHP SDK on the

## [Unreleased]

### Added

* Added support for [rollout parameter values](https://firebase.google.com/docs/reference/remote-config/rest/v1/RemoteConfig#RolloutValue)
in Remote Config Templates.
([#923](https://github.com/kreait/firebase-php/pull/923)), ([#927](https://github.com/kreait/firebase-php/pull/927))
* Please note that it's not (yet?) possible to create rollouts programmatically via the Firebase API. This means that
you have to manually create a rollout in the Firebase console to be able to reference it in the Remote Config
template. Rollout IDs are named `rollout_<number>`, and you can find the ID in the URL after clicking on a rollout in the list.

## [7.14.0] - 2024-08-21

### Added
Expand Down
20 changes: 19 additions & 1 deletion src/Firebase/RemoteConfig/ParameterValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@

/**
* @phpstan-import-type RemoteConfigPersonalizationValueShape from PersonalizationValue
* @phpstan-import-type RemoteConfigRolloutValueShape from RolloutValue
*
* @phpstan-type RemoteConfigParameterValueShape array{
* value?: string,
* useInAppDefault?: bool,
* personalizationValue?: RemoteConfigPersonalizationValueShape
* personalizationValue?: RemoteConfigPersonalizationValueShape,
* rolloutValue?: RemoteConfigRolloutValueShape
* }
*
* @see https://firebase.google.com/docs/reference/remote-config/rest/v1/RemoteConfig#remoteconfigparametervalue
*/
final class ParameterValue implements JsonSerializable
{
private function __construct(
private readonly ?string $value = null,
private readonly ?bool $useInAppDefault = null,
private readonly ?PersonalizationValue $personalizationValue = null,
private readonly ?RolloutValue $rolloutValue = null,
) {
}

Expand All @@ -41,6 +46,11 @@ public static function withPersonalizationValue(PersonalizationValue $value): se
return new self(personalizationValue: $value);
}

public static function withRolloutValue(RolloutValue $value): self
{
return new self(rolloutValue: $value);
}

/**
* @param RemoteConfigParameterValueShape $data
*/
Expand All @@ -58,6 +68,10 @@ public static function fromArray(array $data): self
return self::withPersonalizationValue(PersonalizationValue::fromArray($data['personalizationValue']));
}

if (array_key_exists('rolloutValue', $data)) {
return self::withRolloutValue(RolloutValue::fromArray($data['rolloutValue']));
}

return new self();
}

Expand All @@ -78,6 +92,10 @@ public function toArray(): array
return ['personalizationValue' => $this->personalizationValue->toArray()];
}

if ($this->rolloutValue !== null) {
return ['rolloutValue' => $this->rolloutValue->toArray()];
}

return [];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Firebase/RemoteConfig/PersonalizationValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @phpstan-type RemoteConfigPersonalizationValueShape array{
* personalizationId: string
* }
*
* @see https://firebase.google.com/docs/reference/remote-config/rest/v1/RemoteConfig#personalizationvalue
*/
final class PersonalizationValue implements JsonSerializable
{
Expand Down
50 changes: 50 additions & 0 deletions src/Firebase/RemoteConfig/RolloutValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Kreait\Firebase\RemoteConfig;

use JsonSerializable;

/**
* @phpstan-type RemoteConfigRolloutValueShape array{
* rolloutId: non-empty-string,
* value: string,
* percent: int<0, 100>
* }
*
* @see https://firebase.google.com/docs/reference/remote-config/rest/v1/RemoteConfig#rolloutvalue
*/
final class RolloutValue implements JsonSerializable
{
/**
* @param RemoteConfigRolloutValueShape $data
*/
private function __construct(private readonly array $data)
{
}

/**
* @param RemoteConfigRolloutValueShape $data
*/
public static function fromArray(array $data): self
{
return new self($data);
}

/**
* @return RemoteConfigRolloutValueShape
*/
public function toArray(): array
{
return $this->data;
}

/**
* @return RemoteConfigRolloutValueShape
*/
public function jsonSerialize(): array
{
return $this->data;
}
}
21 changes: 20 additions & 1 deletion tests/Integration/RemoteConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ final class RemoteConfigTest extends IntegrationTestCase
"name": "lang_french",
"expression": "device.language in ['fr', 'fr_CA', 'fr_CH']",
"tagColor": "GREEN"
},
{
"name": "user_exists",
"expression": "true",
"tagColor": "TEAL"
}
],
"parameters": {
Expand All @@ -63,7 +68,7 @@ final class RemoteConfigTest extends IntegrationTestCase
},
"unspecified_value_type": {
"defaultValue": "1",
"valueType": "STRING"
"valueType": "PARAMETER_VALUE_TYPE_UNSPECIFIED"
},
"string_value_type": {
"defaultValue": "1",
Expand All @@ -80,6 +85,20 @@ final class RemoteConfigTest extends IntegrationTestCase
"json_value_type": {
"defaultValue": "{\"key\": \"value\"}",
"valueType": "JSON"
},
"is_ready_for_rollout": {
"defaultValue": "false",
"valueType": "BOOLEAN",
"conditionalValues": {
"lang_german": {
"value": "false"
},
"user_exists": {
"rollout_id": "rollout_2",
"value": "true",
"percent": 50
}
}
}
},
"parameterGroups": {
Expand Down

0 comments on commit 603bcc4

Please sign in to comment.