diff --git a/src/LaunchDarkly/ApcLDDFeatureRequester.php b/src/LaunchDarkly/ApcLDDFeatureRequester.php index d6d56e005..811b8ce13 100644 --- a/src/LaunchDarkly/ApcLDDFeatureRequester.php +++ b/src/LaunchDarkly/ApcLDDFeatureRequester.php @@ -1,7 +1,6 @@ fetch($key); if ($enabled === false) { return null; - } - else { + } else { return $enabled; } } @@ -53,11 +54,13 @@ protected function add($key, $var, $ttl = 0) return \apc_add($key, $var, $ttl); } - protected function store_in_cache($key, $val) { + protected function store_in_cache($key, $val) + { $this->add($this->make_cache_key($key), $val, $this->_expiration); } - private function make_cache_key($name) { + private function make_cache_key($name) + { return $this->_features_key.'.'.$name; } -} \ No newline at end of file +} diff --git a/src/LaunchDarkly/CurlEventPublisher.php b/src/LaunchDarkly/CurlEventPublisher.php index 96131ed55..d731377eb 100644 --- a/src/LaunchDarkly/CurlEventPublisher.php +++ b/src/LaunchDarkly/CurlEventPublisher.php @@ -19,26 +19,25 @@ class CurlEventPublisher implements EventPublisher private $_ssl; private $_curl = '/usr/bin/env curl'; - function __construct($sdkKey, array $options = array()) { + public function __construct($sdkKey, array $options = array()) + { $this->_sdkKey = $sdkKey; $eventsUri = LDClient::DEFAULT_EVENTS_URI; if (isset($options['events_uri'])) { $eventsUri = $options['events_uri']; } - $url = parse_url(rtrim($eventsUri,'/')); + $url = parse_url(rtrim($eventsUri, '/')); $this->_host = $url['host']; $this->_ssl = $url['scheme'] === 'https'; if (isset($url['port'])) { $this->_port = $url['port']; - } - else { + } else { $this->_port = $this->_ssl ? 443 : 80; } if (isset($url['path'])) { $this->_path = $url['path']; - } - else { + } else { $this->_path = ''; } @@ -47,13 +46,15 @@ function __construct($sdkKey, array $options = array()) { } } - public function publish($payload) { + public function publish($payload) + { $args = $this->createArgs($payload); return $this->makeRequest($args); } - private function createArgs($payload) { + private function createArgs($payload) + { $scheme = $this->_ssl ? "https://" : "http://"; $args = " -X POST"; $args.= " -H 'Content-Type: application/json'"; @@ -65,9 +66,10 @@ private function createArgs($payload) { return $args; } - private function makeRequest($args) { + private function makeRequest($args) + { $cmd = $this->_curl . " " . $args . ">> /dev/null 2>&1 &"; shell_exec($cmd); return true; } -} \ No newline at end of file +} diff --git a/src/LaunchDarkly/EvaluationException.php b/src/LaunchDarkly/EvaluationException.php index 294688752..312c5c2cc 100644 --- a/src/LaunchDarkly/EvaluationException.php +++ b/src/LaunchDarkly/EvaluationException.php @@ -1,15 +1,16 @@ _key = $key; $this->_version = $version; $this->_on = $on; @@ -51,7 +53,8 @@ protected function __construct($key, $this->_deleted = $deleted; } - public static function getDecoder() { + public static function getDecoder() + { return function ($v) { return new FeatureFlag( $v['key'], @@ -68,11 +71,13 @@ public static function getDecoder() { }; } - public static function decode($v) { + public static function decode($v) + { return call_user_func(FeatureFlag::getDecoder(), $v); } - public function isOn() { + public function isOn() + { return $this->_on; } @@ -81,7 +86,8 @@ public function isOn() { * @param $featureRequester FeatureRequester * @return EvalResult|null */ - public function evaluate($user, $featureRequester) { + public function evaluate($user, $featureRequester) + { $prereqEvents = array(); if (is_null($user) || is_null($user->getKey())) { return new EvalResult(null, $prereqEvents); @@ -102,7 +108,8 @@ public function evaluate($user, $featureRequester) { * @param $events * @return mixed|null */ - private function _evaluate($user, $featureRequester, &$events) { + private function _evaluate($user, $featureRequester, &$events) + { $prereqOk = true; if ($this->_prerequisites != null) { foreach ($this->_prerequisites as $prereq) { @@ -111,7 +118,7 @@ private function _evaluate($user, $featureRequester, &$events) { $prereqFeatureFlag = $featureRequester->get($prereq->getKey()); if ($prereqFeatureFlag == null) { return null; - } else if ($prereqFeatureFlag->isOn()) { + } elseif ($prereqFeatureFlag->isOn()) { $prereqEvalResult = $prereqFeatureFlag->_evaluate($user, $featureRequester, $events); $variation = $prereqFeatureFlag->getVariation($prereq->getVariation()); if ($prereqEvalResult === null || $variation === null || $prereqEvalResult !== $variation) { @@ -136,7 +143,8 @@ private function _evaluate($user, $featureRequester, &$events) { * @param $user LDUser * @return int|null */ - private function evaluateIndex($user) { + private function evaluateIndex($user) + { // Check to see if targets match if ($this->_targets != null) { foreach ($this->_targets as $target) { @@ -159,7 +167,8 @@ private function evaluateIndex($user) { return $this->_fallthrough->variationIndexForUser($user, $this->_key, $this->_salt); } - private function getVariation($index) { + private function getVariation($index) + { // If the supplied index is null, then rules didn't match, and we want to return // the off variation if (!isset($index)) { @@ -174,7 +183,8 @@ private function getVariation($index) { } } - public function getOffVariationValue() { + public function getOffVariationValue() + { if ($this->_offVariation === null) { return null; } @@ -187,26 +197,30 @@ public function getOffVariationValue() { /** * @return int */ - public function getVersion() { + public function getVersion() + { return $this->_version; } /** * @return string */ - public function getKey() { + public function getKey() + { return $this->_key; } /** * @return boolean */ - public function isDeleted() { + public function isDeleted() + { return $this->_deleted; } } -class EvalResult { +class EvalResult +{ private $_value = null; /** @var array */ private $_prerequisiteEvents = []; @@ -216,7 +230,8 @@ class EvalResult { * @param null $value * @param array $prerequisiteEvents */ - public function __construct($value, array $prerequisiteEvents) { + public function __construct($value, array $prerequisiteEvents) + { $this->_value = $value; $this->_prerequisiteEvents = $prerequisiteEvents; } @@ -224,30 +239,35 @@ public function __construct($value, array $prerequisiteEvents) { /** * @return null */ - public function getValue() { + public function getValue() + { return $this->_value; } /** * @return array */ - public function getPrerequisiteEvents() { + public function getPrerequisiteEvents() + { return $this->_prerequisiteEvents; } } -class WeightedVariation { +class WeightedVariation +{ /** @var int */ private $_variation = null; /** @var int */ private $_weight = null; - private function __construct($variation, $weight) { + private function __construct($variation, $weight) + { $this->_variation = $variation; $this->_weight = $weight; } - public static function getDecoder() { + public static function getDecoder() + { return function ($v) { return new WeightedVariation($v['variation'], $v['weight']); }; @@ -256,30 +276,35 @@ public static function getDecoder() { /** * @return int */ - public function getVariation() { + public function getVariation() + { return $this->_variation; } /** * @return int */ - public function getWeight() { + public function getWeight() + { return $this->_weight; } } -class Target { +class Target +{ /** @var string[] */ private $_values = array(); /** @var int */ private $_variation = null; - protected function __construct(array $values, $variation) { + protected function __construct(array $values, $variation) + { $this->_values = $values; $this->_variation = $variation; } - public static function getDecoder() { + public static function getDecoder() + { return function ($v) { return new Target($v['values'], $v['variation']); }; @@ -288,30 +313,35 @@ public static function getDecoder() { /** * @return \string[] */ - public function getValues() { + public function getValues() + { return $this->_values; } /** * @return int */ - public function getVariation() { + public function getVariation() + { return $this->_variation; } } -class Prerequisite { +class Prerequisite +{ /** @var string */ private $_key = null; /** @var int */ private $_variation = null; - protected function __construct($key, $variation) { + protected function __construct($key, $variation) + { $this->_key = $key; $this->_variation = $variation; } - public static function getDecoder() { + public static function getDecoder() + { return function ($v) { return new Prerequisite($v['key'], $v['variation']); }; @@ -320,30 +350,35 @@ public static function getDecoder() { /** * @return string */ - public function getKey() { + public function getKey() + { return $this->_key; } /** * @return int */ - public function getVariation() { + public function getVariation() + { return $this->_variation; } } -class Rollout { +class Rollout +{ /** @var WeightedVariation[] */ private $_variations = array(); /** @var string */ private $_bucketBy = null; - protected function __construct(array $variations, $bucketBy) { + protected function __construct(array $variations, $bucketBy) + { $this->_variations = $variations; $this->_bucketBy = $bucketBy; } - public static function getDecoder() { + public static function getDecoder() + { return function ($v) { return new Rollout( array_map(WeightedVariation::getDecoder(), $v['variations']), @@ -354,14 +389,16 @@ public static function getDecoder() { /** * @return WeightedVariation[] */ - public function getVariations() { + public function getVariations() + { return $this->_variations; } /** * @return string */ - public function getBucketBy() { + public function getBucketBy() + { return $this->_bucketBy; } } diff --git a/src/LaunchDarkly/FeatureRequester.php b/src/LaunchDarkly/FeatureRequester.php index 2ce2757b1..e8b436f7d 100644 --- a/src/LaunchDarkly/FeatureRequester.php +++ b/src/LaunchDarkly/FeatureRequester.php @@ -1,7 +1,8 @@ _sdkKey = $sdkKey; $this->_logger = $options['logger']; if (isset($options['events_uri'])) { @@ -45,7 +46,8 @@ function __construct($sdkKey, array $options = array()) { ]; } - public function publish($payload) { + public function publish($payload) + { $client = new Client(['base_uri' => $this->_eventsUri]); try { @@ -59,4 +61,4 @@ public function publish($payload) { return false; } } -} \ No newline at end of file +} diff --git a/src/LaunchDarkly/GuzzleFeatureRequester.php b/src/LaunchDarkly/GuzzleFeatureRequester.php index af3e006df..4f4c20852 100644 --- a/src/LaunchDarkly/GuzzleFeatureRequester.php +++ b/src/LaunchDarkly/GuzzleFeatureRequester.php @@ -20,18 +20,18 @@ class GuzzleFeatureRequester implements FeatureRequester /** @var LoggerInterface */ private $_logger; /** @var boolean */ - private $_loggedCacheNotice = FALSE; + private $_loggedCacheNotice = false; - function __construct($baseUri, $sdkKey, $options) + public function __construct($baseUri, $sdkKey, $options) { $this->_baseUri = $baseUri; $this->_logger = $options['logger']; $stack = HandlerStack::create(); if (class_exists('Kevinrob\GuzzleCache\CacheMiddleware')) { $stack->push(new CacheMiddleware(new PublicCacheStrategy(isset($options['cache']) ? $options['cache'] : null), 'cache')); - } else if (!$this->_loggedCacheNotice) { + } elseif (!$this->_loggedCacheNotice) { $this->_logger->info("GuzzleFeatureRequester is not using an HTTP cache because Kevinrob\GuzzleCache\CacheMiddleware was not installed"); - $this->_loggedCacheNotice = TRUE; + $this->_loggedCacheNotice = true; } $this->_defaults = array( @@ -76,7 +76,8 @@ public function get($key) * * @return array()|null The decoded FeatureFlags, or null if missing */ - public function getAll() { + public function getAll() + { try { $uri = $this->_baseUri . self::SDK_FLAGS; $response = $this->_client->get($uri, $this->_defaults); @@ -88,4 +89,4 @@ public function getAll() { return null; } } -} \ No newline at end of file +} diff --git a/src/LaunchDarkly/LDClient.php b/src/LaunchDarkly/LDClient.php index 64d007b52..606fb6d86 100644 --- a/src/LaunchDarkly/LDClient.php +++ b/src/LaunchDarkly/LDClient.php @@ -8,7 +8,8 @@ /** * A client for the LaunchDarkly API. */ -class LDClient { +class LDClient +{ const DEFAULT_BASE_URI = 'https://app.launchdarkly.com'; const DEFAULT_EVENTS_URI = 'https://events.launchdarkly.com'; const VERSION = '2.3.0'; @@ -50,7 +51,8 @@ class LDClient { * - event_publisher: An optional LaunchDarkly\EventPublisher instance. * - event_publisher_class: An optional class implementing LaunchDarkly\EventPublisher, if `event_publisher` is not specified. Defaults to CurlEventPublisher. */ - public function __construct($sdkKey, $options = array()) { + public function __construct($sdkKey, $options = array()) + { $this->_sdkKey = $sdkKey; if (!isset($options['base_uri'])) { $this->_baseUri = self::DEFAULT_BASE_URI; @@ -128,7 +130,8 @@ private function getFeatureRequester($sdkKey, array $options) * * @return mixed The result of the Feature Flag evaluation, or $default if any errors occurred. */ - public function variation($key, $user, $default = false) { + public function variation($key, $user, $default = false) + { $default = $this->_get_default($key, $default); if ($this->_offline) { @@ -178,7 +181,8 @@ public function variation($key, $user, $default = false) { * @param bool $default * @return mixed */ - public function toggle($key, $user, $default = false) { + public function toggle($key, $user, $default = false) + { $this->_logger->warning("Deprecated function: toggle() called. Use variation() instead."); return $this->variation($key, $user, $default); } @@ -187,7 +191,8 @@ public function toggle($key, $user, $default = false) { * Returns whether the LaunchDarkly client is in offline mode. * */ - public function isOffline() { + public function isOffline() + { return $this->_offline; } @@ -198,7 +203,8 @@ public function isOffline() { * @param $user LDUser The user that performed the event * @param $data mixed */ - public function track($eventName, $user, $data) { + public function track($eventName, $user, $data) + { if ($this->isOffline()) { return; } @@ -220,7 +226,8 @@ public function track($eventName, $user, $data) { /** * @param $user LDUser */ - public function identify($user) { + public function identify($user) + { if ($this->isOffline()) { return; } @@ -247,7 +254,8 @@ public function identify($user) { * @param $user LDUser the end user requesting the feature flags * @return array()|null Mapping of feature flag keys to their evaluated results for $user */ - public function allFlags($user) { + public function allFlags($user) + { if (is_null($user) || is_null($user->getKey())) { $this->_logger->warn("allFlags called with null user or null/empty user key! Returning null"); return null; @@ -261,7 +269,7 @@ public function allFlags($user) { * @param $flag FeatureFlag * @return mixed|null */ - $eval = function($flag) use($user) { + $eval = function ($flag) use ($user) { return $flag->evaluate($user, $this->_featureRequester)->getValue(); }; @@ -272,7 +280,8 @@ public function allFlags($user) { * @param $user LDUser * @return string */ - public function secureModeHash($user) { + public function secureModeHash($user) + { if (is_null($user) || strlen($user->getKey()) === 0) { return ""; } @@ -296,14 +305,16 @@ public function flush() * @param $version int | null * @param string | null $prereqOf */ - protected function _sendFlagRequestEvent($key, $user, $value, $default, $version = null, $prereqOf = null) { + protected function _sendFlagRequestEvent($key, $user, $value, $default, $version = null, $prereqOf = null) + { if ($this->isOffline() || !$this->_send_events) { return; } $this->_eventProcessor->enqueue(Util::newFeatureRequestEvent($key, $user, $value, $default, $version, $prereqOf)); } - protected function _get_default($key, $default) { + protected function _get_default($key, $default) + { if (array_key_exists($key, $this->_defaults)) { return $this->_defaults[$key]; } else { diff --git a/src/LaunchDarkly/LDDFeatureRequester.php b/src/LaunchDarkly/LDDFeatureRequester.php index dd5ec7262..88731c7e5 100644 --- a/src/LaunchDarkly/LDDFeatureRequester.php +++ b/src/LaunchDarkly/LDDFeatureRequester.php @@ -1,11 +1,11 @@ _baseUri = $baseUri; $this->_sdkKey = $sdkKey; if (!isset($options['redis_host'])) { @@ -42,7 +43,8 @@ function __construct($baseUri, $sdkKey, $options) { /** * @return ClientInterface */ - protected function get_connection() { + protected function get_connection() + { if ($this->_connection instanceof ClientInterface) { return $this->_connection; } @@ -61,7 +63,8 @@ protected function get_connection() { * @param $key string feature key * @return FeatureFlag|null The decoded JSON feature data, or null if missing */ - public function get($key) { + public function get($key) + { $raw = $this->get_from_cache($key); if ($raw === null) { $redis = $this->get_connection(); @@ -71,13 +74,12 @@ public function get($key) { } } if ($raw) { - $flag = FeatureFlag::decode(json_decode($raw, True)); + $flag = FeatureFlag::decode(json_decode($raw, true)); if ($flag->isDeleted()) { $this->_logger->warning("LDDFeatureRequester: Attempted to get deleted feature with key: " . $key); return null; } return $flag; - } else { $this->_logger->warning("LDDFeatureRequester: Attempted to get missing feature with key: " . $key); return null; @@ -89,7 +91,8 @@ public function get($key) { * @param $key string The feature key * @return null|array The feature data or null if missing */ - protected function get_from_cache($key) { + protected function get_from_cache($key) + { return null; } @@ -98,14 +101,17 @@ protected function get_from_cache($key) { * @param $key string The feature key * @param $val array The feature data */ - protected function store_in_cache($key, $val) {} + protected function store_in_cache($key, $val) + { + } /** * Gets all features * * @return array()|null The decoded FeatureFlags, or null if missing */ - public function getAll() { + public function getAll() + { $redis = $this->get_connection(); $raw = $redis->hgetall($this->_features_key); if ($raw) { diff --git a/src/LaunchDarkly/LDUser.php b/src/LaunchDarkly/LDUser.php index 76aaac5fe..21889dc0f 100644 --- a/src/LaunchDarkly/LDUser.php +++ b/src/LaunchDarkly/LDUser.php @@ -6,7 +6,8 @@ * which must uniquely identify each 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. */ -class LDUser { +class LDUser +{ protected $_key = null; protected $_secondary = null; protected $_ip = null; @@ -32,7 +33,8 @@ class LDUser { * @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, $anonymous = null, $custom = array()) { + public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName = null, $anonymous = null, $custom = array()) + { if ($key !== null) { $this->_key = strval($key); } @@ -48,7 +50,8 @@ public function __construct($key, $secondary = null, $ip = null, $country = null $this->_custom = $custom; } - public function getValueForEvaluation($attr) { + public function getValueForEvaluation($attr) + { switch ($attr) { case "key": return $this->getKey(); @@ -82,58 +85,71 @@ public function getValueForEvaluation($attr) { } } - public function getCountry() { + public function getCountry() + { return $this->_country; } - public function getCustom() { + public function getCustom() + { return $this->_custom; } - public function getIP() { + public function getIP() + { return $this->_ip; } /** * @return null|string */ - public function getKey() { + public function getKey() + { return $this->_key; } - public function getSecondary() { + public function getSecondary() + { return $this->_secondary; } - public function getEmail() { + public function getEmail() + { return $this->_email; } - public function getName() { + public function getName() + { return $this->_name; } - public function getAvatar() { + public function getAvatar() + { return $this->_avatar; } - public function getFirstName() { + public function getFirstName() + { return $this->_firstName; } - public function getLastName() { + public function getLastName() + { return $this->_lastName; } - public function getAnonymous() { + public function getAnonymous() + { return $this->_anonymous; } - public function isKeyBlank() { + public function isKeyBlank() + { return isset($this->_key) && empty($this->_key); } - public function toJSON() { + public function toJSON() + { $json = array("key" => $this->_key); if (isset($this->_secondary)) { diff --git a/src/LaunchDarkly/LDUserBuilder.php b/src/LaunchDarkly/LDUserBuilder.php index e0a4484d7..2bff3b2c6 100644 --- a/src/LaunchDarkly/LDUserBuilder.php +++ b/src/LaunchDarkly/LDUserBuilder.php @@ -1,7 +1,8 @@ _key = $key; } - public function secondary($secondary) { + public function secondary($secondary) + { $this->_secondary = $secondary; return $this; } - public function ip($ip) { + public function ip($ip) + { $this->_ip = $ip; return $this; } - public function country($country) { + public function country($country) + { $this->_country = $country; return $this; } - public function email($email) { + public function email($email) + { $this->_email = $email; return $this; } - public function name($name) { + public function name($name) + { $this->_name = $name; return $this; } - public function avatar($avatar) { + public function avatar($avatar) + { $this->_avatar = $avatar; return $this; } - public function firstName($firstName) { + public function firstName($firstName) + { $this->_firstName = $firstName; return $this; } - public function lastName($lastName) { + public function lastName($lastName) + { $this->_lastName = $lastName; return $this; } - public function anonymous($anonymous) { + public function anonymous($anonymous) + { $this->_anonymous = $anonymous; return $this; } - public function custom($custom) { + public function custom($custom) + { $this->_custom = $custom; return $this; } - public function build() { + 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->_anonymous, $this->_custom); } - -} \ No newline at end of file +} diff --git a/src/LaunchDarkly/Operators.php b/src/LaunchDarkly/Operators.php index a491fc932..1c023ae36 100644 --- a/src/LaunchDarkly/Operators.php +++ b/src/LaunchDarkly/Operators.php @@ -4,7 +4,8 @@ use DateTime; use Exception; -class Operators { +class Operators +{ const RFC3339 = 'Y-m-d\TH:i:s.uP'; /** @@ -13,7 +14,8 @@ class Operators { * @param $c * @return bool */ - public static function apply($op, $u, $c) { + public static function apply($op, $u, $c) + { try { if ($u === null || $c === null) { return false; @@ -45,7 +47,7 @@ public static function apply($op, $u, $c) { break; case "contains": if (is_string($u) && is_string($c)) { - return strpos($u, $c) !== FALSE; + return strpos($u, $c) !== false; } break; case "lessThan": @@ -96,7 +98,8 @@ public static function apply($op, $u, $c) { * @param $in * @return null|int|float */ - public static function parseTime($in) { + public static function parseTime($in) + { if (is_numeric($in)) { return $in; } @@ -115,4 +118,4 @@ public static function parseTime($in) { } return null; } -} \ No newline at end of file +} diff --git a/src/LaunchDarkly/Util.php b/src/LaunchDarkly/Util.php index e1d84897a..0c0a4f774 100644 --- a/src/LaunchDarkly/Util.php +++ b/src/LaunchDarkly/Util.php @@ -5,13 +5,15 @@ use DateTime; use DateTimeZone; -class Util { +class Util +{ /** * @param $dateTime DateTime * @return int */ - public static function dateTimeToUnixMillis($dateTime) { + public static function dateTimeToUnixMillis($dateTime) + { $timeStampSeconds = (int)$dateTime->getTimeStamp(); $timestampMicros = $dateTime->format('u'); return $timeStampSeconds * 1000 + (int)($timestampMicros / 1000); @@ -20,7 +22,8 @@ public static function dateTimeToUnixMillis($dateTime) { /** * @return int */ - public static function currentTimeUnixMillis() { + public static function currentTimeUnixMillis() + { return Util::dateTimeToUnixMillis(new DateTime('now', new DateTimeZone("UTC"))); } @@ -34,7 +37,8 @@ public static function currentTimeUnixMillis() { * @param null $prereqOf string | null * @return array */ - public static function newFeatureRequestEvent($key, $user, $value, $default, $version = null, $prereqOf = null) { + public static function newFeatureRequestEvent($key, $user, $value, $default, $version = null, $prereqOf = null) + { $event = array(); $event['user'] = $user->toJSON(); $event['value'] = $value; @@ -46,5 +50,4 @@ public static function newFeatureRequestEvent($key, $user, $value, $default, $ve $event['prereqOf'] = $prereqOf; return $event; } - -} \ No newline at end of file +}