Skip to content

Commit

Permalink
improve data setter plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
moriony committed Sep 29, 2014
1 parent 66960dd commit ddc2958
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Moriony/Google/Analytics/MeasurementProtocol/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Moriony\Google\Analytics\MeasurementProtocol\Hit\Social;
use Moriony\Google\Analytics\MeasurementProtocol\Hit\Timing;
use Moriony\Google\Analytics\MeasurementProtocol\Hit\Transaction;
use Moriony\Google\Analytics\MeasurementProtocol\Plugin\DefaultData;
use Moriony\Google\Analytics\MeasurementProtocol\Plugin\DataSetter;
use Moriony\Google\Analytics\MeasurementProtocol\Plugin\PluginInterface;

class Client
Expand All @@ -38,7 +38,7 @@ public function __construct(array $options)
$this->client = MeasurementProtocolClient::factory(array(
'ssl' => $options->get(self::OPT_SSL),
));
$this->registerPlugin(new DefaultData(array(
$this->registerPlugin(new DataSetter(array(
HitInterface::FIELD_VERSION => 1,
HitInterface::FIELD_TRACKING_ID => $options->get(self::OPT_TRACKING_ID)
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Krizon\Google\Analytics\MeasurementProtocol\MeasurementProtocolClient;

class DefaultData implements PluginInterface
class DataSetter implements PluginInterface
{
protected $data;

public function __construct(array $data)
public function __construct(array $data, $force = false)
{
$this->data = $data;
$this->force = $force;
}

/**
Expand All @@ -19,11 +20,12 @@ public function __construct(array $data)
public function register($client)
{
$data = & $this->data;
$client->getEventDispatcher()->addListener('command.before_prepare', function ($e) use($data) {
$force = & $this->force;
$client->getEventDispatcher()->addListener('command.before_prepare', function ($e) use($data, $force) {
/** @var \Guzzle\Service\Command\OperationCommand $command */
$command = $e['command'];
foreach ($data as $key => $val) {
if (!$command->hasKey($key)) {
if (!$command->hasKey($key) || $force) {
$command->set($key, $val);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Guzzle\Service\Command\OperationCommand;
use Guzzle\Common\Event;

class DefaultDataTest extends GuzzleTestCase
class DataSetterTest extends GuzzleTestCase
{
/** @var MeasurementProtocolClient $client */
protected $client;
Expand All @@ -19,7 +19,7 @@ public function setUp()

public function testSuccessfulSet()
{
$plugin = new DefaultData(array(
$plugin = new DataSetter(array(
'test_key' => 'test_value'
));
$command = new OperationCommand();
Expand All @@ -33,7 +33,7 @@ public function testSuccessfulSet()

public function testAlreadyStated()
{
$plugin = new DefaultData(array(
$plugin = new DataSetter(array(
'test_key' => 'test_value'
));
$command = new OperationCommand();
Expand All @@ -45,4 +45,19 @@ public function testAlreadyStated()

$this->assertSame('already_stated_value', $command->get('test_key'));
}

public function testForceSet()
{
$plugin = new DataSetter(array(
'test_key' => 'test_value'
), true);
$command = new OperationCommand();
$command->set('test_key', 'already_stated_value');
$event = new Event(array('command' => $command));

$plugin->register($this->client);
$this->client->getEventDispatcher()->dispatch('command.before_prepare', $event);

$this->assertSame('test_value', $command->get('test_key'));
}
}

0 comments on commit ddc2958

Please sign in to comment.