Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/LaunchDarkly/EventProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function enqueue($event) {
}

protected function flush() {
if (empty($this->_queue)) {
return;
}

$socket = $this->createSocket();

if (!$socket) {
Expand Down
38 changes: 38 additions & 0 deletions src/LaunchDarkly/LDClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class LDClient {
protected $_baseUri;
protected $_client;
protected $_eventProcessor;
protected $_offline;

/**
* Creates a new client instance that connects to LaunchDarkly.
Expand Down Expand Up @@ -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);

Expand All @@ -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.
*
Expand All @@ -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";
Expand All @@ -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;
Expand Down