Skip to content

Commit 7c7d079

Browse files
authored
Merge pull request #190 from fingerprintjs/feat/remove-visitorId-comparison-inter-1473
2 parents 3f1b3d2 + da816ac commit 7c7d079

File tree

1 file changed

+53
-55
lines changed

1 file changed

+53
-55
lines changed

run_checks.php

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
require_once __DIR__.'/vendor/autoload.php';
3+
declare(strict_types=1);
44

5-
$host = getenv('FP_API_HOST');
6-
$api_key = getenv('FP_PRIVATE_API_KEY');
5+
require_once __DIR__.'/vendor/autoload.php';
76

7+
use Dotenv\Dotenv;
88
use Fingerprint\ServerAPI\Api\FingerprintApi;
99
use Fingerprint\ServerAPI\Configuration;
1010
use Fingerprint\ServerAPI\Model\EventsGetResponse;
@@ -14,24 +14,31 @@
1414
use Fingerprint\ServerAPI\Webhook\WebhookVerifier;
1515
use GuzzleHttp\Client;
1616

17-
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
18-
17+
$dotenv = Dotenv::createImmutable(__DIR__);
1918
$dotenv->safeLoad();
2019

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+
}
2524

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.');
3128
}
3229

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+
3340
$config = Configuration::getDefaultConfiguration(
34-
$api_key,
41+
$apiKey,
3542
$region,
3643
);
3744

@@ -40,25 +47,19 @@
4047
$config
4148
);
4249

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();
4752

4853
// FingerprintApi->searchEvents usage example
4954
try {
50-
// 3 month from now
51-
$start = (new DateTime())->sub(new DateInterval('P3M'));
52-
$end = new DateTime();
53-
5455
/** @var SearchEventsResponse $result */
5556
list($result, $response) = $client->searchEvents(10, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000);
5657
if (!is_countable($result->getEvents()) || count($result->getEvents()) === 0) {
5758
throw new Exception('No events found');
5859
}
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();
6263
fwrite(STDOUT, sprintf("\n\nGot events: %s \n", $response->getBody()->getContents()));
6364
} catch (Exception $e) {
6465
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->searchEvents: %s\n", $e->getMessage()));
@@ -69,8 +70,8 @@
6970
// FingerprintApi->getVisits usage example
7071
try {
7172
/** @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) {
7475
throw new Exception('Argument visitorId is not equal to deserialized getVisitorId');
7576
}
7677
fwrite(STDOUT, sprintf("Got visits: %s \n", $response->getBody()->getContents()));
@@ -81,9 +82,9 @@
8182
}
8283

8384
// FingerprintApi->deleteVisitorData usage example
84-
if ($visitor_id_to_delete) {
85+
if ($visitorIdToDelete) {
8586
try {
86-
list($model, $response) = $client->deleteVisitorData($visitor_id_to_delete);
87+
list($model, $response) = $client->deleteVisitorData($visitorIdToDelete);
8788
fwrite(STDOUT, sprintf("Visitor data deleted: %s \n", $response->getBody()->getContents()));
8889
} catch (Exception $e) {
8990
fwrite(STDERR, sprintf("Exception when calling FingerprintApi->deleteVisitorData: %s\n", $e->getMessage()));
@@ -94,8 +95,8 @@
9495
// FingerprintApi->getEvent usage example
9596
try {
9697
/** @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) {
99100
throw new Exception('Argument requestId is not equal to deserialized getRequestId');
100101
}
101102
fwrite(STDOUT, sprintf("\n\nGot event: %s \n", $response->getBody()->getContents()));
@@ -106,24 +107,24 @@
106107
}
107108

108109
// FingerprintApi->updateEvent usage example
109-
if ($request_id_to_update) {
110+
if ($requestIdToUpdate) {
110111
try {
111112
$body = new EventsUpdateRequest([
112113
'linked_id' => date('Y-m-d H:i:s'),
113114
]);
114-
list($model, $response) = $client->updateEvent($body, $request_id_to_update);
115+
list($model, $response) = $client->updateEvent($body, $requestIdToUpdate);
115116
fwrite(STDOUT, sprintf("\n\nEvent updated: %s \n", $response->getBody()->getContents()));
116117
} 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()));
118119
exit(1);
119120
}
120121
}
121122

122123
// 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) {
125126
list($result, $response) = $tuple;
126-
if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $request_id) {
127+
if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $requestId) {
127128
throw new Exception('Argument requestId is not equal to deserialized getRequestId');
128129
}
129130
fwrite(STDOUT, sprintf("\n\nGot async event: %s \n", $response->getBody()->getContents()));
@@ -133,10 +134,10 @@
133134
exit(1);
134135
})->wait();
135136

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) {
138139
list($result, $response) = $tuple;
139-
if ($result->getVisitorId() !== $visitor_id) {
140+
if ($result->getVisitorId() !== $visitorId) {
140141
throw new Exception('Argument visitorId is not equal to deserialized getVisitorId');
141142
}
142143
fwrite(STDOUT, sprintf("\n\nGot async visits: %s \n", $response->getBody()->getContents()));
@@ -152,37 +153,34 @@
152153
$webhookHeader = 'v1=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db';
153154
$isValidWebhookSign = WebhookVerifier::IsValidWebhookSignature($webhookHeader, $webhookData, $webhookSecret);
154155
if ($isValidWebhookSign) {
155-
fwrite(STDOUT, sprintf("\n\nVerified webhook signature\n"));
156+
fwrite(STDOUT, "\n\nVerified webhook signature\n");
156157
} else {
157-
fwrite(STDERR, sprintf("\n\nWebhook signature verification failed\n"));
158+
fwrite(STDERR, "\n\nWebhook signature verification failed\n");
158159

159160
exit(1);
160161
}
161162

162163
// Check that old events still match expected format
163164
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) {
166167
throw new Exception('No old events found');
167168
}
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();
171172

172-
if ($visitor_id === $visitor_id_old || $request_id === $request_id_old) {
173+
if ($requestId === $requestIdOld) {
173174
throw new Exception('Old events are identical to new');
174175
}
175176

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");
179180
} catch (Exception $e) {
180181
fwrite(STDERR, sprintf("\n\nException when trying to read old data: %s\n", $e->getMessage()));
181182
}
182183

183-
// Enable the deprecated ArrayAccess return type warning again if needed
184-
error_reporting(error_reporting() | E_DEPRECATED);
185-
186184
fwrite(STDOUT, "\n\nChecks passed\n");
187185

188186
exit(0);

0 commit comments

Comments
 (0)