Skip to content
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
14 changes: 9 additions & 5 deletions src/LaunchDarkly/LDClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
}
Expand Down
32 changes: 21 additions & 11 deletions src/LaunchDarkly/LDUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -83,6 +86,10 @@ public function getLastName() {
return $this->_lastName;
}

public function getAnonymous() {
return $this->_anonymous;
}

public function toJSON() {
$json = ["key" => $this->_key];

Expand Down Expand Up @@ -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;
}
}
8 changes: 7 additions & 1 deletion src/LaunchDarkly/LDUserBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LDUserBuilder {
protected $_avatar = null;
protected $_firstName = null;
protected $_lastName = null;
protected $_anonymous = false;
protected $_custom = [];

public function __construct($key) {
Expand Down Expand Up @@ -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);
}

}
19 changes: 14 additions & 5 deletions src/LaunchDarkly/TargetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic looked completely broken before. Modifying one of our existing test cases revealed the problem.

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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/FeatureRepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testFlagForTargetGroupOn() {
}

public function testFlagForTargetGroupOff() {
$user = new LDUser("[email protected]", null, null, null, null, null, null, null, null, ["groups" => "oracle"]);
$user = (new LDUserBuilder("[email protected]"))->custom(["groups" => ["oracle"]])->build();
$b = $this->_simpleFlag->evaluate($user);
$this->assertEquals(false, $b);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/LDUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ public function testLDUserLastName() {
$this->assertEquals("Bar", $user->getLastName());
$this->assertEquals("Bar", $user->toJSON()['lastName']);
}

public function testLDUserAnonymous() {
$user = (new LDUserBuilder("[email protected]"))->anonymous(true)->build();
$this->assertEquals(true, $user->getAnonymous());
$this->assertEquals(true, $user->toJSON()['anonymous']);

}
}