|
1 | 1 | <?php |
2 | 2 |
|
3 | | -require_once __DIR__.'/vendor/autoload.php'; |
| 3 | +declare(strict_types=1); |
4 | 4 |
|
5 | | -$host = getenv('FP_API_HOST'); |
6 | | -$api_key = getenv('FP_PRIVATE_API_KEY'); |
| 5 | +require_once __DIR__.'/vendor/autoload.php'; |
7 | 6 |
|
| 7 | +use Dotenv\Dotenv; |
8 | 8 | use Fingerprint\ServerAPI\Api\FingerprintApi; |
9 | 9 | use Fingerprint\ServerAPI\Configuration; |
10 | 10 | use Fingerprint\ServerAPI\Model\EventsGetResponse; |
|
14 | 14 | use Fingerprint\ServerAPI\Webhook\WebhookVerifier; |
15 | 15 | use GuzzleHttp\Client; |
16 | 16 |
|
17 | | -$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); |
18 | | - |
| 17 | +$dotenv = Dotenv::createImmutable(__DIR__); |
19 | 18 | $dotenv->safeLoad(); |
20 | 19 |
|
21 | | -$api_key = $_ENV['FP_PRIVATE_API_KEY'] ?? getenv('FP_PRIVATE_API_KEY') ?? 'Private API Key not defined'; |
22 | | -$visitor_id_to_delete = $_ENV['FP_VISITOR_ID_TO_DELETE'] ?? getenv('FP_VISITOR_ID_TO_DELETE') ?? false; |
23 | | -$request_id_to_update = $_ENV['FP_REQUEST_ID_TO_UPDATE'] ?? getenv('FP_REQUEST_ID_TO_UPDATE') ?? false; |
24 | | -$region_env = $_ENV['FP_REGION'] ?? getenv('FP_REGION') ?? 'us'; |
| 20 | +function env(string $key, ?string $default = null): ?string |
| 21 | +{ |
| 22 | + return $_ENV[$key] ?? getenv($key) ?: $default; |
| 23 | +} |
25 | 24 |
|
26 | | -$region = Configuration::REGION_GLOBAL; |
27 | | -if ('eu' === $region_env) { |
28 | | - $region = Configuration::REGION_EUROPE; |
29 | | -} elseif ('ap' === $region_env) { |
30 | | - $region = Configuration::REGION_ASIA; |
| 25 | +$apiKey = env('FP_PRIVATE_API_KEY'); |
| 26 | +if (!$apiKey) { |
| 27 | + throw new Exception('FP_PRIVATE_API_KEY is not defined.'); |
31 | 28 | } |
32 | 29 |
|
| 30 | +$regionEnv = env('FP_REGION', 'us'); |
| 31 | +$visitorIdToDelete = env('FP_VISITOR_ID_TO_DELETE'); |
| 32 | +$requestIdToUpdate = env('FP_REQUEST_ID_TO_UPDATE'); |
| 33 | + |
| 34 | +$region = match(strtolower(trim($regionEnv))) { |
| 35 | + 'eu' => Configuration::REGION_EUROPE, |
| 36 | + 'ap' => Configuration::REGION_ASIA, |
| 37 | + default => Configuration::REGION_GLOBAL, |
| 38 | +}; |
| 39 | + |
33 | 40 | $config = Configuration::getDefaultConfiguration( |
34 | | - $api_key, |
| 41 | + $apiKey, |
35 | 42 | $region, |
36 | 43 | ); |
37 | 44 |
|
|
40 | 47 | $config |
41 | 48 | ); |
42 | 49 |
|
43 | | -// Temporarily suppress a million deprecated ArrayAccess return type warnings for readability |
44 | | -// Our SDK generator does not yet support PHP's new attributes system |
45 | | -// https://github.com/swagger-api/swagger-codegen/issues/11820 |
46 | | -error_reporting(error_reporting() & ~E_DEPRECATED); |
| 50 | +$start = (new DateTime())->sub(new DateInterval('P3M')); |
| 51 | +$end = new DateTime(); |
47 | 52 |
|
48 | 53 | // FingerprintApi->searchEvents usage example |
49 | 54 | try { |
50 | | - // 3 month from now |
51 | | - $start = (new DateTime())->sub(new DateInterval('P3M')); |
52 | | - $end = new DateTime(); |
53 | | - |
54 | 55 | /** @var SearchEventsResponse $result */ |
55 | 56 | list($result, $response) = $client->searchEvents(10, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000); |
56 | 57 | if (!is_countable($result->getEvents()) || count($result->getEvents()) === 0) { |
57 | 58 | throw new Exception('No events found'); |
58 | 59 | } |
59 | | - $identification_data = $result->getEvents()[0]->getProducts()->getIdentification()->getData(); |
60 | | - $visitor_id = $identification_data->getVisitorId(); |
61 | | - $request_id = $identification_data->getRequestId(); |
| 60 | + $identificationData = $result->getEvents()[0]->getProducts()->getIdentification()->getData(); |
| 61 | + $visitorId = $identificationData->getVisitorId(); |
| 62 | + $requestId = $identificationData->getRequestId(); |
62 | 63 | fwrite(STDOUT, sprintf("\n\nGot events: %s \n", $response->getBody()->getContents())); |
63 | 64 | } catch (Exception $e) { |
64 | 65 | fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->searchEvents: %s\n", $e->getMessage())); |
|
69 | 70 | // FingerprintApi->getVisits usage example |
70 | 71 | try { |
71 | 72 | /** @var VisitorsGetResponse $result */ |
72 | | - list($result, $response) = $client->getVisits($visitor_id); |
73 | | - if ($result->getVisitorId() !== $visitor_id) { |
| 73 | + list($result, $response) = $client->getVisits($visitorId); |
| 74 | + if ($result->getVisitorId() !== $visitorId) { |
74 | 75 | throw new Exception('Argument visitorId is not equal to deserialized getVisitorId'); |
75 | 76 | } |
76 | 77 | fwrite(STDOUT, sprintf("Got visits: %s \n", $response->getBody()->getContents())); |
|
81 | 82 | } |
82 | 83 |
|
83 | 84 | // FingerprintApi->deleteVisitorData usage example |
84 | | -if ($visitor_id_to_delete) { |
| 85 | +if ($visitorIdToDelete) { |
85 | 86 | try { |
86 | | - list($model, $response) = $client->deleteVisitorData($visitor_id_to_delete); |
| 87 | + list($model, $response) = $client->deleteVisitorData($visitorIdToDelete); |
87 | 88 | fwrite(STDOUT, sprintf("Visitor data deleted: %s \n", $response->getBody()->getContents())); |
88 | 89 | } catch (Exception $e) { |
89 | 90 | fwrite(STDERR, sprintf("Exception when calling FingerprintApi->deleteVisitorData: %s\n", $e->getMessage())); |
|
94 | 95 | // FingerprintApi->getEvent usage example |
95 | 96 | try { |
96 | 97 | /** @var EventsGetResponse $result */ |
97 | | - list($result, $response) = $client->getEvent($request_id); |
98 | | - if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $request_id) { |
| 98 | + list($result, $response) = $client->getEvent($requestId); |
| 99 | + if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $requestId) { |
99 | 100 | throw new Exception('Argument requestId is not equal to deserialized getRequestId'); |
100 | 101 | } |
101 | 102 | fwrite(STDOUT, sprintf("\n\nGot event: %s \n", $response->getBody()->getContents())); |
|
106 | 107 | } |
107 | 108 |
|
108 | 109 | // FingerprintApi->updateEvent usage example |
109 | | -if ($request_id_to_update) { |
| 110 | +if ($requestIdToUpdate) { |
110 | 111 | try { |
111 | 112 | $body = new EventsUpdateRequest([ |
112 | 113 | 'linked_id' => date('Y-m-d H:i:s'), |
113 | 114 | ]); |
114 | | - list($model, $response) = $client->updateEvent($body, $request_id_to_update); |
| 115 | + list($model, $response) = $client->updateEvent($body, $requestIdToUpdate); |
115 | 116 | fwrite(STDOUT, sprintf("\n\nEvent updated: %s \n", $response->getBody()->getContents())); |
116 | 117 | } catch (Exception $e) { |
117 | | - fwrite("\n\nException when calling FingerprintApi->updateEvent: %s\n", $e->getMessage()); |
| 118 | + fwrite(STDOUT, sprintf("\n\nException when calling FingerprintApi->updateEvent: %s\n", $e->getMessage())); |
118 | 119 | exit(1); |
119 | 120 | } |
120 | 121 | } |
121 | 122 |
|
122 | 123 | // Call API asynchronously examples |
123 | | -$eventPromise = $client->getEventAsync($request_id); |
124 | | -$eventPromise->then(function ($tuple) use ($request_id) { |
| 124 | +$eventPromise = $client->getEventAsync($requestId); |
| 125 | +$eventPromise->then(function ($tuple) use ($requestId) { |
125 | 126 | list($result, $response) = $tuple; |
126 | | - if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $request_id) { |
| 127 | + if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $requestId) { |
127 | 128 | throw new Exception('Argument requestId is not equal to deserialized getRequestId'); |
128 | 129 | } |
129 | 130 | fwrite(STDOUT, sprintf("\n\nGot async event: %s \n", $response->getBody()->getContents())); |
|
133 | 134 | exit(1); |
134 | 135 | })->wait(); |
135 | 136 |
|
136 | | -$visitsPromise = $client->getVisitsAsync($visitor_id); |
137 | | -$visitsPromise->then(function ($tuple) use ($visitor_id) { |
| 137 | +$visitsPromise = $client->getVisitsAsync($visitorId); |
| 138 | +$visitsPromise->then(function ($tuple) use ($visitorId) { |
138 | 139 | list($result, $response) = $tuple; |
139 | | - if ($result->getVisitorId() !== $visitor_id) { |
| 140 | + if ($result->getVisitorId() !== $visitorId) { |
140 | 141 | throw new Exception('Argument visitorId is not equal to deserialized getVisitorId'); |
141 | 142 | } |
142 | 143 | fwrite(STDOUT, sprintf("\n\nGot async visits: %s \n", $response->getBody()->getContents())); |
|
152 | 153 | $webhookHeader = 'v1=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db'; |
153 | 154 | $isValidWebhookSign = WebhookVerifier::IsValidWebhookSignature($webhookHeader, $webhookData, $webhookSecret); |
154 | 155 | if ($isValidWebhookSign) { |
155 | | - fwrite(STDOUT, sprintf("\n\nVerified webhook signature\n")); |
| 156 | + fwrite(STDOUT, "\n\nVerified webhook signature\n"); |
156 | 157 | } else { |
157 | | - fwrite(STDERR, sprintf("\n\nWebhook signature verification failed\n")); |
| 158 | + fwrite(STDERR, "\n\nWebhook signature verification failed\n"); |
158 | 159 |
|
159 | 160 | exit(1); |
160 | 161 | } |
161 | 162 |
|
162 | 163 | // Check that old events still match expected format |
163 | 164 | try { |
164 | | - list($result_old) = $client->searchEvents(1, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000, reverse: true); |
165 | | - if (!is_countable($result_old->getEvents()) || count($result_old->getEvents()) === 0) { |
| 165 | + list($resultOld) = $client->searchEvents(1, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000, reverse: true); |
| 166 | + if (!is_countable($resultOld->getEvents()) || count($resultOld->getEvents()) === 0) { |
166 | 167 | throw new Exception('No old events found'); |
167 | 168 | } |
168 | | - $identification_data_old = $result_old->getEvents()[0]->getProducts()->getIdentification()->getData(); |
169 | | - $visitor_id_old = $identification_data_old->getVisitorId(); |
170 | | - $request_id_old = $identification_data_old->getRequestId(); |
| 169 | + $identificationDataOld = $resultOld->getEvents()[0]->getProducts()->getIdentification()->getData(); |
| 170 | + $visitorIdOld = $identificationDataOld->getVisitorId(); |
| 171 | + $requestIdOld = $identificationDataOld->getRequestId(); |
171 | 172 |
|
172 | | - if ($visitor_id === $visitor_id_old || $request_id === $request_id_old) { |
| 173 | + if ($requestId === $requestIdOld) { |
173 | 174 | throw new Exception('Old events are identical to new'); |
174 | 175 | } |
175 | 176 |
|
176 | | - list($result, $response) = $client->getEvent($request_id_old); |
177 | | - list($result, $response) = $client->getVisits($visitor_id_old); |
178 | | - fwrite(STDERR, sprintf("\n\nOld events are good\n")); |
| 177 | + list($result, $response) = $client->getEvent($requestIdOld); |
| 178 | + list($result, $response) = $client->getVisits($visitorIdOld); |
| 179 | + fwrite(STDOUT, "\n\nOld events are good\n"); |
179 | 180 | } catch (Exception $e) { |
180 | 181 | fwrite(STDERR, sprintf("\n\nException when trying to read old data: %s\n", $e->getMessage())); |
181 | 182 | } |
182 | 183 |
|
183 | | -// Enable the deprecated ArrayAccess return type warning again if needed |
184 | | -error_reporting(error_reporting() | E_DEPRECATED); |
185 | | - |
186 | 184 | fwrite(STDOUT, "\n\nChecks passed\n"); |
187 | 185 |
|
188 | 186 | exit(0); |
0 commit comments