Skip to content

Commit

Permalink
Add support for rolloutValue fields in remote config parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Sep 10, 2024
1 parent dd9560b commit dc90277
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ 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))

## [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

Check warning on line 49 in src/Firebase/RemoteConfig/ParameterValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/ParameterValue.php#L49

Added line #L49 was not covered by tests
{
return new self(rolloutValue: $value);

Check warning on line 51 in src/Firebase/RemoteConfig/ParameterValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/ParameterValue.php#L51

Added line #L51 was not covered by tests
}

/**
* @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']));

Check warning on line 72 in src/Firebase/RemoteConfig/ParameterValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/ParameterValue.php#L71-L72

Added lines #L71 - L72 were not covered by tests
}

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()];

Check warning on line 96 in src/Firebase/RemoteConfig/ParameterValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/ParameterValue.php#L95-L96

Added lines #L95 - L96 were not covered by tests
}

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)

Check warning on line 23 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L23

Added line #L23 was not covered by tests
{
}

/**
* @param RemoteConfigRolloutValueShape $data
*/
public static function fromArray(array $data): self

Check warning on line 30 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L30

Added line #L30 was not covered by tests
{
return new self($data);

Check warning on line 32 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L32

Added line #L32 was not covered by tests
}

/**
* @return RemoteConfigRolloutValueShape
*/
public function toArray(): array

Check warning on line 38 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L38

Added line #L38 was not covered by tests
{
return $this->data;

Check warning on line 40 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L40

Added line #L40 was not covered by tests
}

/**
* @return RemoteConfigRolloutValueShape
*/
public function jsonSerialize(): array

Check warning on line 46 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L46

Added line #L46 was not covered by tests
{
return $this->data;

Check warning on line 48 in src/Firebase/RemoteConfig/RolloutValue.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/RemoteConfig/RolloutValue.php#L48

Added line #L48 was not covered by tests
}
}
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 dc90277

Please sign in to comment.