Skip to content
Merged
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
97 changes: 51 additions & 46 deletions src/LaunchDarkly/EventProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,57 @@
/**
* @internal
*/
class EventProcessor {
class EventProcessor
{
private $_eventPublisher;
private $_queue;
private $_capacity;
private $_timeout;

private $_eventPublisher;
private $_queue;
private $_capacity;
private $_timeout;
public function __construct($sdkKey, $options = array())
{
$this->_eventPublisher = $this->getEventPublisher($sdkKey, $options);

public function __construct($sdkKey, $options = array()) {
$this->_eventPublisher = $this->getEventPublisher($sdkKey, $options);
$this->_capacity = $options['capacity'];
$this->_timeout = $options['timeout'];

$this->_capacity = $options['capacity'];
$this->_timeout = $options['timeout'];

$this->_queue = array();
}

public function __destruct() {
$this->flush();
}
$this->_queue = array();
}

public function sendEvent($event) {
return $this->enqueue($event);
}
public function __destruct()
{
$this->flush();
}

public function enqueue($event) {
if (count($this->_queue) > $this->_capacity) {
return false;
public function sendEvent($event)
{
return $this->enqueue($event);
}

array_push($this->_queue, $event);
public function enqueue($event)
{
if (count($this->_queue) > $this->_capacity) {
return false;
}

return true;
}
array_push($this->_queue, $event);

return true;
}

/**
* Publish events to LaunchDarkly
* @return bool Whether the events were successfully published
*/
public function flush() {
if (empty($this->_queue)) {
return null;
}
public function flush()
{
if (empty($this->_queue)) {
return null;
}

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

return $this->_eventPublisher->publish($payload);
return $this->_eventPublisher->publish($payload);
}

/**
Expand All @@ -59,19 +64,19 @@ public function flush() {
*/
private function getEventPublisher($sdkKey, array $options)
{
if (isset($options['event_publisher']) && $options['event_publisher'] instanceof EventPublisher) {
return $options['event_publisher'];
}

if (isset($options['event_publisher_class'])) {
$eventPublisherClass = $options['event_publisher_class'];
} else {
$eventPublisherClass = CurlEventPublisher::class;
}

if (!is_a($eventPublisherClass, EventPublisher::class, true)) {
throw new \InvalidArgumentException;
}
return new $eventPublisherClass($sdkKey, $options);
if (isset($options['event_publisher']) && $options['event_publisher'] instanceof EventPublisher) {
return $options['event_publisher'];
}

if (isset($options['event_publisher_class'])) {
$eventPublisherClass = $options['event_publisher_class'];
} else {
$eventPublisherClass = CurlEventPublisher::class;
}

if (!is_a($eventPublisherClass, EventPublisher::class, true)) {
throw new \InvalidArgumentException;
}
return new $eventPublisherClass($sdkKey, $options);
}
}
}