Skip to content

Commit f334db5

Browse files
committed
Merge pull request #6 from launchdarkly/jko/anonymous
getFlag -> toggle dance + fix broken rules + add anonymous support
2 parents 54b87dc + 666beb2 commit f334db5

File tree

6 files changed

+59
-23
lines changed

6 files changed

+59
-23
lines changed

src/LaunchDarkly/LDClient.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function __construct($apiKey, $options = []) {
5151
$this->_client = $this->_make_client($options);
5252
}
5353

54+
public function getFlag($key, $user, $default = false) {
55+
return $this->toggle($key, $user, $default);
56+
}
57+
5458
/**
5559
* Calculates the value of a feature flag for a given user.
5660
*
@@ -60,13 +64,13 @@ public function __construct($apiKey, $options = []) {
6064
*
6165
* @return boolean Whether or not the flag should be enabled, or `default` if the flag is disabled in the LaunchDarkly control panel
6266
*/
63-
public function getFlag($key, $user, $default = false) {
67+
public function toggle($key, $user, $default = false) {
6468
if ($this->_offline) {
6569
return $default;
6670
}
6771

6872
try {
69-
$flag = $this->_getFlag($key, $user, $default);
73+
$flag = $this->_toggle($key, $user, $default);
7074

7175
if (is_null($flag)) {
7276
$this->_sendFlagRequestEvent($key, $user, $default);
@@ -90,7 +94,7 @@ public function getFlag($key, $user, $default = false) {
9094

9195
/**
9296
* Puts the LaunchDarkly client in offline mode.
93-
* In offline mode, all calls to `getFlag` will return the default value, and `track` will be a no-op.
97+
* In offline mode, all calls to `toggle` will return the default value, and `track` will be a no-op.
9498
*
9599
*/
96100
public function setOffline() {
@@ -163,13 +167,13 @@ protected function _sendFlagRequestEvent($key, $user, $value) {
163167
$this->_eventProcessor->enqueue($event);
164168
}
165169

166-
protected function _getFlag($key, $user, $default) {
170+
protected function _toggle($key, $user, $default) {
167171
try {
168172
$response = $this->_client->get("/api/eval/features/$key");
169173
return self::_decode($response->json(), $user);
170174
} catch (BadResponseException $e) {
171175
$code = $e->getResponse()->getStatusCode();
172-
error_log("LDClient::getFlag received HTTP status code $code, using default");
176+
error_log("LDClient::toggle received HTTP status code $code, using default");
173177
return $default;
174178
}
175179
}

src/LaunchDarkly/LDUser.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ class LDUser {
1616
protected $_avatar = null;
1717
protected $_firstName = null;
1818
protected $_lastName = null;
19+
protected $_anonyomus = false;
1920
protected $_custom = [];
2021

2122
/**
22-
* @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.
23-
* @param string|null $secondary An optional secondary identifier
24-
* @param string|null $ip The user's IP address (optional)
25-
* @param string|null $country The user's country, as an ISO 3166-1 alpha-2 code (e.g. 'US') (optional)
26-
* @param string|null $email The user's e-mail address (optional)
27-
* @param string|null $name The user's full name (optional)
28-
* @param string|null $avatar A URL pointing to the user's avatar image (optional)
29-
* @param string|null $firstName The user's first name (optional)
30-
* @param string|null $lastName The user's last name (optional)
31-
* @param array|null $custom Other custom attributes that can be used to create custom rules
23+
* @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.
24+
* @param string|null $secondary An optional secondary identifier
25+
* @param string|null $ip The user's IP address (optional)
26+
* @param string|null $country The user's country, as an ISO 3166-1 alpha-2 code (e.g. 'US') (optional)
27+
* @param string|null $email The user's e-mail address (optional)
28+
* @param string|null $name The user's full name (optional)
29+
* @param string|null $avatar A URL pointing to the user's avatar image (optional)
30+
* @param string|null $firstName The user's first name (optional)
31+
* @param string|null $lastName The user's last name (optional)
32+
* @param boolean|null $anonymous Whether this is an anonymous user
33+
* @param array|null $custom Other custom attributes that can be used to create custom rules
3234
*/
33-
public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName= null, $custom = []) {
35+
public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName= null, $anonymous = false, $custom = []) {
3436
$this->_key = $key;
3537
$this->_secondary = $secondary;
3638
$this->_ip = $ip;
@@ -40,6 +42,7 @@ public function __construct($key, $secondary = null, $ip = null, $country = null
4042
$this->_avatar = $avatar;
4143
$this->_firstName = $firstName;
4244
$this->_lastName = $lastName;
45+
$this->_anonymous = $anonymous;
4346
$this->_custom = $custom;
4447
}
4548

@@ -83,6 +86,10 @@ public function getLastName() {
8386
return $this->_lastName;
8487
}
8588

89+
public function getAnonymous() {
90+
return $this->_anonymous;
91+
}
92+
8693
public function toJSON() {
8794
$json = ["key" => $this->_key];
8895

@@ -113,6 +120,9 @@ public function toJSON() {
113120
if (isset($this->_custom)) {
114121
$json['custom'] = $this->_custom;
115122
}
123+
if (isset($this->_anonymous)) {
124+
$json['anonymous'] = $this->_anonymous;
125+
}
116126
return $json;
117127
}
118128
}

src/LaunchDarkly/LDUserBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LDUserBuilder {
1111
protected $_avatar = null;
1212
protected $_firstName = null;
1313
protected $_lastName = null;
14+
protected $_anonymous = false;
1415
protected $_custom = [];
1516

1617
public function __construct($key) {
@@ -57,13 +58,18 @@ public function lastName($lastName) {
5758
return $this;
5859
}
5960

61+
public function anonymous($anonymous) {
62+
$this->_anonymous = $anonymous;
63+
return $this;
64+
}
65+
6066
public function custom($custom) {
6167
$this->_custom = $custom;
6268
return $this;
6369
}
6470

6571
public function build() {
66-
return new LDUser($this->_key, $this->_secondary, $this->_ip, $this->_country, $this->_email, $this->_name, $this->_avatar, $this->_firstName, $this->_lastName, $this->_custom);
72+
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);
6773
}
6874

6975
}

src/LaunchDarkly/TargetRule.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,27 @@ public function matchTarget($user) {
4343
break;
4444
case "lastName":
4545
$u_value = $user->getLastName();
46+
break;
47+
case "anonymous":
48+
$u_value = $user->getAnonymous();
4649
break;
47-
default:
50+
default:
4851
$custom = $user->getCustom();
49-
if (is_array($custom)) {
50-
foreach ($custom as $elt) {
52+
if (is_null($custom)) {
53+
return false;
54+
}
55+
if (!array_key_exists($this->_attribute, $custom)) {
56+
return false;
57+
}
58+
$u_value = $custom[$this->_attribute];
59+
60+
if (is_array($u_value)) {
61+
foreach ($u_value as $elt) {
5162
if (in_array($elt, $this->_values)) {
5263
return true;
5364
}
5465
}
5566
return false;
56-
} else {
57-
$u_value = $custom;
5867
}
5968
break;
6069
}

tests/FeatureRepTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testFlagForTargetGroupOn() {
5656
}
5757

5858
public function testFlagForTargetGroupOff() {
59-
$user = new LDUser("[email protected]", null, null, null, null, null, null, null, null, ["groups" => "oracle"]);
59+
$user = (new LDUserBuilder("[email protected]"))->custom(["groups" => ["oracle"]])->build();
6060
$b = $this->_simpleFlag->evaluate($user);
6161
$this->assertEquals(false, $b);
6262
}

tests/LDUserTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,12 @@ public function testLDUserLastName() {
5959
$this->assertEquals("Bar", $user->getLastName());
6060
$this->assertEquals("Bar", $user->toJSON()['lastName']);
6161
}
62+
63+
public function testLDUserAnonymous() {
64+
$user = (new LDUserBuilder("[email protected]"))->anonymous(true)->build();
65+
$this->assertEquals(true, $user->getAnonymous());
66+
$this->assertEquals(true, $user->toJSON()['anonymous']);
67+
68+
}
6269
}
6370

0 commit comments

Comments
 (0)