-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGradualRolloutUserIdStrategy.php
46 lines (35 loc) · 1.12 KB
/
GradualRolloutUserIdStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace Stogon\UnleashBundle\Strategy;
use Stogon\UnleashBundle\Helper\ValueNormalizer;
use Symfony\Component\Security\Core\User\UserInterface;
class GradualRolloutUserIdStrategy implements StrategyInterface
{
public function isEnabled(array $parameters = [], array $context = [], ...$args): bool
{
$percentage = intval($parameters['percentage'] ?? 0);
$groupId = trim($parameters['groupId'] ?? '');
$userId = trim($this->getUserId($context) ?? '');
if (!$userId) {
return false;
}
$userIdValue = ValueNormalizer::build($userId, $groupId);
return $percentage > 0 && $userIdValue <= $percentage;
}
protected function getUserId(array $context): ?string
{
if (array_key_exists('user', $context) && $context['user'] !== null) {
/** @var UserInterface */
$currentUser = $context['user'];
if (method_exists($currentUser, 'getId')) {
return $currentUser->getId();
}
if ($currentUser instanceof UserInterface) {
if (method_exists($currentUser, 'getUserIdentifier')) {
return $currentUser->getUserIdentifier();
}
return $currentUser->getUsername();
}
}
return null;
}
}