diff --git a/src/Client.php b/src/Client.php index 959617e..790693a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -70,6 +70,9 @@ class Client /** @var Endpoint\Collections */ public $collection; + /** @var Endpoint\Track */ + public $track; + /** * Client constructor. * @param string $apiKey Api Key @@ -92,6 +95,7 @@ public function __construct(string $apiKey, string $siteId, array $options = []) $this->senderIdentities = new Endpoint\SenderIdentities($this); $this->send = new Endpoint\Send($this); $this->collection = new Endpoint\Collections($this); + $this->track = new Endpoint\Track($this); $this->apiKey = $apiKey; $this->siteId = $siteId; diff --git a/src/Endpoint/Track.php b/src/Endpoint/Track.php new file mode 100644 index 0000000..a3b1365 --- /dev/null +++ b/src/Endpoint/Track.php @@ -0,0 +1,54 @@ +mockException('Operation type is required!', 'POST'); + } // @codeCoverageIgnore + + if (!isset($options['action'])) { + $this->mockException('An event action is required!', 'POST'); + } // @codeCoverageIgnore + + if (!isset($options['identifiers'])) { + $this->mockException('Object identifiers is required!', 'POST'); + } // @codeCoverageIgnore + + $path = $this->generatePath('entity'); + $options['endpoint'] = $this->client->getRegion()->trackUri('v2'); + + return $this->client->post($path, $options); + } + + /** + * Send multiple requests + * @see https://docs.customer.io/api/track/#operation/batch + * @param array $options + * @return mixed + * @throws GuzzleException + */ + public function batch(array $options) + { + if (!isset($options['batch'])) { + $this->mockException('Batch paremeter is required!', 'POST'); + } // @codeCoverageIgnore + + $path = $this->generatePath('batch'); + $options['endpoint'] = $this->client->getRegion()->trackUri('v2'); + + return $this->client->post($path, $options); + } +} diff --git a/src/Region/RegionEu.php b/src/Region/RegionEu.php index 86c1dc3..4ab90df 100644 --- a/src/Region/RegionEu.php +++ b/src/Region/RegionEu.php @@ -6,9 +6,9 @@ class RegionEu implements RegionInterface { - public function trackUri(): string + public function trackUri(string $version = 'v1'): string { - return 'https://track-eu.customer.io/api/v1/'; + return 'https://track-eu.customer.io/api/'.$version.'/'; } public function apiUri(): string diff --git a/src/Region/RegionInterface.php b/src/Region/RegionInterface.php index 745148b..c36a254 100644 --- a/src/Region/RegionInterface.php +++ b/src/Region/RegionInterface.php @@ -11,7 +11,7 @@ interface RegionInterface * * @return string */ - public function trackUri(): string; + public function trackUri(string $version = 'v1'): string; /** * API diff --git a/src/Region/RegionUs.php b/src/Region/RegionUs.php index cf196ee..54846eb 100644 --- a/src/Region/RegionUs.php +++ b/src/Region/RegionUs.php @@ -6,9 +6,9 @@ class RegionUs implements RegionInterface { - public function trackUri(): string + public function trackUri(string $version = 'v1'): string { - return 'https://track.customer.io/api/v1/'; + return 'https://track.customer.io/api/'.$version.'/'; } public function apiUri(): string diff --git a/tests/TrackTest.php b/tests/TrackTest.php new file mode 100644 index 0000000..6dbc429 --- /dev/null +++ b/tests/TrackTest.php @@ -0,0 +1,49 @@ +getMockBuilder('Customerio\Client')->disableOriginalConstructor()->getMock(); + $stub->method('post')->willReturn('foo'); + $events = new Track($stub); + $this->assertEquals('foo', $events->entity([ + 'type' => 'person', + 'identifiers' => [ + 'id' => '12345', + ], + 'action' => 'event', + 'name' => 'test new event', + ])); + } + + public function testBatchEntity() + { + $stub = $this->getMockBuilder('Customerio\Client')->disableOriginalConstructor()->getMock(); + $stub->method('post')->willReturn('foo'); + $events = new Track($stub); + $this->assertEquals('foo', $events->batch( + ["batch" => [[ + 'type' => 'person', + 'identifiers' => [ + 'id' => 'ziT8aeAcSdYBKvYCzx', + ], + 'action' => 'event', + 'name' => 'test new event', + ], + [ + 'type' => 'person', + 'identifiers' => [ + 'id' => 'ziT8aeAcSdYBKvYCzx', + ], + 'action' => 'event', + 'name' => 'test new event 2', + ]]] + )); + } +}