Skip to content

Commit 8bd93ba

Browse files
committed
Added support for UserAgent trigger
1 parent e698bbe commit 8bd93ba

4 files changed

Lines changed: 173 additions & 31 deletions

File tree

IntegrationConfigHelpers.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
interface IIntegrationEvaluator
55
{
6-
public function getMatchedIntegrationConfig(array $customerIntegration, $currentPageUrl, array $cookieList);
6+
public function getMatchedIntegrationConfig(array $customerIntegration, $currentPageUrl, array $cookieList, $userAgent);
77
}
88

99
class IntegrationEvaluator implements IIntegrationEvaluator
1010
{
11-
public function getMatchedIntegrationConfig(array $customerIntegration, $currentPageUrl, array $cookieList) {
11+
public function getMatchedIntegrationConfig(array $customerIntegration, $currentPageUrl, array $cookieList, $userAgent) {
1212
if (!array_key_exists("Integrations", $customerIntegration) || !is_array($customerIntegration["Integrations"])) {
1313
return null;
1414
}
@@ -21,15 +21,15 @@ public function getMatchedIntegrationConfig(array $customerIntegration, $current
2121
if (!is_array($trigger)) {
2222
return false;
2323
}
24-
if ($this->evaluateTrigger($trigger, $currentPageUrl, $cookieList)) {
25-
return $integrationConfig;
24+
if ($this->evaluateTrigger($trigger, $currentPageUrl, $cookieList, $userAgent)) {
25+
return $integrationConfig;
2626
}
2727
}
2828
}
2929
return null;
3030
}
3131

32-
private function evaluateTrigger(array $trigger, $currentPageUrl, array $cookieList) {
32+
private function evaluateTrigger(array $trigger, $currentPageUrl, array $cookieList, $userAgent) {
3333
if (!array_key_exists("LogicalOperator", $trigger) || !array_key_exists("TriggerParts", $trigger) || !is_array($trigger["TriggerParts"])) {
3434
return false;
3535
}
@@ -38,7 +38,7 @@ private function evaluateTrigger(array $trigger, $currentPageUrl, array $cookieL
3838
if (!is_array($triggerPart)) {
3939
return false;
4040
}
41-
if ($this->evaluateTriggerPart($triggerPart, $currentPageUrl, $cookieList)) {
41+
if ($this->evaluateTriggerPart($triggerPart, $currentPageUrl, $cookieList, $userAgent)) {
4242
return true;
4343
}
4444
}
@@ -48,15 +48,15 @@ private function evaluateTrigger(array $trigger, $currentPageUrl, array $cookieL
4848
if (!is_array($triggerPart)) {
4949
return false;
5050
}
51-
if (!$this->evaluateTriggerPart($triggerPart, $currentPageUrl, $cookieList)) {
51+
if (!$this->evaluateTriggerPart($triggerPart, $currentPageUrl, $cookieList, $userAgent)) {
5252
return false;
5353
}
5454
}
5555
return true;
5656
}
5757
}
5858

59-
private function evaluateTriggerPart(array $triggerPart, $currentPageUrl, array $cookieList) {
59+
private function evaluateTriggerPart(array $triggerPart, $currentPageUrl, array $cookieList, $userAgent) {
6060
if (!array_key_exists("ValidatorType", $triggerPart)) {
6161
return false;
6262
}
@@ -66,6 +66,8 @@ private function evaluateTriggerPart(array $triggerPart, $currentPageUrl, array
6666
return UrlValidatorHelper::evaluate($triggerPart, $currentPageUrl);
6767
case "CookieValidator":
6868
return CookieValidatorHelper::evaluate($triggerPart, $cookieList);
69+
case "UserAgentValidator":
70+
return UserAgentValidatorHelper::evaluate($triggerPart, $userAgent);
6971
default:
7072
return false;
7173
}
@@ -132,6 +134,25 @@ public static function evaluate(array $triggerPart, array $cookieList) {
132134
}
133135
}
134136

137+
class UserAgentValidatorHelper
138+
{
139+
public static function evaluate(array $triggerPart, $userAgent) {
140+
if (!array_key_exists("Operator", $triggerPart) ||
141+
!array_key_exists("IsNegative", $triggerPart) ||
142+
!array_key_exists("IsIgnoreCase", $triggerPart) ||
143+
!array_key_exists("ValueToCompare", $triggerPart) ) {
144+
return false;
145+
}
146+
147+
return ComparisonOperatorHelper::evaluate(
148+
$triggerPart["Operator"],
149+
$triggerPart["IsNegative"],
150+
$triggerPart["IsIgnoreCase"],
151+
$userAgent,
152+
$triggerPart["ValueToCompare"]);
153+
}
154+
}
155+
135156
class ComparisonOperatorHelper
136157
{
137158
public static function evaluate($opt, $isNegative, $isIgnoreCase, $left, $right) {

KnownUser.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,30 @@ class KnownUser
1111
//used for unittest
1212
private static $userInQueueService = NULL;
1313

14-
private static function createUserInQueueService() {
14+
private static function getUserInQueueService() {
1515
if (KnownUser::$userInQueueService == NULL)
1616
{
1717
return new UserInQueueService(new UserInQueueStateCookieRepository(new CookieManager()));
1818
}
19-
2019
return KnownUser::$userInQueueService;
2120
}
2221

22+
//used for unittest
23+
private static $httpRequestProvider= null;
24+
private static function getHttpRequestProvider() {
25+
if (KnownUser::$httpRequestProvider == NULL)
26+
{
27+
return new HttpRequestProvider();
28+
}
29+
return KnownUser::$httpRequestProvider;
30+
}
31+
2332
public static function cancelQueueCookie($eventId, $cookieDomain) {
2433
if (empty($eventId)) {
2534
throw new KnownUserException("eventId can not be null or empty.");
2635
}
2736

28-
$userInQueueService = KnownUser::createUserInQueueService();
37+
$userInQueueService = KnownUser::getUserInQueueService();
2938
$userInQueueService->cancelQueueCookie($eventId, $cookieDomain);
3039
}
3140

@@ -39,7 +48,7 @@ public static function extendQueueCookie($eventId, $cookieValidityMinute, $cooki
3948
if (!is_int($cookieValidityMinute) || intval($cookieValidityMinute) <= 0) {
4049
throw new KnownUserException("cookieValidityMinute should be integer greater than 0.");
4150
}
42-
$userInQueueService = KnownUser::createUserInQueueService();
51+
$userInQueueService = KnownUser::getUserInQueueService();
4352
$userInQueueService->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $secretKey);
4453
}
4554

@@ -68,7 +77,7 @@ public static function validateRequestByLocalEventConfig($targetUrl, $queueitTok
6877
throw new KnownUserException("extendCookieValidity should be valid boolean.");
6978
}
7079

71-
$userInQueueService = KnownUser::createUserInQueueService();
80+
$userInQueueService = KnownUser::getUserInQueueService();
7281
return $userInQueueService->validateRequest($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey);
7382
}
7483

@@ -87,7 +96,8 @@ public static function validateRequestByIntegrationConfig($currentUrl, $queueitT
8796
try {
8897
$integrationEvaluator = new IntegrationEvaluator();
8998
$customerIntegration = json_decode($integrationsConfigString, true);
90-
$integrationConfig = $integrationEvaluator->getMatchedIntegrationConfig($customerIntegration, $currentUrl, KnownUser::getCookieArray());
99+
$integrationConfig = $integrationEvaluator->getMatchedIntegrationConfig($customerIntegration, $currentUrl,
100+
KnownUser::getCookieArray(), KnownUser::getHttpRequestProvider()->getUserAgent());
91101

92102
if ($integrationConfig == null) {
93103
return new RequestValidationResult(NULL, NULL, NULL);
@@ -115,8 +125,6 @@ public static function validateRequestByIntegrationConfig($currentUrl, $queueitT
115125
} catch (\Exception $e) {
116126
throw new KnownUserException("integrationConfiguration text was not valid: ". $e->getMessage());
117127
}
118-
//var_dump($targetUrl);
119-
//var_dump($integrationConfig["RedirectLogic"]);
120128
return KnownUser::validateRequestByLocalEventConfig($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey);
121129
}
122130

@@ -146,4 +154,15 @@ public function setCookie($name, $value, $expire, $domain) {
146154
}
147155
setcookie($name, $value, $expire, "/", $domain, false, true);
148156
}
157+
}
158+
interface IHttpRequestProvider
159+
{
160+
function getUserAgent();
161+
}
162+
163+
class HttpRequestProvider implements IHttpRequestProvider
164+
{
165+
function getUserAgent() {
166+
return array_key_exists ('HTTP_USER_AGENT',$_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : "";
167+
}
149168
}

Tests/IntegrationConfigHelpersTest.php

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,41 @@ function test_evaluate()
121121
$this->assertFalse( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1")));
122122

123123
}
124+
}
124125

126+
class UserAgentValidatorHelperTest extends UnitTestCase
127+
{
128+
function test_evaluate() {
129+
$triggerPart = array();
130+
$triggerPart ["Operator"] = "Contains";
131+
$triggerPart ["IsIgnoreCase"] = false;
132+
$triggerPart ["IsNegative"] = false;
133+
$triggerPart ["ValueToCompare"] = "googlebot";
134+
$this->assertFalse( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot sample useraagent"));
135+
136+
$triggerPart = array();
137+
$triggerPart ["Operator"] = "Equals";
138+
$triggerPart ["ValueToCompare"] = "googlebot";
139+
$triggerPart ["IsIgnoreCase"] = true;
140+
$triggerPart ["IsNegative"] = true;
141+
$this->assertTrue( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart,"oglebot sample useraagent"));
125142

143+
$triggerPart = array();
144+
$triggerPart ["Operator"] = "Contains";
145+
$triggerPart ["ValueToCompare"] = "googlebot";
146+
$triggerPart ["IsIgnoreCase"] = false;
147+
$triggerPart ["IsNegative"] = true;
148+
$this->assertFalse( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "googlebot"));
149+
150+
$triggerPart = array();
151+
$triggerPart ["Operator"] = "Contains";
152+
$triggerPart ["ValueToCompare"] = "googlebot";
153+
$triggerPart ["IsIgnoreCase"] = true;
154+
$triggerPart ["IsNegative"] = false;
155+
$this->assertTrue( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot"));
156+
}
126157
}
158+
127159
class IntegrationEvaluatorTest extends UnitTestCase
128160
{
129161
function test_getMatchedIntegrationConfig_OneTrigger_And_NotMatched()
@@ -163,9 +195,9 @@ function test_getMatchedIntegrationConfig_OneTrigger_And_NotMatched()
163195
$url = "http://test.tesdomain.com:8080/test?q=2";
164196
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
165197

166-
$this->assertTrue( $testObject->getMatchedIntegrationConfig($integrationConfig, $url, array()) === null);
167-
198+
$this->assertTrue( $testObject->getMatchedIntegrationConfig($integrationConfig, $url, array(), "") === null);
168199
}
200+
169201
function test_getMatchedIntegrationConfig_OneTrigger_And_Matched()
170202
{
171203

@@ -204,7 +236,55 @@ function test_getMatchedIntegrationConfig_OneTrigger_And_Matched()
204236
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
205237

206238
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig,
207-
$url,array("c2"=>"ddd","c1"=>"Value1"))["Name"]==="integration1");
239+
$url,array("c2"=>"ddd","c1"=>"Value1"),"")["Name"]==="integration1");
240+
}
241+
242+
function test_getMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent()
243+
{
244+
245+
$integrationConfig = array(
246+
"Integrations"=>array(
247+
array(
248+
"Name"=>"integration1",
249+
"Triggers"=> array(
250+
array(
251+
"LogicalOperator"=>"And",
252+
"TriggerParts"=>array(
253+
array(
254+
"CookieName" =>"c1",
255+
"Operator" =>"Equals",
256+
"ValueToCompare" =>"value1",
257+
"ValidatorType"=> "CookieValidator",
258+
"IsIgnoreCase"=>true,
259+
"IsNegative"=>false
260+
),
261+
array(
262+
"UrlPart" => "PageUrl",
263+
"ValidatorType"=> "UrlValidator",
264+
"ValueToCompare"=> "test",
265+
"Operator"=>"Contains",
266+
"IsIgnoreCase"=>false,
267+
"IsNegative"=>false
268+
),
269+
array(
270+
"ValidatorType"=> "UserAgentValidator",
271+
"ValueToCompare"=> "googlebot",
272+
"Operator"=>"Contains",
273+
"IsIgnoreCase"=>true,
274+
"IsNegative"=>true
275+
)
276+
)
277+
)
278+
)
279+
)
280+
)
281+
);
282+
283+
$url = "http://test.tesdomain.com:8080/test?q=2";
284+
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
285+
286+
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig,
287+
$url,array("c2"=>"ddd","c1"=>"Value1"),"bot.html google.com googlebot test")==NULL);
208288
}
209289

210290

@@ -245,10 +325,11 @@ function test_getMatchedIntegrationConfig_OneTrigger_Or_NotMatched()
245325
$url = "http://test.tesdomain.com:8080/test?q=2";
246326
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
247327

248-
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,array("c2"=>"ddd","c1"=>"Value1"))==null);
328+
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,array("c2"=>"ddd","c1"=>"Value1"),"")==null);
249329

250330
}
251331

332+
252333
function test_getMatchedIntegrationConfig_OneTrigger_Or_Matched()
253334
{
254335

@@ -286,7 +367,8 @@ function test_getMatchedIntegrationConfig_OneTrigger_Or_Matched()
286367
$url = "http://test.tesdomain.com:8080/test?q=2";
287368
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
288369

289-
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,array("c2"=>"ddd","c1"=>"Value1"))["Name"]==="integration1");
370+
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig,
371+
$url,array("c2"=>"ddd","c1"=>"Value1"),"")["Name"]==="integration1");
290372

291373
}
292374
function test_getMatchedIntegrationConfig_TwoTriggers_Matched()
@@ -341,7 +423,8 @@ function test_getMatchedIntegrationConfig_TwoTriggers_Matched()
341423
$url = "http://test.tesdomain.com:8080/test?q=2";
342424
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
343425

344-
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,array("c2"=>"ddd","c1"=>"Value1"))["Name"]=="integration1");
426+
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,
427+
array("c2"=>"ddd","c1"=>"Value1"),"")["Name"]=="integration1");
345428

346429
}
347430
function test_getMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched()
@@ -409,7 +492,8 @@ function test_getMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched
409492
$url = "http://test.tesdomain.com:8080/test?q=2";
410493
$testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator();
411494

412-
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig,$url,array("c2"=>"ddd","c1"=>"Value1"))["Name"]=="integration1");
495+
$this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig,$url,
496+
array("c2"=>"ddd","c1"=>"Value1"),"")["Name"]=="integration1");
413497
}
414498

415499
}

0 commit comments

Comments
 (0)