Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support transient identities and traits #78

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v2
Expand Down
22 changes: 14 additions & 8 deletions src/Flagsmith.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,28 @@ public function getEnvironmentFlags(): Flags

/**
* Get all the flags for the current environment for a given identity. Will also
* upsert all traits to the Flagsmith API for future evaluations. Providing a
* trait with a value of None will remove the trait from the identity if it exists.
* upsert all traits to the Flagsmith API for future evaluations.
*
* Providing a trait with a value of None will remove the trait from the identity if it exists.
*
* To send a transient trait, use an object as value:
*
* `$flagsmith->getIdentityFlags($identifier, (object)['transient' => ['value' => 'no-persist', 'transient' => true]])->allFlags();`
*
* @param string $identifier
* @param object|null $traits
* @return Flags
*
* @throws FlagsmithThrowable
*/
public function getIdentityFlags(string $identifier, ?object $traits = null): Flags
public function getIdentityFlags(string $identifier, ?object $traits = null, ?bool $transient = false): Flags
{
$traits = $traits ?? (object) [];
$traits = $traits ?? (object)[];
if ($this->environment) {
return $this->getIdentityFlagsFromDocument($identifier, $traits);
}

return $this->getIdentityFlagsFromApi($identifier, $traits);
return $this->getIdentityFlagsFromApi($identifier, $traits, $transient);
}

/**
Expand Down Expand Up @@ -426,11 +432,11 @@ private function getEnvironmentFlagsFromApi(): Flags
*
* @throws FlagsmithAPIError
*/
private function getIdentityFlagsFromApi(string $identifier, ?object $traits): Flags
private function getIdentityFlagsFromApi(string $identifier, ?object $traits, ?bool $transient): Flags
{
try {
$data = IdentitiesGenerator::generateIdentitiesData($identifier, $traits);
$cacheKey = IdentitiesGenerator::generateIdentitiesCacheKey($identifier, $traits);
$data = IdentitiesGenerator::generateIdentitiesData($identifier, $traits, $transient);
$cacheKey = IdentitiesGenerator::generateIdentitiesCacheKey($identifier, $traits, $transient);
$apiFlags = $this->cachedCall(
$cacheKey,
'POST',
Expand Down
25 changes: 19 additions & 6 deletions src/Utils/IdentitiesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@

class IdentitiesGenerator
{
public static function generateIdentitiesData(string $identifier, ?object $traits)
public static function generateIdentitiesData(string $identifier, ?object $traits, ?bool $transient)
{
$identities = [
$identityData = [
'identifier' => $identifier,
'traits' => [],
];

if ($transient) {
$identityData['transient'] = true;
}

if (!empty($traits)) {
foreach ($traits as $key => $value) {
$identities['traits'][] = ['trait_key' => $key, 'trait_value' => $value];
$traitData = ['trait_key' => $key];
if (is_object($value)) {
$traitData['trait_value'] = $value->value;
if ($value->transient) {
$traitData['transient'] = true;
}
} else {
$traitData['trait_value'] = $value;
}
$identityData['traits'][] = $traitData;
}
}

return $identities;
return $identityData;
}

public static function generateIdentitiesCacheKey(string $identifier, ?object $traits)
public static function generateIdentitiesCacheKey(string $identifier, ?object $traits, ?bool $transient)
{
$hashedTraits = $traits !== null ? '.'.sha1(serialize($traits)) : '';
$hashedIdentifier = sha1($identifier);
return 'Identity.'.$hashedIdentifier.$hashedTraits;
return 'Identity.'.$transient ? 'Transient' : ''.$hashedIdentifier.$hashedTraits;
}
}
36 changes: 32 additions & 4 deletions tests/FlagsmithClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ public function testGetIdentityFlagsCallsApiWhenNoLocalEnvironmentWithTraits()
->withStreamFactory($streamMock);

$identifier = 'identifer';
$traits = (object) ['some_trait' => 'some-value'];

$requestBody = IdentitiesGenerator::generateIdentitiesData($identifier, $traits);
$traits = (object)['some_trait' => 'some-value', 'transient_trait' => (object)['transient' => true, 'value' => 'some-transient-value']];

$streamMock->expects($this->once())
->method('createStream')
->with($this->equalTo(json_encode($requestBody)));
->with($this->equalTo(json_encode([
'identifier' => $identifier,
'traits' => [
['trait_key' => 'some_trait', 'trait_value' => 'some-value'],
['trait_key' => 'transient_trait', 'trait_value' => 'some-transient-value', 'transient' => true],
],
])));

$identityFlags = $flagsmith->getIdentityFlags($identifier, $traits)->allFlags();

Expand All @@ -88,6 +92,30 @@ public function testGetIdentityFlagsCallsApiWhenNoLocalEnvironmentWithTraits()
$this->assertEquals($identityFlags[0]->feature_name, 'some_feature');
}

public function testGetIdentityFlagsCallsApiWhenNoLocalEnvironmentTransient()
{
$streamMock = $this->createMock(StreamFactoryInterface::class);

$flagsmith = (new Flagsmith('api_key'))
->withClient(ClientFixtures::getMockClient())
->withStreamFactory($streamMock);

$identifier = 'identifer';
$traits = (object)['some_trait' => 'some-value'];

$streamMock->expects($this->once())
->method('createStream')
->with($this->equalTo(json_encode([
'identifier' => $identifier,
'traits' => [
['trait_key' => 'some_trait', 'trait_value' => 'some-value'],
],
'transient' => true,
])));

$flagsmith->getIdentityFlags($identifier, $traits, true);
}

public function testRequestConnectionErrorRaisesFlagsmithApiError()
{
$flagsmith = (new Flagsmith('api_key'))
Expand Down
Loading