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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.phar
test/analytics.log
/.vscode
.phplint-cache
/.idea
27 changes: 22 additions & 5 deletions lib/Segment/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ class Segment_Client {
*
*/
public function __construct($secret, $options = array()) {

$consumers = array(
"socket" => "Segment_Consumer_Socket",
"file" => "Segment_Consumer_File",
"fork_curl" => "Segment_Consumer_ForkCurl",
"lib_curl" => "Segment_Consumer_LibCurl"
"socket" => "Segment_Consumer_Socket",
"file" => "Segment_Consumer_File",
"fork_curl" => "Segment_Consumer_ForkCurl",
"lib_curl" => "Segment_Consumer_LibCurl"
);

// Use our socket libcurl by default
$consumer_type = isset($options["consumer"]) ? $options["consumer"] :
"lib_curl";

if (!array_key_exists($consumer_type, $consumers) && class_exists($consumer_type)) {
if (!is_subclass_of($consumer_type, Segment_Consumer::class)) {
throw new Exception('Consumers must extend the Segment_Consumer abstract class');
}
// Try to resolve it by class name
$this->consumer = new $consumer_type($secret, $options);
return;
}

$Consumer = $consumers[$consumer_type];

$this->consumer = new $Consumer($secret, $options);
Expand Down Expand Up @@ -131,6 +140,14 @@ public function flush() {
return true;
}

/**
* @return Segment_Consumer
*/
public function getConsumer() {
return $this->consumer;
}


/**
* Formats a timestamp by making sure it is set
* and converting it to iso8601.
Expand Down
31 changes: 31 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

require_once __DIR__ . '/../lib/Segment/Client.php';

class ClientTest extends PHPUnit_Framework_TestCase
{
/** @test */
public function it_uses_the_lib_curl_consumer_as_default()
{
$client = new Segment_Client('foobar', []);
$this->assertInstanceOf(Segment_Consumer_LibCurl::class, $client->getConsumer());
}

/** @test */
public function can_provide_the_consumer_configuration_as_string()
{
$client = new Segment_Client('foobar', [
'consumer' => 'fork_curl',
]);
$this->assertInstanceOf(Segment_Consumer_ForkCurl::class, $client->getConsumer());
}

/** @test */
public function can_provide_a_class_namespace_as_consumer_configuration()
{
$client = new Segment_Client('foobar', [
'consumer' => Segment_Consumer_ForkCurl::class,
]);
$this->assertInstanceOf(Segment_Consumer_ForkCurl::class, $client->getConsumer());
}
}