From a34d56be6db22aa08753ec56197530cafbd86c15 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Thu, 4 Jul 2024 17:34:10 +0530 Subject: [PATCH 1/2] fix: changelog update [skip-ci] --- CHANGELOG.md | 28 ++++++++++ composer.json | 2 +- src/Api/GetFlag.php | 53 ++++++++++++------- src/Api/SetAttribute.php | 8 +-- src/Api/TrackEvent.php | 10 ++-- src/Models/CampaignModel.php | 10 ++++ src/Models/RuleModel.php | 11 ++++ src/Models/VariationModel.php | 4 ++ src/Packages/Logger/Core/LogManager.php | 2 +- .../Logger/Core/LogTransportManager.php | 9 ++-- .../Logger/Transports/ConsoleTransport.php | 2 +- .../Enums/SegmentOperandValueEnum.php | 4 ++ .../Evaluators/SegmentEvaluator.php | 20 +++---- .../Evaluators/SegmentOperandEvaluator.php | 43 +++++++++++++-- src/Utils/FunctionUtil.php | 1 + src/Utils/GetFlagResultUtil.php | 9 +++- src/Utils/NetworkUtil.php | 6 ++- src/Utils/VWOGatewayServiceUtil.php | 2 +- src/VWO.php | 14 +++-- src/VWOBuilder.php | 14 +++-- src/VWOClient.php | 14 +++-- 21 files changed, 203 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b994513..f7cdcca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,34 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +[1.2.1] - 2024-07-17 +### Added + +- Support for optional parameter SettingsFile in init +- Support for gt,gte,lt,lte in Custom Variable pre-segmentation + + +[1.1.1] - 2024-07-04 +### Changed +- **Testing** + - PHPUnit version changed to support in lower php versions + + +[1.1.0] - 2024-07-02 +### Changed +- **Refactoring** + + - Redesigned models to use private properties instead of public properties. + - Implemented object notation with getter and setter functions for all models. + +- **Unit and E2E Testing** + + - Set up Test framework using `Jest` + - Wrote unit and E2E tests to ensure nothing breaks while pushing new code + - Ensure criticla components are working properly on every build + + [1.0.0] - 2024-06-11 ### Added diff --git a/composer.json b/composer.json index a8e1b8a..818863e 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "vwo/vwo-fme-php-sdk", - "version": "1.2.0", + "version": "1.2.1", "keywords": ["vwo", "fme", "sdk"], "license": "Apache-2.0", "authors": [{ diff --git a/src/Api/GetFlag.php b/src/Api/GetFlag.php index 5cc2e41..b061815 100644 --- a/src/Api/GetFlag.php +++ b/src/Api/GetFlag.php @@ -36,12 +36,12 @@ interface IGetFlag { - public function get($featureKey, $settings, $context, $hookManager); + public function get($featureKey, $settings, $context, $hookManager, $settingsFilePassedInOptions = false); } class GetFlag implements IGetFlag { - public function get($featureKey, $settings, $context, $hookManager) + public function get($featureKey, $settings, $context, $hookManager, $settingsFilePassedInOptions = false) { // initialize contextUtil object $decision = $this->createDecision($settings, $featureKey, $context); @@ -51,7 +51,7 @@ public function get($featureKey, $settings, $context, $hookManager) $rulesInformation = []; // for storing and integration callback $evaluatedFeatureMap = array(); $shouldCheckForAbPersonalise = false; - + $ruleStatus = []; $storageService = new StorageService(); $storedData = (new StorageDecorator())->getFeatureFromStorage( @@ -74,7 +74,8 @@ public function get($featureKey, $settings, $context, $hookManager) ); return new GetFlagResultUtil( true, - $variation->getVariables() + $variation->getVariables(), + $ruleStatus ); } } @@ -106,14 +107,15 @@ public function get($featureKey, $settings, $context, $hookManager) $feature = FunctionUtil::getFeatureFromKey($settings, $featureKey); if (!is_object($feature) || $feature === null) { LogManager::instance()->info("Feature not found for the key {$featureKey}"); - return new GetFlagResultUtil(false, []); + return new GetFlagResultUtil(false, [], $ruleStatus); } $rollOutRules = FunctionUtil::getSpecificRulesBasedOnType($settings, $featureKey, CampaignTypeEnum::ROLLOUT); if (count($rollOutRules) > 0 && !$isEnabled) { foreach ($rollOutRules as $rule) { - $evaluateRuleResult = $this->evaluateRule($settings, $feature, $rule, $context, false, $decision); + + $evaluateRuleResult = $this->evaluateRule($settings, $feature, $rule, $context, false, $decision, $settingsFilePassedInOptions); if ($evaluateRuleResult[0]) { $ruleToTrack[] = $rule; $evaluatedFeatureMap[$featureKey] = [ @@ -121,7 +123,10 @@ public function get($featureKey, $settings, $context, $hookManager) 'rolloutKey' => $rule->getKey(), 'rolloutVariationId' => $rule->getVariations()[0]->getId() ]; + $ruleStatus[$rule->getRuleKey()] = "Passed"; break; + } else { + $ruleStatus[$rule->getRuleKey()] = "Failed"; } continue; } @@ -133,7 +138,7 @@ public function get($featureKey, $settings, $context, $hookManager) if (count($ruleToTrack) > 0) { $ruleElement = array_pop($ruleToTrack); $campaign = $ruleElement; - $variation = $this->trafficCheckAndReturnVariation($settings, $feature, $campaign, $context, $rulesInformation, $decision); + $variation = $this->trafficCheckAndReturnVariation($settings, $feature, $campaign, $context, $rulesInformation, $decision, $settingsFilePassedInOptions); if (DataTypeUtil::isObject($variation)) { $isEnabled = true; @@ -257,7 +262,8 @@ public function get($featureKey, $settings, $context, $hookManager) $rule, $context, false, - $decision + $decision, + $settingsFilePassedInOptions ); if ($abPersonalizeResult) { @@ -272,7 +278,10 @@ public function get($featureKey, $settings, $context, $hookManager) 'experimentVariationId' => $whitelistedVariation['variationId'] ]); } + $ruleStatus[$rule->getRuleKey()] = "Passed"; break; + } else { + $ruleStatus[$rule->getRuleKey()] = "Failed"; } $campaignToSkip[] = $rule->getId(); continue; @@ -282,7 +291,7 @@ public function get($featureKey, $settings, $context, $hookManager) if (count($ruleToTrack) > 0) { $ruleElement = array_pop($ruleToTrack); $campaign = $ruleElement; - $variation = $this->trafficCheckAndReturnVariation($settings, $feature, $campaign, $context, $rulesInformation, $decision); + $variation = $this->trafficCheckAndReturnVariation($settings, $feature, $campaign, $context, $rulesInformation, $decision, $settingsFilePassedInOptions); if ($variation !== null) { $isEnabled = true; @@ -299,13 +308,19 @@ public function get($featureKey, $settings, $context, $hookManager) $hookManager->execute($hookManager->get()); } - if ($feature->getImpactCampaign()->getcampaignId()) { + if ($feature->getImpactCampaign()->getcampaignId() && !$settingsFilePassedInOptions) { + $campaign = new \vwo\Models\CampaignModel(); + $campaign->setId($feature->getImpactCampaign()->getCampaignId()); + + $variation = new \vwo\Models\VariationModel(); + $variation->setId($isEnabled ? 2 : 1); + $this->createImpressionForVariationShown( $settings, $feature, - ['id' => $feature->impactCampaign->campaignId], + $campaign, $context['user'], - ['id' => $isEnabled ? 2 : 1], + $variation, true ); } @@ -317,7 +332,7 @@ public function get($featureKey, $settings, $context, $hookManager) $variables = $rolloutVariationToReturn->getVariables() ?? null; } - return new GetFlagResultUtil($isEnabled, $variables); + return new GetFlagResultUtil($isEnabled, $variables, $ruleStatus); } private function createDecision($settings, $featureKey, $context) @@ -331,7 +346,7 @@ private function createDecision($settings, $featureKey, $context) ]; } - private function trafficCheckAndReturnVariation($settings, $feature, $campaign, $context, &$rulesInformation, &$decision) + private function trafficCheckAndReturnVariation($settings, $feature, $campaign, $context, &$rulesInformation, &$decision, $settingsFilePassedInOptions) { $variation = DecisionUtil::evaluateTrafficAndGetVariation($settings, $campaign, $context['user']['id']); @@ -355,7 +370,8 @@ private function trafficCheckAndReturnVariation($settings, $feature, $campaign, $decision = array_merge($decision, $rulesInformation); // Assuming createImpressionForVariationShown() accepts objects for $campaign and $variation - $this->createImpressionForVariationShown($settings, $feature, $campaign, $context['user'], $variation); + if (!$settingsFilePassedInOptions) + $this->createImpressionForVariationShown($settings, $feature, $campaign, $context['user'], $variation); return $variation; } @@ -369,7 +385,7 @@ private function trafficCheckAndReturnVariation($settings, $feature, $campaign, * @param user user object * @returns */ - public function evaluateRule($settings, $feature, $campaign, $context, $isMegWinnerRule, &$decision) + public function evaluateRule($settings, $feature, $campaign, $context, $isMegWinnerRule, &$decision, $settingsFilePassedInOptions = false) { // check for whitelisting and pre segmentation $result = DecisionUtil::checkWhitelistingAndPreSeg( @@ -389,12 +405,13 @@ public function evaluateRule($settings, $feature, $campaign, $context, $isMegWin 'experimentKey' => $campaign->getKey(), 'experimentVariationId' => $whitelistedObject->variationId, ]); - $this->createImpressionForVariationShown($settings, $feature, $campaign, $context['user'], $whitelistedObject['variation']); + if (!$settingsFilePassedInOptions) + $this->createImpressionForVariationShown($settings, $feature, $campaign, $context['user'], $whitelistedObject['variation']); } return [$preSegmentationResult, $whitelistedObject]; } - function createImpressionForVariationShown($settings, $feature, $campaign, $user, $variation, $isImpactCampaign = false) + function createImpressionForVariationShown($settings, $feature, $campaign, $user, $variation, $isImpactCampaign = false, $settingsFilePassedInOptions = false) { if (isset($user['userAgent'])) { $userAgent = $user['userAgent']; diff --git a/src/Api/SetAttribute.php b/src/Api/SetAttribute.php index ecee917..fd34ab8 100644 --- a/src/Api/SetAttribute.php +++ b/src/Api/SetAttribute.php @@ -22,12 +22,14 @@ use vwo\Enums\EventEnum; interface ISetAttribute { - function setAttribute($settings, $attributeKey, $attributeValue, $context); + function setAttribute($settings, $attributeKey, $attributeValue, $context, $settingsFilePassedInOptions = false); } class SetAttribute implements ISetAttribute { - function setAttribute($settings, $attributeKey, $attributeValue, $context) { - createImpressionForAttribute($settings, $attributeKey, $attributeValue, $context); + function setAttribute($settings, $attributeKey, $attributeValue, $context, $settingsFilePassedInOptions = false) { + if (!$settingsFilePassedInOptions) { + createImpressionForAttribute($settings, $attributeKey, $attributeValue, $context); + } } } diff --git a/src/Api/TrackEvent.php b/src/Api/TrackEvent.php index 3b4267d..740bee4 100644 --- a/src/Api/TrackEvent.php +++ b/src/Api/TrackEvent.php @@ -30,16 +30,18 @@ // Interface for tracking functionality interface ITrack { - public function track( $settings, $eventName, $eventProperties, $context, $hookManager); + public function track( $settings, $eventName, $eventProperties, $context, $hookManager, $settingsFilePassedInOptions = false); } class TrackEvent implements ITrack { - public function track( $settings, $eventName, $eventProperties, $context, $hookManager) + public function track( $settings, $eventName, $eventProperties, $context, $hookManager, $settingsFilePassedInOptions = false) { if (FunctionUtil::eventExists($eventName, $settings)) { - // Create impression for track - $this->createImpressionForTrack($settings, $eventName, $context['user'], $eventProperties); + if (!$settingsFilePassedInOptions) { + // Create impression for track + $this->createImpressionForTrack($settings, $eventName, $context['user'], $eventProperties); + } // Integration callback for track $hookManager->set([ diff --git a/src/Models/CampaignModel.php b/src/Models/CampaignModel.php index 34659e6..78b351c 100644 --- a/src/Models/CampaignModel.php +++ b/src/Models/CampaignModel.php @@ -40,6 +40,7 @@ class CampaignModel private $variationId; private $campaignId; private $weight; + private $ruleKey; public function copy($campaignModel) { @@ -91,6 +92,7 @@ public function processCampaignKeys($campaign) $this->segments = isset($campaign->segments) ? $campaign->segments : null; $this->key = isset($campaign->key) ? $campaign->key : null; $this->type = isset($campaign->type) ? $campaign->type : null; + $this->ruleKey = isset($campaign->ruleKey) ? $campaign->ruleKey : null; } public function getId() @@ -167,6 +169,10 @@ public function getWeight() { return $this->weight; } + + public function getRuleKey() { + return $this->ruleKey; + } public function setId($id) { @@ -238,4 +244,8 @@ public function setWeight($weight) { $this->weight = $weight; } + public function setRuleKey($ruleKey) + { + $this->ruleKey = $ruleKey; + } } diff --git a/src/Models/RuleModel.php b/src/Models/RuleModel.php index 25cdf2d..dae3329 100644 --- a/src/Models/RuleModel.php +++ b/src/Models/RuleModel.php @@ -23,12 +23,14 @@ class RuleModel { private $variationId; private $campaignId; private $type; + private $ruleKey; public function modelFromDictionary($rule) { $this->type = isset($rule->type) ? $rule->type : null; $this->status = isset($rule->status) ? $rule->status : null; $this->variationId = isset($rule->variationId) ? $rule->variationId : null; $this->campaignId = isset($rule->campaignId) ? $rule->campaignId : null; + $this->ruleKey = isset($rule->ruleKey) ? $rule->ruleKey : null; return $this; } @@ -48,6 +50,10 @@ public function getType() { return $this->type; } + public function getRuleKey() { + return $this->ruleKey; + } + public function setCampaignId($campaignId) { $this->campaignId = $campaignId; } @@ -63,6 +69,11 @@ public function setStatus($status) { public function setType($type) { $this->type = $type; } + + public function setRuleKey($ruleKey) { + $this->ruleKey = $ruleKey; + } + } ?> diff --git a/src/Models/VariationModel.php b/src/Models/VariationModel.php index 8942d22..c94abc0 100644 --- a/src/Models/VariationModel.php +++ b/src/Models/VariationModel.php @@ -89,6 +89,10 @@ public function setType($type) { $this->type = $type; } + public function setId($id) { + $this->id = $id; + } + public function setPercentTraffic($percentTraffic) { $this->percentTraffic = $percentTraffic; } diff --git a/src/Packages/Logger/Core/LogManager.php b/src/Packages/Logger/Core/LogManager.php index d409c19..c4da48b 100644 --- a/src/Packages/Logger/Core/LogManager.php +++ b/src/Packages/Logger/Core/LogManager.php @@ -86,7 +86,7 @@ public function handleTransports(): void { $this->addTransport([ 'level' => $this->config['level'], 'logHandler' => function ($message, $level) { - echo $message.PHP_EOL; + file_put_contents("php://stdout", $message . PHP_EOL); } ]); } diff --git a/src/Packages/Logger/Core/LogTransportManager.php b/src/Packages/Logger/Core/LogTransportManager.php index 1a85923..dc8066b 100644 --- a/src/Packages/Logger/Core/LogTransportManager.php +++ b/src/Packages/Logger/Core/LogTransportManager.php @@ -89,13 +89,16 @@ public function log($level, $message): void { $logHandler = $transport['logHandler']; $logHandler($formattedMessage, $level); } else { - if (method_exists($transport, $level)) { - $transport->{$level}($formattedMessage, $level); - } + // Use php://stdout for console logging + $this->logToConsole($level, $formattedMessage); } } } } + + private function logToConsole($message) { + file_put_contents("php://stdout", $message . PHP_EOL); + } } ?> diff --git a/src/Packages/Logger/Transports/ConsoleTransport.php b/src/Packages/Logger/Transports/ConsoleTransport.php index ae9e151..289d417 100644 --- a/src/Packages/Logger/Transports/ConsoleTransport.php +++ b/src/Packages/Logger/Transports/ConsoleTransport.php @@ -52,7 +52,7 @@ public function error($message): void { public function log($level, $message): void { // Ensure the message is output to the console with a single newline - echo rtrim($message) . PHP_EOL; + file_put_contents("php://stdout", $message . PHP_EOL); } } diff --git a/src/Packages/SegmentationEvaluator/Enums/SegmentOperandValueEnum.php b/src/Packages/SegmentationEvaluator/Enums/SegmentOperandValueEnum.php index 5ba4e4a..455e611 100644 --- a/src/Packages/SegmentationEvaluator/Enums/SegmentOperandValueEnum.php +++ b/src/Packages/SegmentationEvaluator/Enums/SegmentOperandValueEnum.php @@ -25,6 +25,10 @@ class SegmentOperandValueEnum { public const ENDING_STAR_VALUE = 4; public const REGEX_VALUE = 5; public const EQUAL_VALUE = 6; + public const GREATER_THAN_VALUE = 7; + public const GREATER_THAN_EQUAL_TO_VALUE = 8; + public const LESS_THAN_VALUE = 9; + public const LESS_THAN_EQUAL_TO_VALUE = 10; } ?> diff --git a/src/Packages/SegmentationEvaluator/Evaluators/SegmentEvaluator.php b/src/Packages/SegmentationEvaluator/Evaluators/SegmentEvaluator.php index 9ef7a24..1aff821 100644 --- a/src/Packages/SegmentationEvaluator/Evaluators/SegmentEvaluator.php +++ b/src/Packages/SegmentationEvaluator/Evaluators/SegmentEvaluator.php @@ -101,7 +101,7 @@ public function some($dslNodes, $customVariables, $settings, $context): bool if ($isUaParser && $keyCount === count($dslNodes)) { try { - if (!$context['userAgent'] || $context['userAgent'] === null) { + if (!isset($context['userAgent']) || $context['userAgent'] === null) { LogManager::instance()->error('To evaluate user agent related segments, please pass userAgent in context object'); return false; } @@ -146,14 +146,14 @@ public function every($dslNodes, $customVariables, $settings, $context): bool public function addLocationValuesToMap($dsl, &$locationMap): void { - if (isset($dsl[SegmentOperatorValueEnum::COUNTRY])) { - $locationMap[SegmentOperatorValueEnum::COUNTRY] = $dsl[SegmentOperatorValueEnum::COUNTRY]; + if (isset($dsl->{SegmentOperatorValueEnum::COUNTRY})) { + $locationMap[SegmentOperatorValueEnum::COUNTRY] = $dsl->{SegmentOperatorValueEnum::COUNTRY}; } - if (isset($dsl[SegmentOperatorValueEnum::REGION])) { - $locationMap[SegmentOperatorValueEnum::REGION] = $dsl[SegmentOperatorValueEnum::REGION]; + if (isset($dsl->{SegmentOperatorValueEnum::REGION})) { + $locationMap[SegmentOperatorValueEnum::REGION] = $dsl->{SegmentOperatorValueEnum::REGION}; } - if (isset($dsl[SegmentOperatorValueEnum::CITY])) { - $locationMap[SegmentOperatorValueEnum::CITY] = $dsl[SegmentOperatorValueEnum::CITY]; + if (isset($dsl->{SegmentOperatorValueEnum::CITY})) { + $locationMap[SegmentOperatorValueEnum::CITY] = $dsl->{SegmentOperatorValueEnum::CITY}; } } @@ -164,7 +164,7 @@ public function checkLocationPreSegmentation($locationMap, $ipAddress): bool if (!$userLocation || $userLocation === null || $userLocation === 'false') { return false; } - return $this->valuesMatch($locationMap, $userLocation['location']); + return $this->valuesMatch($locationMap, $userLocation->location); } public function checkUserAgentParser($uaParserMap, $userAgent): bool @@ -223,9 +223,9 @@ public function checkValuePresent($expectedMap, $actualMap): bool public function valuesMatch($expectedLocationMap, $userLocation): bool { foreach ($expectedLocationMap as $key => $value) { - if (isset($userLocation[$key])) { + if (isset($userLocation->$key)) { $normalizedValue1 = $this->normalizeValue($value); - $normalizedValue2 = $this->normalizeValue($userLocation[$key]); + $normalizedValue2 = $this->normalizeValue($userLocation->$key); if ($normalizedValue1 !== $normalizedValue2) { return false; } diff --git a/src/Packages/SegmentationEvaluator/Evaluators/SegmentOperandEvaluator.php b/src/Packages/SegmentationEvaluator/Evaluators/SegmentOperandEvaluator.php index 6081c6d..32ee78d 100644 --- a/src/Packages/SegmentationEvaluator/Evaluators/SegmentOperandEvaluator.php +++ b/src/Packages/SegmentationEvaluator/Evaluators/SegmentOperandEvaluator.php @@ -22,6 +22,7 @@ use vwo\Enums\UrlEnum; use vwo\Packages\SegmentationEvaluator\Enums\SegmentOperandRegexEnum; use vwo\Packages\SegmentationEvaluator\Enums\SegmentOperandValueEnum; +use vwo\Packages\Logger\Core\LogManager; class SegmentOperandEvaluator { public function evaluateCustomVariableDSL($dslOperandValue, $properties): bool { @@ -44,7 +45,7 @@ public function evaluateCustomVariableDSL($dslOperandValue, $properties): bool { if (preg_match(SegmentOperandRegexEnum::IN_LIST, $operand)) { preg_match(SegmentOperandRegexEnum::IN_LIST, $operand, $matches); if (!$matches || count($matches) < 2) { - echo "Invalid 'inList' operand format"; + LogManager::instance()->error('Invalid inList operand format'); return false; } @@ -64,7 +65,7 @@ public function evaluateCustomVariableDSL($dslOperandValue, $properties): bool { } return $res; } catch (\Exception $error) { - echo "Error while fetching data:", $error->getMessage(); + LogManager::instance()->error('Error while fetching data:'. $error->getMessage()); return false; } } else { @@ -90,11 +91,11 @@ public function evaluateUserDSL($dslOperandValue, $properties): bool { public function evaluateUserAgentDSL($dslOperandValue, $context): bool { $operand = $dslOperandValue; - if (!isset($context->userAgent) || $context->userAgent === null) { - echo 'To Evaluate UserAgent segmentation, please provide userAgent in context'; + if (!isset($context['userAgent']) || $context['userAgent'] === null) { + LogManager::instance()->error('To Evaluate UserAgent segmentation, please provide userAgent in context'); return false; } - $tagValue = urldecode($context->userAgent); + $tagValue = urldecode($context['userAgent']); $operandTypeAndValue = $this->preProcessOperandValue($operand); $processedValues = $this->processValues($operandTypeAndValue->operandValue, $tagValue); $tagValue = $processedValues->tagValue; @@ -133,6 +134,18 @@ public function preProcessOperandValue($operand) { } elseif (preg_match(SegmentOperandRegexEnum::REGEX_MATCH, $operand)) { $operandType = SegmentOperandValueEnum::REGEX_VALUE; $operandValue = $this->extractOperandValue($operand, SegmentOperandRegexEnum::REGEX_MATCH); + } elseif (preg_match(SegmentOperandRegexEnum::GREATER_THAN, $operand)) { + $operandType = SegmentOperandValueEnum::GREATER_THAN_VALUE; + $operandValue = $this->extractOperandValue($operand, SegmentOperandRegexEnum::GREATER_THAN); + } elseif (preg_match(SegmentOperandRegexEnum::GREATER_THAN_EQUAL_TO, $operand)) { + $operandType = SegmentOperandValueEnum::GREATER_THAN_EQUAL_TO_VALUE; + $operandValue = $this->extractOperandValue($operand, SegmentOperandRegexEnum::GREATER_THAN_EQUAL_TO); + } elseif (preg_match(SegmentOperandRegexEnum::LESS_THAN, $operand)) { + $operandType = SegmentOperandValueEnum::LESS_THAN_VALUE; + $operandValue = $this->extractOperandValue($operand, SegmentOperandRegexEnum::LESS_THAN); + } elseif (preg_match(SegmentOperandRegexEnum::LESS_THAN_EQUAL_TO, $operand)) { + $operandType = SegmentOperandValueEnum::LESS_THAN_EQUAL_TO_VALUE; + $operandValue = $this->extractOperandValue($operand, SegmentOperandRegexEnum::LESS_THAN_EQUAL_TO); } else { $operandType = SegmentOperandValueEnum::EQUAL_VALUE; $operandValue = $operand; @@ -198,6 +211,26 @@ public function extractResult($operandType, $operandValue, $tagValue) { case SegmentOperandValueEnum::EQUAL_VALUE: $result = $tagValue === $operandValue; break; + case SegmentOperandValueEnum::GREATER_THAN_VALUE: + if ($tagValue !== null) { + $result = $tagValue > $operandValue; + } + break; + case SegmentOperandValueEnum::GREATER_THAN_EQUAL_TO_VALUE: + if ($tagValue !== null) { + $result = $tagValue >= $operandValue; + } + break; + case SegmentOperandValueEnum::LESS_THAN_VALUE: + if ($tagValue !== null) { + $result = $tagValue < $operandValue; + } + break; + case SegmentOperandValueEnum::LESS_THAN_EQUAL_TO_VALUE: + if ($tagValue !== null) { + $result = $tagValue <= $operandValue; + } + break; default: $result = false; break; diff --git a/src/Utils/FunctionUtil.php b/src/Utils/FunctionUtil.php index 521bf3f..a7c187b 100644 --- a/src/Utils/FunctionUtil.php +++ b/src/Utils/FunctionUtil.php @@ -123,6 +123,7 @@ public static function addLinkedCampaignsToSettings($settingsFile) $linkedCampaign->setVariationId($rule->getVariationId()); $linkedCampaign->setType($rule->getType()); $linkedCampaign->setCampaignId($rule->getCampaignId()); + $linkedCampaign->setRuleKey($rule->getRuleKey()); $variationId = $rule->getVariationId(); if ($variationId) { diff --git a/src/Utils/GetFlagResultUtil.php b/src/Utils/GetFlagResultUtil.php index 55fc2cb..7dfa819 100644 --- a/src/Utils/GetFlagResultUtil.php +++ b/src/Utils/GetFlagResultUtil.php @@ -23,11 +23,13 @@ class GetFlagResultUtil { private $isEnabled; private $variables; + private $ruleStatus; - public function __construct($isEnabled, $variables) + public function __construct($isEnabled, $variables, $ruleStatus) { $this->isEnabled = $isEnabled; $this->variables = $variables; + $this->ruleStatus = $ruleStatus; } public function isEnabled() @@ -51,4 +53,9 @@ public function getVariable($key, $defaultValue) } return $defaultValue; } + + public function getRuleStatus() + { + return $this->ruleStatus; + } } diff --git a/src/Utils/NetworkUtil.php b/src/Utils/NetworkUtil.php index 680f8e5..993ecfe 100644 --- a/src/Utils/NetworkUtil.php +++ b/src/Utils/NetworkUtil.php @@ -227,7 +227,8 @@ public function sendPostApiRequest($properties, $payload) { $response = NetworkManager::Instance()->post($request); return $response; } catch (Exception $err) { - echo 'Error occurred while sending POST request: ' . $err->getMessage(); + $errorMessage = $err instanceof \Exception ? $err->getMessage() : 'Unknown error'; + LogManager::instance()->error("Error occurred while sending POST request $errorMessage"); } } @@ -248,7 +249,8 @@ public function sendGetApiRequest($properties, $endpoint) { $response = NetworkManager::Instance()->get($request); return $response; // Return the response model } catch (Exception $err) { - echo 'Error occurred while sending GET request:' . $err; + $errorMessage = $err instanceof \Exception ? $err->getMessage() : 'Unknown error'; + LogManager::instance()->error("Error occurred while sending GET request $errorMessage "); return null; } } diff --git a/src/Utils/VWOGatewayServiceUtil.php b/src/Utils/VWOGatewayServiceUtil.php index 3163751..2b57cc0 100644 --- a/src/Utils/VWOGatewayServiceUtil.php +++ b/src/Utils/VWOGatewayServiceUtil.php @@ -43,7 +43,7 @@ public static function getFromVWOGatewayService($queryParams, $endpoint) { $queryParams, null, null, - null, + UrlService::getProtocol(), UrlService::getPort() ); diff --git a/src/VWO.php b/src/VWO.php index 6c2fefe..9975829 100644 --- a/src/VWO.php +++ b/src/VWO.php @@ -46,9 +46,17 @@ private static function setInstance($options) ->initBatching() ->initPolling(); + + if (isset($options['settingsFile'])) { + // Use the provided settings file + $settingsObject = json_decode($options['settingsFile']); + self::$vwoBuilder->setSettings($settingsObject); + $settings = new SettingsModel($settingsObject); + } else { + // Fetch settings and build VWO instance + $settings = self::$vwoBuilder->getSettings(); + } - // Fetch settings and build VWO instance - $settings = self::$vwoBuilder->getSettings(); if ($settings) { self::$instance = self::$vwoBuilder->build($settings); } @@ -82,7 +90,7 @@ public static function init($options = []) } catch (\Throwable $error) { $msg = sprintf('API - %s failed to execute. Trace: %s. ', $apiName, $error->getMessage()); $logMessage = sprintf('[ERROR]: VWO-SDK %s %s', (new \DateTime())->format(DATE_ISO8601), $msg); - echo $logMessage; + file_put_contents("php://stdout", $logMessage . PHP_EOL); } } } diff --git a/src/VWOBuilder.php b/src/VWOBuilder.php index 037b5b7..bd94ca1 100644 --- a/src/VWOBuilder.php +++ b/src/VWOBuilder.php @@ -54,6 +54,7 @@ class VWOBuilder implements IVWOBuilder private $logManager; private $originalSettings; private $isSettingsFetchInProgress; + private $settingsSetManually = false; public function __construct($options = []) { @@ -103,6 +104,7 @@ public function setSettings($settings): void $this->originalSettings = $settings; $this->settings = new SettingsModel($settings); $this->settings = SettingsUtil::processSettings($this->settings); + $this->settingsSetManually = true; } public function getSettings($force = false) @@ -112,8 +114,7 @@ public function getSettings($force = false) return $this->settings; } else { try { - $var = $this->fetchSettings($force); - return $var; + return $this->fetchSettings($force); } catch (\Exception $error) { $errorMessage = $error instanceof \Exception ? $error->getMessage() : 'Unknown error'; LogManager::instance()->error("Error getting settings: $errorMessage"); @@ -148,9 +149,9 @@ public function setLogger() ] ); return $this; - } catch (\Exception $e) { - echo("In catch"); - echo($e); + } catch (\Exception $error) { + $errorMessage = $error instanceof \Exception ? $error->getMessage() : 'Unknown error'; + LogManager::instance()->error("Error setting Logger Instance: $errorMessage"); } } @@ -216,6 +217,9 @@ public function initPolling() LogManager::instance()->error('Poll interval should be greater than 1'); return $this; } + if (!$this->settingsSetManually){ + return $this; + } $this->checkAndPoll($this->options['pollInterval']); return $this; diff --git a/src/VWOClient.php b/src/VWOClient.php index 9ecb3d6..f146eb5 100644 --- a/src/VWOClient.php +++ b/src/VWOClient.php @@ -67,9 +67,11 @@ public function getFlag($featureKey, $context) { $defaultReturnValue = new GetFlagResultUtil( false, - [] // No variables + [], // No variables + [] ); - + + $settingsFilePassedInOptions = isset($this->options['settingsFile']); try { $hookManager = new HooksManager($this->options); @@ -96,7 +98,7 @@ public function getFlag($featureKey, $context) { // Wrap the context in a 'user' key if it's not already $context = ['user' => $context]; - return (new GetFlag())->get($featureKey, $this->settings, $context, $hookManager); + return (new GetFlag())->get($featureKey, $this->settings, $context, $hookManager, $settingsFilePassedInOptions); } catch (\Throwable $error) { LogManager::instance()->error(sprintf('API - %s failed to execute. Trace: %s', $apiName, $error->getMessage())); return $defaultReturnValue; @@ -106,6 +108,7 @@ public function getFlag($featureKey, $context) { public function trackEvent($eventName, $context, $eventProperties = []) { $apiName = 'trackEvent'; + $settingsFilePassedInOptions = isset($this->options['settingsFile']); try { $hookManager = new HooksManager($this->options); @@ -133,7 +136,7 @@ public function trackEvent($eventName, $context, $eventProperties = []) // Wrap the context in a 'user' key $context = ['user' => $context]; - return (new TrackEvent())->track($this->settings, $eventName, $eventProperties, $context, $hookManager); + return (new TrackEvent())->track($this->settings, $eventName, $eventProperties, $context, $hookManager, $settingsFilePassedInOptions); } catch (\Throwable $error) { LogManager::instance()->error(sprintf('API - %s failed to execute. Trace: %s', $apiName, $error->getMessage())); return [$eventName => false]; @@ -143,6 +146,7 @@ public function trackEvent($eventName, $context, $eventProperties = []) public function setAttribute($attributeKey, $attributeValue, $context) { $apiName = 'setAttribute'; + $settingsFilePassedInOptions = isset($this->options['settingsFile']); try { LogManager::instance()->debug(sprintf(DebugLogMessageEnum::API_CALLED, $apiName)); @@ -156,7 +160,7 @@ public function setAttribute($attributeKey, $attributeValue, $context) throw new \Error('TypeError: Invalid context'); } - (new SetAttribute())->setAttribute($this->settings, $attributeKey, $attributeValue, $context); + (new SetAttribute())->setAttribute($this->settings, $attributeKey, $attributeValue, $context, $settingsFilePassedInOptions); } catch (\Throwable $error) { LogManager::instance()->error(sprintf('API - %s failed to execute. Trace: %s', $apiName, $error->getMessage())); } From 5387424d7219ee6faf42221b833cb017e19287da Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Mon, 29 Jul 2024 21:26:43 +0530 Subject: [PATCH 2/2] fix: remove vendor/autoload from UuidUtil --- CHANGELOG.md | 4 ++++ composer.json | 2 +- src/Utils/UuidUtil.php | 1 - 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7cdcca..3bd9510 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +[1.2.5] - 2024-07-29 +### Fixed + +- Removed unnecessary vendor/autoload from UuidUtil.php [1.2.1] - 2024-07-17 ### Added diff --git a/composer.json b/composer.json index 818863e..ed2fd0b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "vwo/vwo-fme-php-sdk", - "version": "1.2.1", + "version": "1.2.5", "keywords": ["vwo", "fme", "sdk"], "license": "Apache-2.0", "authors": [{ diff --git a/src/Utils/UuidUtil.php b/src/Utils/UuidUtil.php index 8f73507..ef0a313 100644 --- a/src/Utils/UuidUtil.php +++ b/src/Utils/UuidUtil.php @@ -17,7 +17,6 @@ */ namespace vwo\Utils; -require 'vendor/autoload.php'; use Ramsey\Uuid\Uuid;