From 48f5213894bbaf8e34e5851cb2c68afec69af05c Mon Sep 17 00:00:00 2001 From: AmineAlyate <68709410+AmineAlyate@users.noreply.github.com> Date: Mon, 25 Jul 2022 15:29:43 +0100 Subject: [PATCH 1/5] init commit --- .gitignore | 1 + src/Feature.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1cf0182..587fce5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock phpunit.xml /vendor/ +.idea \ No newline at end of file diff --git a/src/Feature.php b/src/Feature.php index ba68a1e..198c203 100644 --- a/src/Feature.php +++ b/src/Feature.php @@ -264,7 +264,8 @@ private function isParamInRequestParams(array $requestParameters) */ private function isUserInPercentage(RolloutUserInterface $user) { - return abs(crc32($user->getRolloutIdentifier()) % 100) < $this->percentage; + // To be updated + return false; } /** From 3d8beb6f0fe5ece0b7f76e53d4cd18f3968f1a41 Mon Sep 17 00:00:00 2001 From: AmineAlyate <68709410+AmineAlyate@users.noreply.github.com> Date: Mon, 25 Jul 2022 16:35:24 +0100 Subject: [PATCH 2/5] Update isUserInPercentage function --- src/Feature.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Feature.php b/src/Feature.php index 198c203..a1855f0 100644 --- a/src/Feature.php +++ b/src/Feature.php @@ -262,10 +262,20 @@ private function isParamInRequestParams(array $requestParameters) * @param RolloutUserInterface $user * @return bool */ - private function isUserInPercentage(RolloutUserInterface $user) + private function isUserInPercentage(RolloutUserInterface $user, array $users) { - // To be updated - return false; + if ($this->percentage === 100) { + return true; + } + + if ($this->percentage === 0) { + return false; + } + + $limit = ceil(($this->percentage / 100) * count($users)); + $users = array_slice($users, $limit); + + return in_array($user->getRolloutIdentifier(), $users); } /** From 690bb40cc39ca4bb5eff0487701bafe0bbb4387c Mon Sep 17 00:00:00 2001 From: AmineAlyate <68709410+AmineAlyate@users.noreply.github.com> Date: Mon, 25 Jul 2022 17:08:29 +0100 Subject: [PATCH 3/5] add users to isActive param --- src/Feature.php | 10 +++++++--- src/Rollout.php | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Feature.php b/src/Feature.php index a1855f0..18f9d9e 100644 --- a/src/Feature.php +++ b/src/Feature.php @@ -216,8 +216,12 @@ public function clear() * @param array $requestParameters * @return bool */ - public function isActive(Rollout $rollout, RolloutUserInterface $user = null, array $requestParameters = array()) - { + public function isActive( + Rollout $rollout, + RolloutUserInterface $user = null, + array $users = array(), + array $requestParameters = array() + ) { if (null == $user) { return $this->isParamInRequestParams($requestParameters) || $this->percentage == 100 @@ -225,7 +229,7 @@ public function isActive(Rollout $rollout, RolloutUserInterface $user = null, ar } return $this->isParamInRequestParams($requestParameters) || - $this->isUserInPercentage($user) || + $this->isUserInPercentage($user, $users) || $this->isUserInActiveUsers($user) || $this->isInActiveGroup($rollout, $user); } diff --git a/src/Rollout.php b/src/Rollout.php index c5d8db5..6c326a3 100644 --- a/src/Rollout.php +++ b/src/Rollout.php @@ -124,11 +124,15 @@ public function defineGroup($group, \Closure $closure) * @param array $requestParameters * @return bool */ - public function isActive($feature, RolloutUserInterface $user = null, array $requestParameters = array()) - { + public function isActive( + $feature, + RolloutUserInterface $user = null, + array $users = array(), + array $requestParameters = array() + ) { $feature = $this->get($feature); - return $feature ? $feature->isActive($this, $user, $requestParameters) : false; + return $feature && $feature->isActive($this, $user, $users, $requestParameters); } /** From fa13e1eb7d2bc4fe7b368a64104e0b39aac5c554 Mon Sep 17 00:00:00 2001 From: AmineAlyate <68709410+AmineAlyate@users.noreply.github.com> Date: Tue, 26 Jul 2022 11:22:13 +0100 Subject: [PATCH 4/5] udapte isUserInPercentage function --- src/Feature.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Feature.php b/src/Feature.php index 18f9d9e..fb272e1 100644 --- a/src/Feature.php +++ b/src/Feature.php @@ -277,9 +277,14 @@ private function isUserInPercentage(RolloutUserInterface $user, array $users) } $limit = ceil(($this->percentage / 100) * count($users)); - $users = array_slice($users, $limit); + $users = array_slice($users, 0, $limit); + foreach ($users as $userData) { + if ($userData['slug'] === $user->getRolloutIdentifier()) { + return true; + } + } - return in_array($user->getRolloutIdentifier(), $users); + return false; } /** From bd4469018ad29b9c9b11afb433387d0b78348b57 Mon Sep 17 00:00:00 2001 From: AmineAlyate <68709410+AmineAlyate@users.noreply.github.com> Date: Thu, 28 Jul 2022 11:38:29 +0100 Subject: [PATCH 5/5] change save method level to public --- src/Rollout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rollout.php b/src/Rollout.php index 6c326a3..aa53d56 100644 --- a/src/Rollout.php +++ b/src/Rollout.php @@ -305,7 +305,7 @@ private function featuresKey() /** * @param Feature $feature */ - protected function save(Feature $feature) + public function save(Feature $feature) { $name = $feature->getName(); $this->storage->set($this->key($name), $feature->serialize());