Skip to content
Merged

Q2 #84

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 .github/workflows/CI_COVERAGE.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI PHP 8

on:
push:
branches: [ main]
branches: [ main, q2]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CI_PHP_8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI PHP 8

on:
pull_request:
branches: [ main ]
branches: [ main, q2 ]

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ bucketing.json
flagship
/src/Api/HIT_LOG
/.phpunit.cache
demo-test.php
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 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 @@
}
}

/**
* @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 @@
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 @@
$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;

Check warning on line 376 in src/Api/BatchingCachingStrategyAbstract.php

View check run for this annotation

Codecov / codecov/patch

src/Api/BatchingCachingStrategyAbstract.php#L376

Added line #L376 was not covered by tests
}

$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 @@
}

$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 @@
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 @@
$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 @@
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 @@
$this->config,
FlagshipConstant::PROCESS_CACHE,
FlagshipConstant::HIT_CACHE_ERROR,
["cacheHit", $exception->getMessage()]
[
"cacheHit",
$exception->getMessage(),
]
);
}
}
Expand Down Expand Up @@ -564,7 +586,10 @@
$this->config,
FlagshipConstant::PROCESS_CACHE,
FlagshipConstant::HIT_CACHE_ERROR,
["flushHits", $exception->getMessage()]
[
"flushHits",
$exception->getMessage(),
]
);
}
}
Expand All @@ -586,7 +611,10 @@
$this->config,
FlagshipConstant::PROCESS_CACHE,
FlagshipConstant::HIT_CACHE_ERROR,
["flushAllHits", $exception->getMessage()]
[
"flushAllHits",
$exception->getMessage(),
]
);
}
}
Expand Down Expand Up @@ -656,8 +684,10 @@
$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 @@
$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