Skip to content

Commit

Permalink
⬆️ (composer.json): upgrade dependencies to latest versions
Browse files Browse the repository at this point in the history
♻️ (HasFlags.php): add support for BackedEnum and UnitEnum in trait
✨ (UserLogin.php): use IdentitiesGenerator to generate cache key for user identity
  • Loading branch information
tm1000 committed Jul 7, 2024
1 parent 0c243a2 commit 2965136
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.2",
"laravel/framework": "^10.44.0 || ^11.0",
"flagsmith/flagsmith-php-client": "^4.2.0"
"flagsmith/flagsmith-php-client": "^4.3.1"
},
"extra": {
"laravel": {
Expand All @@ -32,10 +32,10 @@
"require-dev": {
"guzzlehttp/psr7": "^2.6.2",
"guzzlehttp/guzzle": "^7.8.1",
"orchestra/testbench": "^8.22.0",
"phpunit/phpunit": "^10.5.13",
"vimeo/psalm": "^5.23.1",
"squizlabs/php_codesniffer": "^3.9.0"
"orchestra/testbench": "^8.23.0",
"phpunit/phpunit": "^10.5.25",
"vimeo/psalm": "^5.25.0",
"squizlabs/php_codesniffer": "^3.10.1"
},
"config": {
"allow-plugins": {
Expand Down
10 changes: 9 additions & 1 deletion src/Concerns/HasFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Clearlyip\LaravelFlagsmith\Concerns;

use BackedEnum;
use Flagsmith\Flagsmith;
use Flagsmith\Models\Identity;
use Flagsmith\Models\IdentityTrait;
use Flagsmith\Utils\Collections\FlagModelsList;
use Illuminate\Support\Facades\App;
use UnitEnum;

trait HasFlags
{
Expand Down Expand Up @@ -112,7 +114,13 @@ public function getFlagTraits(): array
return array_reduce(
config('flagsmith.identity.traits', []),
function ($carry, $attribute) {
$carry[$attribute] = $this->getRawOriginal($attribute);
$value = $this->{$attribute};
if ($value instanceof BackedEnum) {
$value = $value->value;
} elseif ($value instanceof UnitEnum) {
$value = $value->name;
}
$carry[$attribute] = $value;
return $carry;
},
[],
Expand Down
8 changes: 7 additions & 1 deletion src/Listeners/UserLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Clearlyip\LaravelFlagsmith\Contracts\UserFlags;
use Clearlyip\LaravelFlagsmith\Jobs\SyncUser;
use Flagsmith\Utils\IdentitiesGenerator;
use Illuminate\Auth\Events\Login;

class UserLogin
Expand Down Expand Up @@ -42,8 +43,13 @@ public function handle(Login $event)
return;
}

$key = IdentitiesGenerator::generateIdentitiesCacheKey(
$user->getFlagIdentityId(),
(object) $user->getFlagTraits(),
);

//Doesn't exist so get it now
if (!$cache->has('Identity.' . $user->getFlagIdentityId())) {
if (!$cache->has($key)) {
SyncUser::dispatchSync($user);
} else {
SyncUser::dispatch($user)->onQueue($queue);
Expand Down

0 comments on commit 2965136

Please sign in to comment.