From 5862c6d512f32ba70b9f07985dd93f518689ea0b Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Wed, 21 Jan 2015 10:36:29 -0800 Subject: [PATCH] Add setOffline to PHP client --- src/LaunchDarkly/EventProcessor.php | 4 +++ src/LaunchDarkly/LDClient.php | 38 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index 823865c1e..30d8016dc 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -59,6 +59,10 @@ public function enqueue($event) { } protected function flush() { + if (empty($this->_queue)) { + return; + } + $socket = $this->createSocket(); if (!$socket) { diff --git a/src/LaunchDarkly/LDClient.php b/src/LaunchDarkly/LDClient.php index 5efb2c4cb..d079f8130 100644 --- a/src/LaunchDarkly/LDClient.php +++ b/src/LaunchDarkly/LDClient.php @@ -15,6 +15,7 @@ class LDClient { protected $_baseUri; protected $_client; protected $_eventProcessor; + protected $_offline; /** * Creates a new client instance that connects to LaunchDarkly. @@ -60,6 +61,10 @@ 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) { + if ($this->_offline) { + return $default; + } + try { $flag = $this->_getFlag($key, $user, $default); @@ -78,6 +83,31 @@ 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 `sendEvent` will be a no-op. + * + */ + public function setOffline() { + $this->_offline = true; + } + + /** + * Puts the LaunchDarkly client in online mode. + * + */ + public function setOnline() { + $this->_offline = false; + } + + /** + * Returns whether the LaunchDarlkly client is in offline mode. + * + */ + public function isOffline() { + return $this->_offline; + } + /** * Tracks that a user performed an event. * @@ -86,6 +116,10 @@ public function getFlag($key, $user, $default = false) { * */ public function sendEvent($eventName, $user, $data) { + if ($this->isOffline()) { + return; + } + $event = array(); $event['user'] = $user->toJSON(); $event['kind'] = "custom"; @@ -98,6 +132,10 @@ public function sendEvent($eventName, $user, $data) { } protected function _sendFlagRequestEvent($key, $user, $value) { + if ($this->isOffline()) { + return; + } + $event = array(); $event['user'] = $user->toJSON(); $event['value'] = $value;