Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ flagship
/src/Api/HIT_LOG
/.phpunit.cache
demo-test.php
docker-compose.demo.yml
docker-compose.demo.yml
12 changes: 10 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{
"php.version": "8.1"
}
"php.version": "8.1",
"phpcs.executablePath": "./vendor/bin/phpcs",
"phpcs.standard": "PSR12",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "always"
},
"php-cs-fixer.executablePath": "./vendor/bin/php-cs-fixer",
"php-cs-fixer.onsave": true,
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"scripts": {
"test": "phpunit",
"check-style": "phpcs --standard=PSR12 -n src tests",
"fix-style": "phpcbf --standard=PSR12 src tests"
"fix-style": "phpcbf --standard=phpcs.xml src tests"
}
}
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0"?>
<ruleset name="Flagship SDK Coding Standards">
<description>PHP CodeSniffer configuration for the Flagship SDK project.</description>

<!-- Define the coding standard to use -->
<rule ref="PSR12" />

<!-- Include all PHP files in the src and tests directories -->
<file>src</file>
<file>tests</file>

<!-- Exclude specific directories or files if needed -->
<exclude-pattern>vendor</exclude-pattern>

<!-- Custom rules or override existing rules -->
<rule ref="Generic.Commenting.DocComment.Missing">
<severity>5</severity>
</rule>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120" />
<property name="absoluteLineLimit" value="0" />
</properties>
</rule>

<!-- Ensure proper indentation -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4" />
<property name="tabIndent" value="false" />
</properties>
</rule>

<!-- Ensure proper spacing around operators -->
<rule ref="Squiz.WhiteSpace.OperatorSpacing" />

<!-- Ensure proper spacing around control structures -->
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing" />

<!-- Ensure proper spacing around function definitions -->
<!-- <rule ref="Squiz.WhiteSpace.FunctionSpacing" /> -->

<!-- Ensure proper spacing around array declarations -->
<!-- <rule ref="Squiz.Arrays.ArrayDeclaration" /> -->

<!-- Ensure proper spacing around object operators -->
<!-- <rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing" /> -->

<!-- Ensure proper spacing around semicolons -->
<rule ref="Squiz.WhiteSpace.SemicolonSpacing" />

<!-- Ensure proper spacing around cast operators -->
<rule ref="Squiz.WhiteSpace.CastSpacing" />

<!-- Set the tab width to 4 spaces -->
<arg name="tab-width" value="4" />

<!-- Show warnings as well as errors -->
<arg name="warning-severity" value="1" />
<arg name="error-severity" value="1" />
</ruleset>
112 changes: 72 additions & 40 deletions src/Api/BatchingCachingStrategyAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ public function hydrateActivatePoolQueue($key, Activate $hit): void
public function getActivateHeaders(): array
{
return [
FlagshipConstant::HEADER_X_API_KEY => $this->config->getApiKey(),
FlagshipConstant::HEADER_X_API_KEY => $this->config->getApiKey(),
FlagshipConstant::HEADER_X_SDK_VERSION => FlagshipConstant::SDK_VERSION,
FlagshipConstant::HEADER_CONTENT_TYPE => FlagshipConstant::HEADER_APPLICATION_JSON,
FlagshipConstant::HEADER_X_SDK_CLIENT => FlagshipConstant::SDK_LANGUAGE,
FlagshipConstant::HEADER_CONTENT_TYPE => FlagshipConstant::HEADER_APPLICATION_JSON,
FlagshipConstant::HEADER_X_SDK_CLIENT => FlagshipConstant::SDK_LANGUAGE,
];
}

Expand Down Expand Up @@ -298,15 +298,9 @@ protected function onVisitorExposed(Activate $activate): void
}
}

/**
* @return void
*/
protected function sendActivateHit(): void
protected function sendActivateHitBatch(ActivateBatch $activateBatch)
{
$headers = $this->getActivateHeaders();

$activateBatch = new ActivateBatch($this->config, $this->activatePoolQueue);

$requestBody = $activateBatch->toApiKeys();
$url = FlagshipConstant::BASE_API_URL . '/' . FlagshipConstant::URL_ACTIVATE_MODIFICATION;
$now = $this->getNow();
Expand All @@ -323,7 +317,8 @@ protected function sendActivateHit(): void
FlagshipConstant::HIT_SENT_SUCCESS,
[
FlagshipConstant::SEND_ACTIVATE,
$this->getLogFormat(null, $url, $requestBody, $headers, $this->getNow() - $now)]
$this->getLogFormat(null, $url, $requestBody, $headers, $this->getNow() - $now),
]
);

$hitKeysToRemove = [];
Expand All @@ -342,30 +337,53 @@ protected function sendActivateHit(): void
$this->cacheHit($this->activatePoolQueue);
$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::SEND_ACTIVATE_HIT_ROUTE_ERROR)
->setLogLevel(LogLevel::ERROR)
->setVisitorId($this->flagshipInstanceId)
->setFlagshipInstanceId($this->flagshipInstanceId)
->setTraffic(100)
->setConfig($this->config)
->setHttpRequestBody($requestBody)
->setHttpRequestHeaders($headers)
->setHttpRequestMethod("POST")
->setHttpRequestUrl($url)
->setHttpResponseBody($exception->getMessage())
->setHttpResponseTime($this->getNow() - $now)
;
->setFlagshipInstanceId($this->flagshipInstanceId)
->setLogLevel(LogLevel::ERROR)
->setTraffic(100)->setConfig($this->config)
->setVisitorId($this->flagshipInstanceId);

$this->addTroubleshootingHit($troubleshooting);
$this->sendTroubleshootingQueue();
$this->logErrorSprintf(
$this->config,
FlagshipConstant::TRACKING_MANAGER,
FlagshipConstant::UNEXPECTED_ERROR_OCCURRED,
[FlagshipConstant::SEND_ACTIVATE,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, $headers, $this->getNow() - $now)]
[
FlagshipConstant::SEND_ACTIVATE,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, $headers, $this->getNow() - $now),
]
);
}
}

/**
* @return void
*/
protected function sendActivateHit(): void
{
$filteredItems = array_filter($this->activatePoolQueue, function ($item) {
return $this->getNow() - $item->getCreatedAt() < FlagshipConstant::DEFAULT_HIT_CACHE_TIME_MS;
});


if (empty($filteredItems)) {
return;
}

$batches = array_chunk($filteredItems, FlagshipConstant::MAX_ACTIVATE_HIT_PER_BATCH);

foreach ($batches as $batch) {
$activateBatch = new ActivateBatch($this->config, $batch);
$this->sendActivateHitBatch($activateBatch);
}
}

/**
* @param string $visitorId
* @return string []
Expand Down Expand Up @@ -440,7 +458,7 @@ public function sendBatch(): void
}

$header = [
FlagshipConstant::HEADER_CONTENT_TYPE => FlagshipConstant::HEADER_APPLICATION_JSON
FlagshipConstant::HEADER_CONTENT_TYPE => FlagshipConstant::HEADER_APPLICATION_JSON,
];

$requestBody = $batchHit->toApiKeys();
Expand All @@ -458,7 +476,8 @@ public function sendBatch(): void
FlagshipConstant::HIT_SENT_SUCCESS,
[
FlagshipConstant::SEND_BATCH,
$this->getLogFormat(null, $url, $requestBody, $header, $this->getNow() - $now)]
$this->getLogFormat(null, $url, $requestBody, $header, $this->getNow() - $now),
]
);

$this->hitsPoolQueue = [];
Expand All @@ -470,25 +489,25 @@ public function sendBatch(): void
$troubleshooting = new Troubleshooting();
$troubleshooting->setLabel(TroubleshootingLabel::SEND_BATCH_HIT_ROUTE_RESPONSE_ERROR)
->setLogLevel(LogLevel::ERROR)
->setVisitorId($this->flagshipInstanceId)
->setFlagshipInstanceId($this->flagshipInstanceId)
->setTraffic(100)
->setConfig($this->config)
->setHttpRequestBody($requestBody)
->setHttpRequestHeaders($header)
->setHttpRequestMethod("POST")
->setHttpRequestUrl($url)
->setHttpResponseBody($exception->getMessage())
->setHttpResponseTime($this->getNow() - $now)
;
->setTraffic(100)->setConfig($this->config)
->setVisitorId($this->flagshipInstanceId);
$this->addTroubleshootingHit($troubleshooting);
$this->sendTroubleshootingQueue();
$this->logErrorSprintf(
$this->config,
FlagshipConstant::TRACKING_MANAGER,
FlagshipConstant::UNEXPECTED_ERROR_OCCURRED,
[FlagshipConstant::SEND_BATCH,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, $header, $this->getNow() - $now)]
[
FlagshipConstant::SEND_BATCH,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, $header, $this->getNow() - $now),
]
);
}
}
Expand All @@ -510,13 +529,13 @@ public function cacheHit(array $hits): void
foreach ($hits as $hit) {
$hitData = [
HitCacheFields::VERSION => 1,
HitCacheFields::DATA => [
HitCacheFields::VISITOR_ID => $hit->getVisitorId(),
HitCacheFields::DATA => [
HitCacheFields::VISITOR_ID => $hit->getVisitorId(),
HitCacheFields::ANONYMOUS_ID => $hit->getAnonymousId(),
HitCacheFields::TYPE => $hit->getType(),
HitCacheFields::CONTENT => $hit->toArray(),
HitCacheFields::TIME => $this->getNow()
]
HitCacheFields::TYPE => $hit->getType(),
HitCacheFields::CONTENT => $hit->toArray(),
HitCacheFields::TIME => $this->getNow(),
],
];

$data[$hit->getKey()] = $hitData;
Expand All @@ -535,7 +554,10 @@ public function cacheHit(array $hits): void
$this->config,
FlagshipConstant::PROCESS_CACHE,
FlagshipConstant::HIT_CACHE_ERROR,
["cacheHit", $exception->getMessage()]
[
"cacheHit",
$exception->getMessage(),
]
);
}
}
Expand Down Expand Up @@ -564,7 +586,10 @@ public function flushHits(array $hitKeys): void
$this->config,
FlagshipConstant::PROCESS_CACHE,
FlagshipConstant::HIT_CACHE_ERROR,
["flushHits", $exception->getMessage()]
[
"flushHits",
$exception->getMessage(),
]
);
}
}
Expand All @@ -586,7 +611,10 @@ public function flushAllHits(): void
$this->config,
FlagshipConstant::PROCESS_CACHE,
FlagshipConstant::HIT_CACHE_ERROR,
["flushAllHits", $exception->getMessage()]
[
"flushAllHits",
$exception->getMessage(),
]
);
}
}
Expand Down Expand Up @@ -656,8 +684,10 @@ protected function sendTroubleshooting(Troubleshooting $hit): void
$this->config,
FlagshipConstant::SEND_TROUBLESHOOTING,
FlagshipConstant::UNEXPECTED_ERROR_OCCURRED,
[FlagshipConstant::SEND_TROUBLESHOOTING,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, [], $this->getNow() - $now)]
[
FlagshipConstant::SEND_TROUBLESHOOTING,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, [], $this->getNow() - $now),
]
);
}
}
Expand Down Expand Up @@ -703,8 +733,10 @@ public function sendUsageHit(UsageHit $hit): void
$this->config,
FlagshipConstant::SEND_USAGE_HIT,
FlagshipConstant::UNEXPECTED_ERROR_OCCURRED,
[FlagshipConstant::SEND_USAGE_HIT,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, [], $this->getNow() - $now)]
[
FlagshipConstant::SEND_USAGE_HIT,
$this->getLogFormat($exception->getMessage(), $url, $requestBody, [], $this->getNow() - $now),
]
);
}
}
Expand Down
Loading