|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AmplitudeExperiment\Amplitude; |
| 4 | + |
| 5 | +use AmplitudeExperiment\Backoff; |
| 6 | +use GuzzleHttp\Client; |
| 7 | +use GuzzleHttp\Promise\PromiseInterface; |
| 8 | +use Monolog\Logger; |
| 9 | +use function AmplitudeExperiment\initializeLogger; |
| 10 | + |
| 11 | +require_once __DIR__ . '/../Util.php'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Amplitude client for sending events to Amplitude. |
| 15 | + */ |
| 16 | +class Amplitude |
| 17 | +{ |
| 18 | + private string $apiKey; |
| 19 | + protected array $queue = []; |
| 20 | + protected Client $httpClient; |
| 21 | + private Logger $logger; |
| 22 | + private ?AmplitudeConfig $config; |
| 23 | + |
| 24 | + public function __construct(string $apiKey, bool $debug, AmplitudeConfig $config = null) |
| 25 | + { |
| 26 | + $this->apiKey = $apiKey; |
| 27 | + $this->httpClient = new Client(); |
| 28 | + $this->logger = initializeLogger($debug); |
| 29 | + $this->config = $config ?? AmplitudeConfig::builder()->build(); |
| 30 | + } |
| 31 | + |
| 32 | + public function flush(): PromiseInterface |
| 33 | + { |
| 34 | + $payload = ["api_key" => $this->apiKey, "events" => $this->queue, "options" => ["min_id_length" => $this->config->minIdLength]]; |
| 35 | + |
| 36 | + // Fetch initial flag configs and await the result. |
| 37 | + return Backoff::doWithBackoff( |
| 38 | + function () use ($payload) { |
| 39 | + return $this->post($this->config->serverUrl, $payload)->then( |
| 40 | + function () { |
| 41 | + $this->queue = []; |
| 42 | + } |
| 43 | + ); |
| 44 | + }, |
| 45 | + new Backoff($this->config->flushMaxRetries, 1, 1, 1) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function logEvent(Event $event) |
| 50 | + { |
| 51 | + $this->queue[] = $event->toArray(); |
| 52 | + if (count($this->queue) >= $this->config->flushQueueSize) { |
| 53 | + $this->flush()->wait(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Flush the queue when the client is destructed. |
| 59 | + */ |
| 60 | + public function __destruct() |
| 61 | + { |
| 62 | + if (count($this->queue) > 0) { |
| 63 | + $this->flush()->wait(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private function post(string $url, array $payload): PromiseInterface |
| 68 | + { |
| 69 | + // Using sendAsync to make an asynchronous request |
| 70 | + $promise = $this->httpClient->postAsync($url, [ |
| 71 | + 'json' => $payload, |
| 72 | + ]); |
| 73 | + |
| 74 | + return $promise->then( |
| 75 | + function ($response) use ($payload) { |
| 76 | + // Process the successful response if needed |
| 77 | + $this->logger->debug("[Amplitude] Event sent successfully: " . json_encode($payload)); |
| 78 | + }, |
| 79 | + function (\Exception $exception) use ($payload) { |
| 80 | + // Handle the exception for async request |
| 81 | + $this->logger->error('[Amplitude] Failed to send event: ' . json_encode($payload) . ', ' . $exception->getMessage()); |
| 82 | + throw $exception; |
| 83 | + } |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments