Skip to content

Commit e3619df

Browse files
committed
Add LDClient, getFlag
1 parent 9f3292b commit e3619df

File tree

8 files changed

+97
-9
lines changed

8 files changed

+97
-9
lines changed

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=5.4"
17+
"php": ">=5.4",
18+
"guzzlehttp/guzzle": "5.0.*",
19+
"guzzlehttp/cache-subscriber": "0.1.*"
1820
},
1921
"require-dev": {
2022
"phpunit/phpunit": "4.3.*",
2123
"phpdocumentor/phpdocumentor": "2.*"
2224
},
2325
"autoload": {
24-
"files": ["lib/LaunchDarkly.php"]
26+
"psr-4": {
27+
"": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"": "tests/"
33+
}
2534
}
2635
}

lib/LaunchDarkly.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/LaunchDarkly/FeatureRep.php renamed to src/LaunchDarkly/FeatureRep.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace LaunchDarkly;
23

34
class FeatureRep {
45
protected static $LONG_SCALE = 0xFFFFFFFFFFFFFFF;

src/LaunchDarkly/LDClient.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
namespace LaunchDarkly;
3+
4+
use \GuzzleHttp\Exception\BadResponseException;
5+
use \GuzzleHttp\Subscriber\Cache\CacheSubscriber;
6+
7+
class LDClient {
8+
const DEFAULT_BASE_URI = 'https://app.launchdarkly.com';
9+
const VERSION = '0.1.0';
10+
11+
protected $_apiKey;
12+
protected $_baseUri;
13+
protected $_client;
14+
15+
public function __construct($apiKey, $baseUri = self::DEFAULT_BASE_URI) {
16+
$this->_apiKey = $apiKey;
17+
$this->_baseUri = $baseUri;
18+
$this->_client = $this->_make_client();
19+
}
20+
21+
public function getFlag($key, $user, $default = false) {
22+
try {
23+
$flag = $this->_getFlag($key, $user, $default);
24+
return is_null($flag) ? $default : $flag;
25+
} catch (Exception $e) {
26+
error_log("LaunchDarkly caught $e");
27+
return $default;
28+
}
29+
}
30+
31+
protected function _getFlag($key, $user, $default) {
32+
try {
33+
$response = $this->_client->get("/api/eval/features/$key");
34+
return self::_decode($response->json(), $user);
35+
} catch (BadResponseException $e) {
36+
$code = $e->getResponse()->getStatusCode();
37+
error_log("LDClient::getFlag received HTTP status code $code, using default");
38+
return $default;
39+
}
40+
}
41+
42+
protected function _make_client() {
43+
$client = new \GuzzleHttp\Client([
44+
'base_url' => $this->_baseUri,
45+
'defaults' => [
46+
'headers' => [
47+
'Authorization' => "api_key {$this->_apiKey}",
48+
'Content-Type' => 'application/json',
49+
'User-Agent' => 'PHPClient/' . VERSION
50+
]
51+
]
52+
]);
53+
54+
CacheSubscriber::attach($client);
55+
return $client;
56+
}
57+
58+
protected static function _decode($json, $user) {
59+
$makeVariation = function ($v) {
60+
$makeTarget = function ($t) {
61+
return new TargetRule($t['attribute'], $t['op'], $t['values']);
62+
};
63+
64+
$ts = empty($v['targets']) ? [] : $v['targets'];
65+
$targets = array_map($makeTarget, $ts);
66+
return new Variation($v['value'], $v['weight'], $targets);
67+
};
68+
69+
$vs = empty($json['variations']) ? [] : $json['variations'];
70+
$variations = array_map($makeVariation, $vs);
71+
$feature = new FeatureRep($json['name'], $json['key'], $json['salt'], $json['on'], $variations);
72+
73+
return $feature->evaluate($user);
74+
}
75+
}

lib/LaunchDarkly/LDUser.php renamed to src/LaunchDarkly/LDUser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace LaunchDarkly;
23

34
class LDUser {
45
protected $_key = null;

lib/LaunchDarkly/TargetRule.php renamed to src/LaunchDarkly/TargetRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace LaunchDarkly;
23

34
class TargetRule {
45
protected $_attribute = null;

lib/LaunchDarkly/Variation.php renamed to src/LaunchDarkly/Variation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace LaunchDarkly;
23

34
class Variation {
45
protected $_value = null;

test/FeatureRepTest.php renamed to tests/FeatureRepTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
2+
namespace LaunchDarkly\Tests;
23

3-
class FeatureRepTest extends PHPUnit_Framework_TestCase {
4+
use LaunchDarkly\FeatureRep;
5+
use LaunchDarkly\LDUser;
6+
use LaunchDarkly\TargetRule;
7+
use LaunchDarkly\Variation;
8+
9+
class FeatureRepTest extends \PHPUnit_Framework_TestCase {
410

511
protected $_simpleFlag = null;
612
protected $_disabledFlag = null;

0 commit comments

Comments
 (0)