Skip to content

Commit 3d73351

Browse files
committed
Support LDD feature retrieval
* Clean up formatting and add types * New FeatureRequester interface to abstract grizzly and ldd support * Support PHP 5.3 * New optional predis for LDD retrieval
1 parent c747f4c commit 3d73351

16 files changed

+339
-119
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/vendor/
2-
/doc/
2+
/doc/
3+
*.iml
4+
composer.phar

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "4.3.*",
23-
"phpdocumentor/phpdocumentor": "2.*"
23+
"phpdocumentor/phpdocumentor": "2.*",
24+
"predis/predis": "1.0.*"
25+
},
26+
"suggested": {
27+
"predis/predis": "1.0.*"
2428
},
2529
"autoload": {
2630
"psr-4": {

composer.lock

Lines changed: 57 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LaunchDarkly/EventProcessor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ class EventProcessor {
1010
private $_queue;
1111
private $_capacity;
1212
private $_timeout;
13-
private $_socket_failed;
1413
private $_host;
1514
private $_port;
1615
private $_ssl;
1716

18-
public function __construct($apiKey, $options = []) {
17+
public function __construct($apiKey, $options = array()) {
1918
$this->_apiKey = $apiKey;
2019
if (!isset($options['base_uri'])) {
2120
$this->_host = 'app.launchdarkly.com';
@@ -60,7 +59,7 @@ public function enqueue($event) {
6059

6160
protected function flush() {
6261
if (empty($this->_queue)) {
63-
return;
62+
return null;
6463
}
6564

6665
$payload = json_encode($this->_queue);

src/LaunchDarkly/FeatureRep.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ class FeatureRep {
1111
protected $_key = null;
1212
protected $_salt = null;
1313
protected $_on = false;
14-
protected $_variations = [];
1514

16-
public function __construct($name, $key, $salt, $on = true, $variations = []) {
15+
/** @var Variation[] */
16+
protected $_variations = array();
17+
18+
public function __construct($name, $key, $salt, $on = true, $variations = array()) {
1719
$this->_name = $name;
18-
$this->_key = $key;
20+
$this->_key = $key;
1921
$this->_salt = $salt;
20-
$this->_on = $on;
22+
$this->_on = $on;
2123
$this->_variations = $variations;
2224
}
2325

26+
/**
27+
* @param $user LDUser
28+
* @return mixed
29+
*/
2430
public function evaluate($user) {
2531
if (!$this->_on || !$user) {
2632
return null;
@@ -29,7 +35,8 @@ public function evaluate($user) {
2935
$param = $this->_get_param($user);
3036
if (is_null($param)) {
3137
return null;
32-
} else {
38+
}
39+
else {
3340
foreach ($this->_variations as $variation) {
3441
if ($variation->matchUser($user)) {
3542
return $variation->getValue();
@@ -55,18 +62,23 @@ public function evaluate($user) {
5562
return null;
5663
}
5764

65+
/**
66+
* @param $user LDUser
67+
* @return float|null
68+
*/
5869
private function _get_param($user) {
5970
$id_hash = null;
6071
$hash = null;
6172

6273
if ($user->getKey()) {
6374
$id_hash = $user->getKey();
64-
} else {
75+
}
76+
else {
6577
return null;
6678
}
6779

6880
if ($user->getSecondary()) {
69-
$id_hash += "." . $user->getSecondary();
81+
$id_hash .= "." . $user->getSecondary();
7082
}
7183

7284
$hash = substr(sha1($this->_key . "." . $this->_salt . "." . $id_hash), 0, 15);

src/LaunchDarkly/FeatureRequester.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace LaunchDarkly;
3+
4+
interface FeatureRequester {
5+
6+
/**
7+
* Gets feature data from a likely cached store
8+
*
9+
* @param $key string feature key
10+
* @return mixed|null The decoded JSON feature data, or null if missing
11+
*/
12+
public function get($key);
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace LaunchDarkly;
3+
4+
use GuzzleHttp\Client;
5+
use \GuzzleHttp\Exception\BadResponseException;
6+
use \GuzzleHttp\Subscriber\Cache\CacheSubscriber;
7+
8+
class GuzzleFeatureRequester implements FeatureRequester {
9+
function __construct($baseUri, $apiKey, $options) {
10+
$this->_client = new Client(array(
11+
'base_url' => $baseUri,
12+
'defaults' => array(
13+
'headers' => array(
14+
'Authorization' => "api_key {$apiKey}",
15+
'Content-Type' => 'application/json',
16+
'User-Agent' => 'PHPClient/' . LDClient::VERSION
17+
),
18+
'debug' => false,
19+
'timeout' => $options['timeout'],
20+
'connect_timeout' => $options['connect_timeout']
21+
)
22+
));
23+
24+
if (!isset($options['cache_storage'])) {
25+
$csOptions = array('validate' => false);
26+
}
27+
else {
28+
$csOptions = array('storage' => $options['cache_storage'], 'validate' => false);
29+
}
30+
31+
CacheSubscriber::attach($this->_client, $csOptions);
32+
}
33+
34+
35+
/**
36+
* Gets feature data from a likely cached store
37+
*
38+
* @param $key string feature key
39+
* @return mixed The decoded JSON feature data, or null if missing
40+
*/
41+
public function get($key) {
42+
try {
43+
$response = $this->_client->get("/api/eval/features/$key");
44+
return $response->json();
45+
} catch (BadResponseException $e) {
46+
$code = $e->getResponse()->getStatusCode();
47+
error_log("GuzzleFeatureRetriever::get received an unexpected HTTP status code $code");
48+
return null;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)