From 34938b6791fcc7bac02b01219c2fd2d9027a6d0b Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Mon, 9 Mar 2015 13:24:13 -0700 Subject: [PATCH 1/2] Add support for anonymous, and fix matching rules --- src/LaunchDarkly/LDUser.php | 32 ++++++++++++++++++++---------- src/LaunchDarkly/LDUserBuilder.php | 8 +++++++- src/LaunchDarkly/TargetRule.php | 19 +++++++++++++----- tests/FeatureRepTest.php | 2 +- tests/LDUserTest.php | 7 +++++++ 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/LaunchDarkly/LDUser.php b/src/LaunchDarkly/LDUser.php index 60764a251..a7181a226 100644 --- a/src/LaunchDarkly/LDUser.php +++ b/src/LaunchDarkly/LDUser.php @@ -16,21 +16,23 @@ class LDUser { protected $_avatar = null; protected $_firstName = null; protected $_lastName = null; + protected $_anonyomus = false; protected $_custom = []; /** - * @param string $key Unique key for the user. For authenticated users, this may be a username or e-mail address. For anonymous users, this could be an IP address or session ID. - * @param string|null $secondary An optional secondary identifier - * @param string|null $ip The user's IP address (optional) - * @param string|null $country The user's country, as an ISO 3166-1 alpha-2 code (e.g. 'US') (optional) - * @param string|null $email The user's e-mail address (optional) - * @param string|null $name The user's full name (optional) - * @param string|null $avatar A URL pointing to the user's avatar image (optional) - * @param string|null $firstName The user's first name (optional) - * @param string|null $lastName The user's last name (optional) - * @param array|null $custom Other custom attributes that can be used to create custom rules + * @param string $key Unique key for the user. For authenticated users, this may be a username or e-mail address. For anonymous users, this could be an IP address or session ID. + * @param string|null $secondary An optional secondary identifier + * @param string|null $ip The user's IP address (optional) + * @param string|null $country The user's country, as an ISO 3166-1 alpha-2 code (e.g. 'US') (optional) + * @param string|null $email The user's e-mail address (optional) + * @param string|null $name The user's full name (optional) + * @param string|null $avatar A URL pointing to the user's avatar image (optional) + * @param string|null $firstName The user's first name (optional) + * @param string|null $lastName The user's last name (optional) + * @param boolean|null $anonymous Whether this is an anonymous user + * @param array|null $custom Other custom attributes that can be used to create custom rules */ - public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName= null, $custom = []) { + public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName= null, $anonymous = false, $custom = []) { $this->_key = $key; $this->_secondary = $secondary; $this->_ip = $ip; @@ -40,6 +42,7 @@ public function __construct($key, $secondary = null, $ip = null, $country = null $this->_avatar = $avatar; $this->_firstName = $firstName; $this->_lastName = $lastName; + $this->_anonymous = $anonymous; $this->_custom = $custom; } @@ -83,6 +86,10 @@ public function getLastName() { return $this->_lastName; } + public function getAnonymous() { + return $this->_anonymous; + } + public function toJSON() { $json = ["key" => $this->_key]; @@ -113,6 +120,9 @@ public function toJSON() { if (isset($this->_custom)) { $json['custom'] = $this->_custom; } + if (isset($this->_anonymous)) { + $json['anonymous'] = $this->_anonymous; + } return $json; } } diff --git a/src/LaunchDarkly/LDUserBuilder.php b/src/LaunchDarkly/LDUserBuilder.php index f490c2ddc..a5db8a151 100644 --- a/src/LaunchDarkly/LDUserBuilder.php +++ b/src/LaunchDarkly/LDUserBuilder.php @@ -11,6 +11,7 @@ class LDUserBuilder { protected $_avatar = null; protected $_firstName = null; protected $_lastName = null; + protected $_anonymous = false; protected $_custom = []; public function __construct($key) { @@ -57,13 +58,18 @@ public function lastName($lastName) { return $this; } + public function anonymous($anonymous) { + $this->_anonymous = $anonymous; + return $this; + } + public function custom($custom) { $this->_custom = $custom; return $this; } public function build() { - return new LDUser($this->_key, $this->_secondary, $this->_ip, $this->_country, $this->_email, $this->_name, $this->_avatar, $this->_firstName, $this->_lastName, $this->_custom); + return new LDUser($this->_key, $this->_secondary, $this->_ip, $this->_country, $this->_email, $this->_name, $this->_avatar, $this->_firstName, $this->_lastName, $this->_anonymous, $this->_custom); } } \ No newline at end of file diff --git a/src/LaunchDarkly/TargetRule.php b/src/LaunchDarkly/TargetRule.php index 0a2661e42..cc7345f25 100644 --- a/src/LaunchDarkly/TargetRule.php +++ b/src/LaunchDarkly/TargetRule.php @@ -43,18 +43,27 @@ public function matchTarget($user) { break; case "lastName": $u_value = $user->getLastName(); + break; + case "anonymous": + $u_value = $user->getAnonymous(); break; - default: + default: $custom = $user->getCustom(); - if (is_array($custom)) { - foreach ($custom as $elt) { + if (is_null($custom)) { + return false; + } + if (!array_key_exists($this->_attribute, $custom)) { + return false; + } + $u_value = $custom[$this->_attribute]; + + if (is_array($u_value)) { + foreach ($u_value as $elt) { if (in_array($elt, $this->_values)) { return true; } } return false; - } else { - $u_value = $custom; } break; } diff --git a/tests/FeatureRepTest.php b/tests/FeatureRepTest.php index 096e33e3d..28e38d353 100644 --- a/tests/FeatureRepTest.php +++ b/tests/FeatureRepTest.php @@ -56,7 +56,7 @@ public function testFlagForTargetGroupOn() { } public function testFlagForTargetGroupOff() { - $user = new LDUser("targetOther@test.com", null, null, null, null, null, null, null, null, ["groups" => "oracle"]); + $user = (new LDUserBuilder("targetOther@test.com"))->custom(["groups" => ["oracle"]])->build(); $b = $this->_simpleFlag->evaluate($user); $this->assertEquals(false, $b); } diff --git a/tests/LDUserTest.php b/tests/LDUserTest.php index 3ce9de806..6d5a8412b 100644 --- a/tests/LDUserTest.php +++ b/tests/LDUserTest.php @@ -59,5 +59,12 @@ public function testLDUserLastName() { $this->assertEquals("Bar", $user->getLastName()); $this->assertEquals("Bar", $user->toJSON()['lastName']); } + + public function testLDUserAnonymous() { + $user = (new LDUserBuilder("foo@bar.com"))->anonymous(true)->build(); + $this->assertEquals(true, $user->getAnonymous()); + $this->assertEquals(true, $user->toJSON()['anonymous']); + + } } From 666beb23ebd2b99e5f37d9044b928775797e5fa5 Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Mon, 9 Mar 2015 13:25:50 -0700 Subject: [PATCH 2/2] Rename getFlag to toggle, and add alias for backwards-compatibility --- src/LaunchDarkly/LDClient.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/LaunchDarkly/LDClient.php b/src/LaunchDarkly/LDClient.php index c95fb82d7..573fb7eef 100644 --- a/src/LaunchDarkly/LDClient.php +++ b/src/LaunchDarkly/LDClient.php @@ -51,6 +51,10 @@ public function __construct($apiKey, $options = []) { $this->_client = $this->_make_client($options); } + public function getFlag($key, $user, $default = false) { + return $this->toggle($key, $user, $default); + } + /** * Calculates the value of a feature flag for a given user. * @@ -60,13 +64,13 @@ public function __construct($apiKey, $options = []) { * * @return boolean Whether or not the flag should be enabled, or `default` if the flag is disabled in the LaunchDarkly control panel */ - public function getFlag($key, $user, $default = false) { + public function toggle($key, $user, $default = false) { if ($this->_offline) { return $default; } try { - $flag = $this->_getFlag($key, $user, $default); + $flag = $this->_toggle($key, $user, $default); if (is_null($flag)) { $this->_sendFlagRequestEvent($key, $user, $default); @@ -90,7 +94,7 @@ public function getFlag($key, $user, $default = false) { /** * Puts the LaunchDarkly client in offline mode. - * In offline mode, all calls to `getFlag` will return the default value, and `track` will be a no-op. + * In offline mode, all calls to `toggle` will return the default value, and `track` will be a no-op. * */ public function setOffline() { @@ -163,13 +167,13 @@ protected function _sendFlagRequestEvent($key, $user, $value) { $this->_eventProcessor->enqueue($event); } - protected function _getFlag($key, $user, $default) { + protected function _toggle($key, $user, $default) { try { $response = $this->_client->get("/api/eval/features/$key"); return self::_decode($response->json(), $user); } catch (BadResponseException $e) { $code = $e->getResponse()->getStatusCode(); - error_log("LDClient::getFlag received HTTP status code $code, using default"); + error_log("LDClient::toggle received HTTP status code $code, using default"); return $default; } }