|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SegmentIO; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use GuzzleHttp\Collection; |
|
7
|
|
|
use GuzzleHttp\Command\Guzzle\GuzzleClient; |
|
8
|
|
|
use GuzzleHttp\Command\Guzzle\Description; |
|
9
|
|
|
use SegmentIO\Subscriber\BatchFileSubscriber; |
|
10
|
|
|
use SegmentIO\Subscriber\BatchRequestSubscriber; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Web Service Client for Segment.io |
|
14
|
|
|
* |
|
15
|
|
|
* @author Keith Kirk <[email protected]> |
|
16
|
|
|
* |
|
17
|
|
|
* @method array identify(array $data = []) |
|
18
|
|
|
* @method array alias(array $data = []) |
|
19
|
|
|
* @method array group(array $data = []) |
|
20
|
|
|
* @method array track(array $data = []) |
|
21
|
|
|
* @method array page(array $data = []) |
|
22
|
|
|
* @method array screen(array $data = []) |
|
23
|
|
|
* @method array import(array $data = []) |
|
24
|
|
|
*/ |
|
25
|
|
|
class Client extends GuzzleClient |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* PHP Client Version |
|
29
|
|
|
*/ |
|
30
|
|
|
const VERSION = '1.1.0'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $config |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(array $config = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$defaults = [ |
|
40
|
|
|
'write_key' => null, |
|
41
|
|
|
'version' => 'v1', |
|
42
|
|
|
'batching' => 'request', |
|
43
|
|
|
'log_file' => null, |
|
44
|
|
|
'max_queue_size' => 10000, |
|
45
|
|
|
'batch_size' => 100 |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
// Create Configuration |
|
49
|
|
|
$config = Collection::fromConfig($config, $defaults, ['write_key', 'version', 'batching']); |
|
50
|
|
|
|
|
51
|
|
|
// Load versioned Service Description |
|
52
|
|
|
$description = $this->loadServiceDescription( |
|
53
|
|
|
__DIR__ . '/Description/segment.io.%s.php', $config->get('version') |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
// Allow the Adapter to be set |
|
57
|
|
|
$httpConfig = $config->hasKey('adapter') ? ['adapter' => $config->get('adapter')] : []; |
|
58
|
|
|
|
|
59
|
|
|
// Create the Client |
|
60
|
|
|
parent::__construct(new HttpClient($httpConfig), $description, $config->toArray()); |
|
61
|
|
|
|
|
62
|
|
|
// Set Basic Auth |
|
63
|
|
|
$this->getHttpClient()->setDefaultOption('auth', [$config->get('write_key'), null]); |
|
64
|
|
|
// Set the content type header to use "application/json" for all requests |
|
65
|
|
|
$this->getHttpClient()->setDefaultOption('headers', array('Content-Type' => 'application/json')); |
|
66
|
|
|
|
|
67
|
|
|
// Default the Version |
|
68
|
|
|
$this->setConfig('defaults/version', $this->getDescription()->getApiVersion()); |
|
69
|
|
|
|
|
70
|
|
|
if ($config->get('batching') == 'request') { |
|
71
|
|
|
$this->getEmitter()->attach(new BatchRequestSubscriber($this->getDescription(), [ |
|
72
|
|
|
'max_queue_size' => $config->get('max_queue_size'), |
|
73
|
|
|
'batch_size' => $config->get('batch_size') |
|
74
|
|
|
])); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($config->get('batching') == 'file') { |
|
78
|
|
|
$this->getEmitter()->attach(new BatchFileSubscriber($this->getDescription(), [ |
|
79
|
|
|
'filename' => $config->get('log_file') |
|
80
|
|
|
])); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Loads the Service Description from the given file path |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $filepath The Service Description filepath |
|
88
|
|
|
* @param string $version The API Version |
|
89
|
|
|
* |
|
90
|
|
|
* @return Description |
|
91
|
|
|
*/ |
|
92
|
|
|
public function loadServiceDescription($filepath, $version) |
|
93
|
|
|
{ |
|
94
|
|
|
$filepath = sprintf($filepath, $version); |
|
95
|
|
|
$description = file_exists($filepath) ? include $filepath : null; |
|
96
|
|
|
|
|
97
|
|
|
if (!is_array($description)) { |
|
98
|
|
|
throw new \InvalidArgumentException('Invalid Service Description!'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return new Description($description); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|