Skip to content

Commit

Permalink
Make timeout configurable (#44)
Browse files Browse the repository at this point in the history
* make timeout configurable

* change version 3.0.3

* formatting

* add version to workflow

* format

* format

* try removing standard

* try ignore

* change standards

* change scope
  • Loading branch information
EDsCODE authored Mar 21, 2023
1 parent 7e69338 commit 26847b8
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text
version: 9.6
env:
XDEBUG_MODE: coverage

Expand All @@ -45,3 +46,4 @@ jobs:
files: "**.php" # you may customize glob as needed
phpcs_path: php phpcs.phar
standard: phpcs.xml
scope: "file"
43 changes: 34 additions & 9 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ class Client
* @param array $options array of consumer options [optional]
* @param HttpClient|null $httpClient
*/
public function __construct(string $apiKey, array $options = [], ?HttpClient $httpClient = null, string $personalAPIKey = null)
{
public function __construct(
string $apiKey,
array $options = [],
?HttpClient $httpClient = null,
string $personalAPIKey = null
) {
$this->apiKey = $apiKey;
$this->personalAPIKey = $personalAPIKey;
$Consumer = self::CONSUMERS[$options["consumer"] ?? "lib_curl"];
Expand All @@ -76,7 +80,9 @@ public function __construct(string $apiKey, array $options = [], ?HttpClient $ht
$options['ssl'] ?? true,
(int) ($options['maximum_backoff_duration'] ?? 10000),
false,
$options["debug"] ?? false
$options["debug"] ?? false,
null,
(int) ($options['timeout'] ?? 10000)
);
$this->featureFlags = [];
$this->groupTypeMapping = [];
Expand Down Expand Up @@ -162,7 +168,15 @@ public function isFeatureEnabled(
bool $onlyEvaluateLocally = false,
bool $sendFeatureFlagEvents = true
): null | bool {
$result = $this->getFeatureFlag($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
$result = $this->getFeatureFlag(
$key,
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally,
$sendFeatureFlagEvents
);

if (is_null($result)) {
return $result;
Expand Down Expand Up @@ -339,9 +353,16 @@ private function computeFlagLocally(
* @return array of feature flags
* @throws Exception
*/
public function fetchFeatureVariants(string $distinctId, array $groups = array(), array $personProperties = [], array $groupProperties = []): array
{
$flags = json_decode($this->decide($distinctId, $groups, $personProperties, $groupProperties), true)['featureFlags'] ?? [];
public function fetchFeatureVariants(
string $distinctId,
array $groups = array(),
array $personProperties = [],
array $groupProperties = []
): array {
$flags = json_decode(
$this->decide($distinctId, $groups, $personProperties, $groupProperties),
true
)['featureFlags'] ?? [];
return $flags;
}

Expand Down Expand Up @@ -376,8 +397,12 @@ public function localFlags()
)->getResponse();
}

public function decide(string $distinctId, array $groups = array(), array $personProperties = [], array $groupProperties = [])
{
public function decide(
string $distinctId,
array $groups = array(),
array $personProperties = [],
array $groupProperties = []
) {
$payload = array(
'api_key' => $this->apiKey,
'distinct_id' => $distinctId,
Expand Down
16 changes: 8 additions & 8 deletions lib/FeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ private static function compareFlagConditions($conditionA, $conditionB)

if ($AhasVariantOverride && $BhasVariantOverride) {
return 0;
} else if ($AhasVariantOverride) {
} elseif ($AhasVariantOverride) {
return -1;
} else if ($BhasVariantOverride) {
} elseif ($BhasVariantOverride) {
return 1;
} else {
return 0;
Expand All @@ -155,20 +155,20 @@ public static function matchFeatureFlagProperties($flag, $distinctId, $propertie
$flagConditionsWithIndexes[] = array($value, $i);
$i++;
}
// # Stable sort conditions with variant overrides to the top. This ensures that if overrides are present, they are
// # Stable sort conditions with variant overrides to the top.
// # This ensures that if overrides are present, they are
// # evaluated first, and the variant override is applied to the first matching condition.
usort(
$flagConditionsWithIndexes,
function ($conditionA, $conditionB)
{
function ($conditionA, $conditionB) {
$AhasVariantOverride = isset($conditionA[0]["variant"]);
$BhasVariantOverride = isset($conditionB[0]["variant"]);

if ($AhasVariantOverride && $BhasVariantOverride) {
return $conditionA[1] - $conditionB[1];
} else if ($AhasVariantOverride) {
} elseif ($AhasVariantOverride) {
return -1;
} else if ($BhasVariantOverride) {
} elseif ($BhasVariantOverride) {
return 1;
} else {
return $conditionA[1] - $conditionB[1];
Expand Down
2 changes: 1 addition & 1 deletion lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
bool $compressRequests = false,
bool $debug = false,
?Closure $errorHandler = null,
int $curlTimeoutMilliseconds = 750
int $curlTimeoutMilliseconds = 10000
) {
$this->host = $host;
$this->useSsl = $useSsl;
Expand Down
40 changes: 33 additions & 7 deletions lib/PostHog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class PostHog
{
public const VERSION = '3.0.2';
public const VERSION = '3.0.3';
public const ENV_API_KEY = "POSTHOG_API_KEY";
public const ENV_HOST = "POSTHOG_HOST";

Expand All @@ -19,8 +19,12 @@ class PostHog
* @param Client|null $client
* @throws Exception
*/
public static function init(?string $apiKey = null, ?array $options = [], ?Client $client = null, ?string $personalAPIKey = null): void
{
public static function init(
?string $apiKey = null,
?array $options = [],
?Client $client = null,
?string $personalAPIKey = null
): void {
if (null === $client) {
$apiKey = $apiKey ?: getenv(self::ENV_API_KEY);

Expand Down Expand Up @@ -124,7 +128,15 @@ public static function isFeatureEnabled(
bool $sendFeatureFlagEvents = true
): null | bool {
self::checkClient();
return self::$client->isFeatureEnabled($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
return self::$client->isFeatureEnabled(
$key,
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally,
$sendFeatureFlagEvents
);
}

/**
Expand All @@ -148,7 +160,15 @@ public static function getFeatureFlag(
bool $sendFeatureFlagEvents = true
): null | bool | string {
self::checkClient();
return self::$client->GetFeatureFlag($key, $distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally, $sendFeatureFlagEvents);
return self::$client->GetFeatureFlag(
$key,
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally,
$sendFeatureFlagEvents
);
}

/**
Expand All @@ -169,7 +189,13 @@ public static function getAllFlags(
bool $onlyEvaluateLocally = false
): array {
self::checkClient();
return self::$client->getAllFlags($distinctId, $groups, $personProperties, $groupProperties, $onlyEvaluateLocally);
return self::$client->getAllFlags(
$distinctId,
$groups,
$personProperties,
$groupProperties,
$onlyEvaluateLocally
);
}


Expand Down Expand Up @@ -224,7 +250,7 @@ public static function raw(array $message)
public static function validate($msg, $type)
{
$distinctId = !empty($msg["distinctId"]);
self::assert($distinctId, "PostHog::${type}() requires distinctId");
self::assert($distinctId, "PostHog::{$type}() requires distinctId");
}

/**
Expand Down
10 changes: 9 additions & 1 deletion test/MockedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ public function __construct(
int $curlTimeoutMilliseconds = 750,
array $flagEndpointResponse = []
) {
parent::__construct($host, $useSsl, $maximumBackoffDuration, $compressRequests, $debug, $errorHandler, $curlTimeoutMilliseconds);
parent::__construct(
$host,
$useSsl,
$maximumBackoffDuration,
$compressRequests,
$debug,
$errorHandler,
$curlTimeoutMilliseconds
);
$this->flagEndpointResponse = $flagEndpointResponse;
}

Expand Down
15 changes: 12 additions & 3 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public function testIsFeatureEnabledGroups()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}', FAKE_API_KEY),
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}',
FAKE_API_KEY
),
),
)
);
Expand Down Expand Up @@ -164,7 +167,10 @@ public function testGetFeatureFlagDefault()

public function testGetFeatureFlagGroups()
{
$this->assertEquals("variant-value", PostHog::getFeatureFlag('multivariate-test', 'user-id', array("company" => "id:5")));
$this->assertEquals(
"variant-value",
PostHog::getFeatureFlag('multivariate-test', 'user-id', array("company" => "id:5"))
);

$this->assertEquals(
$this->http_client->calls,
Expand All @@ -175,7 +181,10 @@ public function testGetFeatureFlagGroups()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}', FAKE_API_KEY),
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"}}',
FAKE_API_KEY
),
),
)
);
Expand Down

0 comments on commit 26847b8

Please sign in to comment.