Skip to content

Commit

Permalink
refactor(SDK): structured and modular, bugfixes, and increased code c…
Browse files Browse the repository at this point in the history
…overage
  • Loading branch information
softvar committed Jun 15, 2020
1 parent 25589ee commit 077e561
Show file tree
Hide file tree
Showing 40 changed files with 2,605 additions and 1,978 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ build/
vendor/
composer.phar
composer.lock
coverage/
.vscode/
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ 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.8.0] - 2020-05-30
### Changed
- Refactor entire SDK - into modular and reusable components
- Improve Test Cases - cleaner & structured code and reusable utitlity functions
- Coverage increased from 83% to ~96% i.e. covering most of the source code via E2E and Unit Tests
- Bugfixes
- pre-segmentation logging issue when evaluated to false
- sending call in case of feature-test when Control is the decision

## [1.7.4] - 2020-05-14
### Changed
- Return `null` from APIs in case user is not part of campaign
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Class CustomLogger implements LoggerInterface{
*
* @return
*/
public function addLog($message, $level){
public function log($message, $level){
// use $level and log $message to either print or store them for later use
}

Expand Down
2 changes: 1 addition & 1 deletion git-hooks/utils/check-license.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const isSuccess = checkLicenseUtil.checkLicenseAndCopyright({
author: 'Wingify Software Pvt. Ltd.',
paths: 'src,tests',
stoppingCriteria: '\\*\\/',
excludes: ['src/Error', 'src/Handlers/Connection.php', 'src/Utils/murmur.php'],
excludes: ['src/Error', 'src/HttpHandler/Connection.php', 'src/Utils/murmur.php'],
extension: 'php'
});
console.timeEnd('Execution time for License and Copyright');
Expand Down
26 changes: 26 additions & 0 deletions src/Constants/CampaignTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Copyright 2019-2020 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo\Constants;

class CampaignTypes
{
const FEATURE_ROLLOUT = 'FEATURE_ROLLOUT';
const FEATURE_TEST = 'FEATURE_TEST';
const AB = 'VISUAL_AB';
}
56 changes: 6 additions & 50 deletions src/Constants/Constants.php → src/Constants/LogMessages.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,13 @@
namespace vwo\Constants;

/***
* Class Constants
* All the constant variables are used from constant class
* Class LogMessages
* All the constant log messages
*
* @package vwo\Constants
* @package vwo\Constants\LogMessages
*/
class Constants
class LogMessages
{
/**
* sdk version for api hit
*/
const SDK_VERSION = '1.7.4';
/**
* sdk langauge for api hit
*/
const SDK_LANGUAGE = 'php';
/**
* base url for api hit
*/
const BASE_URL = 'https://dev.visualwebsiteoptimizer.com/server-side/';
/**
* query string to fetch settings
*/
const SETTINGS_URL = self::BASE_URL . 'settings';
/**
* query string to track goal
*/
const GOAL_URL = self::BASE_URL . 'track-goal';
/**
* query string to track user
*/
const TRACK_URL = self::BASE_URL . 'track-user';
/**
* query string to push api
*/
const PUSH_URL = self::BASE_URL . 'push';
/**
* base seed for uuid
*/
const UUID_SEED = 'https://vwo.com';



/**
* messages used for dedug mode
*/
Expand All @@ -81,8 +46,7 @@ class Constants
'UUID_FOR_USER' => '({file}): Uuid generated for userId:{userid} and accountId:{accountId} is {desiredUuid}',
'FEATURE_FLAG_NOT_LINKED' => '({file}): Feature:{featureKey} is not linked to any running campaigns',
'USER_HASH_BUCKET_VALUE' => '({file}): User ID:{userId} having hash:{hashValue} got bucketValue:{bucketValue}',
'VARIATION_HASH_BUCKET_VALUE' => '({file}): User ID:{userId} for campaign:{campaignKey} having percent traffic:{percentTraffic} got bucket value:{bucketValue}',
'DEVELOPMENT_MODE' => '({file}): Development mode is ON, no calls are made to the VWO server'
'VARIATION_HASH_BUCKET_VALUE' => '({file}): User ID:{userId} for campaign:{campaignKey} having percent traffic:{percentTraffic} got bucket value:{bucketValue}'
];

/**
Expand Down Expand Up @@ -117,8 +81,6 @@ class Constants
'SEGMENTATION_SKIPPED' => '({file}): For userId:{userId} of campaign:{campaignKey}, segment was missing, hence skipping segmentation{variation}',
'SEGMENTATION_STATUS' => '({file}): For userId:{userId} of campaign:{campaignKey} with variables:{customVariables} {status} {segmentationType} {variation}',



];
/**
* messages used for warning mode
Expand All @@ -127,7 +89,7 @@ class Constants
/**
* messages used for error mode
*/
const ERROR_MESSAGE = [
const ERROR_MESSAGES = [
'INVALID_CONFIGURATION' => '({file}): SDK configuration or account settings or both is/are not valid.',
'SETTINGS_FILE_CORRUPTED' => '({file}): Settings file is corrupted. Please contact VWO Support for help.',
'ACTIVATE_API_CONFIG_CORRUPTED' => '({file}): "activate" API has corrupted configuration',
Expand Down Expand Up @@ -156,10 +118,4 @@ class Constants
'ACTIVATE_API_MISSING_PARAMS' => '({file}): "activate" API got bad parameters. It expects campaignKey(String) as first, userId(String) as second and options(optional Object) as third argument',
'SEGMENTATION_ERROR' => '({file}): Error while segmenting the userId:{userId} of campaignKey:{campaignKey}{variation} with customVariables:{customVariables}. Error message: {err}'
];


const VARIABLE_TYPE_BOOLEAN = 'boolean';
const VARIABLE_TYPE_INTEGER = 'integer';
const VARIABLE_TYPE_DOUBLE = 'double';
const VARIABLE_TYPE_STRING = 'string';
}
43 changes: 43 additions & 0 deletions src/Constants/Urls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright 2019-2020 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo\Constants;

class Urls
{
/**
* base url for api hit
*/
const BASE_URL = 'https://dev.visualwebsiteoptimizer.com/server-side/';
/**
* query string to fetch settings
*/
const SETTINGS_URL = self::BASE_URL . 'settings';
/**
* query string to track goal
*/
const TRACK_GOAL_URL = self::BASE_URL . 'track-goal';
/**
* query string to track user
*/
const TRACK_USER_URL = self::BASE_URL . 'track-user';
/**
* query string to push api
*/
const PUSH_URL = self::BASE_URL . 'push';
}
13 changes: 7 additions & 6 deletions src/Core/Bucketer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
namespace vwo\Core;

use Monolog\Logger;
use vwo\Constants\Constants;
use vwo\Constants\LogMessages;
use vwo\Services\LoggerService;
use vwo\Utils\murmur as murmur;

/***
Expand Down Expand Up @@ -79,27 +80,27 @@ public static function getBucket($userId, $campaign)
// if bucketing to be done
$bucketVal = self::getBucketVal($userId, self::$MAX_CAMPAIGN_TRAFFIC);
if (!self::isUserPartofCampaign($bucketVal, $campaign['percentTraffic'])) {
\vwo\VWO::addLog(Logger::DEBUG, Constants::DEBUG_MESSAGES['USER_NOT_PART_OF_CAMPAIGN'], ['{userId}' => $userId, '{method}' => 'getBucket', '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
LoggerService::log(Logger::DEBUG, LogMessages::DEBUG_MESSAGES['USER_NOT_PART_OF_CAMPAIGN'], ['{userId}' => $userId, '{method}' => 'getBucket', '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
return null;
}
$multiplier = self::getMultiplier($campaign['percentTraffic']);

$rangeForVariations = self::getRangeForVariations($bucketVal, $multiplier);

$variation = self::variationUsingRange($rangeForVariations, $campaign['variations']);
\vwo\VWO::addLog(Logger::DEBUG, Constants::DEBUG_MESSAGES['VARIATION_HASH_BUCKET_VALUE'], ['{userId}' => $userId,'{bucketValue}' => $rangeForVariations, '{percentTraffic}' => $campaign['percentTraffic'], '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
LoggerService::log(Logger::DEBUG, LogMessages::DEBUG_MESSAGES['VARIATION_HASH_BUCKET_VALUE'], ['{userId}' => $userId,'{bucketValue}' => $rangeForVariations, '{percentTraffic}' => $campaign['percentTraffic'], '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
if ($variation !== null) {
\vwo\VWO::addLog(Logger::INFO, Constants::INFO_MESSAGES['GOT_VARIATION_FOR_USER'], ['{variationName}' => $variation['name'], '{userId}' => $userId, '{method}' => 'getBucket', '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
LoggerService::log(Logger::INFO, LogMessages::INFO_MESSAGES['GOT_VARIATION_FOR_USER'], ['{variationName}' => $variation['name'], '{userId}' => $userId, '{method}' => 'getBucket', '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
return $variation;
}
\vwo\VWO::addLog(Logger::INFO, Constants::INFO_MESSAGES['NO_VARIATION_ALLOCATED'], ['{userId}' => $userId, '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
LoggerService::log(Logger::INFO, LogMessages::INFO_MESSAGES['NO_VARIATION_ALLOCATED'], ['{userId}' => $userId, '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
return null;
}

public static function variationUsingRange($rangeForVariations, $variations)
{
foreach ($variations as $variation) {
if ($variation['max_range'] >= $rangeForVariations && $rangeForVariations >= $variation['min_range']) {
if (isset($variation['max_range']) && $variation['max_range'] >= $rangeForVariations && $rangeForVariations >= $variation['min_range']) {
return $variation;
}
}
Expand Down
Loading

0 comments on commit 077e561

Please sign in to comment.