Skip to content

Commit a94737f

Browse files
committed
-Support for Ajax calls
1 parent 9ec413d commit a94737f

4 files changed

Lines changed: 619 additions & 72 deletions

File tree

KnownUser.php

Lines changed: 89 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
class KnownUser
1313
{
14+
const QueueITAjaxHeaderKey = "x-queueit-ajaxpageurl" ;
1415
//used for unittest
1516
private static $userInQueueService = null;
1617
private static function getUserInQueueService() {
@@ -45,46 +46,10 @@ public static function extendQueueCookie($eventId, $cookieValidityMinute, $cooki
4546
$userInQueueService->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $secretKey);
4647
}
4748

48-
public static function resolveRequestByLocalEventConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) {
49-
if (KnownUser::getIsDebug($queueitToken, $secretKey))
50-
{
51-
$dic = array(
52-
"TargetUrl"=> $targetUrl,
53-
"QueueitToken"=> $queueitToken,
54-
"QueueConfig"=>$queueConfig != null ? $queueConfig->getString() : "NULL",
55-
"OriginalUrl"=> KnownUser::getHttpRequestProvider()->getAbsoluteUri());
56-
KnownUser::logMoreRequestDetails($dic);
57-
KnownUser::doCookieLog($dic);
58-
}
59-
if (Utils::isNullOrEmptyString($customerId)) {
60-
throw new KnownUserException("customerId can not be null or empty.");
61-
}
62-
63-
if (Utils::isNullOrEmptyString($secretKey)) {
64-
throw new KnownUserException("secretKey can not be null or empty.");
65-
}
66-
67-
if (Utils::isNullOrEmptyString($queueConfig->eventId)) {
68-
throw new KnownUserException("eventId from queueConfig can not be null or empty.");
69-
}
7049

71-
if (Utils::isNullOrEmptyString($queueConfig->queueDomain)) {
72-
throw new KnownUserException("queueDomain from queueConfig can not be null or empty.");
73-
}
74-
75-
if (!is_int($queueConfig->cookieValidityMinute) || intval($queueConfig->cookieValidityMinute) <= 0) {
76-
throw new KnownUserException("cookieValidityMinute from queueConfig should be integer greater than 0.");
77-
}
78-
79-
if (!is_bool($queueConfig->extendCookieValidity)) {
80-
throw new KnownUserException("extendCookieValidity from queueConfig should be valid boolean.");
81-
}
82-
83-
$userInQueueService = KnownUser::getUserInQueueService();
84-
return $userInQueueService->validateQueueRequest($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey);
85-
}
8650

8751
public static function cancelRequestByLocalConfig($targetUrl, $queueitToken,CancelEventConfig $cancelConfig, $customerId, $secretKey) {
52+
$targetUrl = KnownUser::generateTargetUrl($targetUrl);
8853
if (KnownUser::getIsDebug($queueitToken, $secretKey))
8954
{
9055
$dic = array(
@@ -114,8 +79,11 @@ public static function cancelRequestByLocalConfig($targetUrl, $queueitToken,Canc
11479
if (Utils::isNullOrEmptyString($cancelConfig->queueDomain)) {
11580
throw new KnownUserException("queueDomain from cancelConfig can not be null or empty.");
11681
}
82+
11783
$userInQueueService = KnownUser::getUserInQueueService();
118-
return $userInQueueService->validateCancelRequest($targetUrl, $cancelConfig, $customerId, $secretKey);
84+
$result = $userInQueueService->validateCancelRequest($targetUrl, $cancelConfig, $customerId, $secretKey);
85+
$result->isAjaxResult = KnownUser::isQueueAjaxCall();
86+
return $result;
11987
}
12088

12189
public static function validateRequestByIntegrationConfig($currentUrlWithoutQueueITToken, $queueitToken, $integrationsConfigString, $customerId, $secretKey) {
@@ -175,7 +143,9 @@ public static function validateRequestByIntegrationConfig($currentUrlWithoutQueu
175143
else //IgnoreAction
176144
{
177145
$userInQueueService = KnownUser::getUserInQueueService();
178-
return $userInQueueService->getIgnoreActionResult();
146+
$result = $userInQueueService->getIgnoreActionResult();
147+
$result->isAjaxResult = KnownUser::isQueueAjaxCall();
148+
return $result;
179149
}
180150
}
181151
catch (\Exception $e) {
@@ -184,7 +154,52 @@ public static function validateRequestByIntegrationConfig($currentUrlWithoutQueu
184154

185155

186156
}
157+
public static function resolveRequestByLocalEventConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) {
158+
$targetUrl = KnownUser::generateTargetUrl($targetUrl);
159+
return KnownUser::_resolveRequestByLocalEventConfig($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey);
160+
}
161+
private static function _resolveRequestByLocalEventConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) {
162+
163+
if (KnownUser::getIsDebug($queueitToken, $secretKey))
164+
{
165+
$dic = array(
166+
"TargetUrl"=> $targetUrl,
167+
"QueueitToken"=> $queueitToken,
168+
"QueueConfig"=>$queueConfig != null ? $queueConfig->getString() : "NULL",
169+
"OriginalUrl"=> KnownUser::getHttpRequestProvider()->getAbsoluteUri());
170+
KnownUser::logMoreRequestDetails($dic);
171+
KnownUser::doCookieLog($dic);
172+
}
173+
if (Utils::isNullOrEmptyString($customerId)) {
174+
throw new KnownUserException("customerId can not be null or empty.");
175+
}
176+
177+
if (Utils::isNullOrEmptyString($secretKey)) {
178+
throw new KnownUserException("secretKey can not be null or empty.");
179+
}
180+
181+
if (Utils::isNullOrEmptyString($queueConfig->eventId)) {
182+
throw new KnownUserException("eventId from queueConfig can not be null or empty.");
183+
}
187184

185+
if (Utils::isNullOrEmptyString($queueConfig->queueDomain)) {
186+
throw new KnownUserException("queueDomain from queueConfig can not be null or empty.");
187+
}
188+
189+
if (!is_int($queueConfig->cookieValidityMinute) || intval($queueConfig->cookieValidityMinute) <= 0) {
190+
throw new KnownUserException("cookieValidityMinute from queueConfig should be integer greater than 0.");
191+
}
192+
193+
if (!is_bool($queueConfig->extendCookieValidity)) {
194+
throw new KnownUserException("extendCookieValidity from queueConfig should be valid boolean.");
195+
}
196+
197+
198+
$userInQueueService = KnownUser::getUserInQueueService();
199+
$result = $userInQueueService->validateQueueRequest($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey);
200+
$result->isAjaxResult = KnownUser::isQueueAjaxCall();
201+
return $result;
202+
}
188203
private static function handleQueueAction(
189204
$currentUrlWithoutQueueITToken, $queueitToken,
190205
$customerIntegration, $customerId,
@@ -202,32 +217,33 @@ private static function handleQueueAction(
202217
$eventConfig->cookieValidityMinute = $matchedConfig["CookieValidityMinute"];
203218
$eventConfig->version = $customerIntegration["Version"];
204219

205-
switch ($matchedConfig["RedirectLogic"]) {
206-
case "ForcedTargetUrl":
207-
case "ForecedTargetUrl":
208-
$targetUrl = $matchedConfig["ForcedTargetUrl"];
209-
break;
210-
case "EventTargetUrl":
211-
$targetUrl = "";
212-
break;
213-
default :
214-
$targetUrl = $currentUrlWithoutQueueITToken;
215-
}
216-
return KnownUser::resolveRequestByLocalEventConfig($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey);
217-
}
220+
switch ($matchedConfig["RedirectLogic"]) {
221+
case "ForcedTargetUrl":
222+
case "ForecedTargetUrl":
223+
$targetUrl = $matchedConfig["ForcedTargetUrl"];
224+
break;
225+
case "EventTargetUrl":
226+
$targetUrl = "";
227+
break;
228+
default :
229+
$targetUrl = KnownUser::generateTargetUrl($currentUrlWithoutQueueITToken);
230+
}
231+
232+
return KnownUser::_resolveRequestByLocalEventConfig($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey);
233+
}
218234

219235
private static function handleCancelAction(
220236
$currentUrlWithoutQueueITToken, $queueitToken,
221237
$customerIntegration, $customerId,
222238
$secretKey,
223239
$matchedConfig)
224240
{
225-
$cancelEventConfig = new CancelEventConfig();
226-
$cancelEventConfig->eventId = $matchedConfig["EventId"];
227-
$cancelEventConfig->queueDomain = $matchedConfig["QueueDomain"];
228-
$cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"];
229-
$cancelEventConfig->version = $customerIntegration["Version"];
230-
return KnownUser::cancelRequestByLocalConfig($currentUrlWithoutQueueITToken, $queueitToken, $cancelEventConfig, $customerId, $secretKey);
241+
$cancelEventConfig = new CancelEventConfig();
242+
$cancelEventConfig->eventId = $matchedConfig["EventId"];
243+
$cancelEventConfig->queueDomain = $matchedConfig["QueueDomain"];
244+
$cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"];
245+
$cancelEventConfig->version = $customerIntegration["Version"];
246+
return KnownUser::cancelRequestByLocalConfig($currentUrlWithoutQueueITToken, $queueitToken, $cancelEventConfig, $customerId, $secretKey);
231247
}
232248
private static function logMoreRequestDetails(array &$debugInfos)
233249
{
@@ -276,6 +292,21 @@ private static function getIsDebug($queueitToken, $secretKey)
276292
}
277293
return false;
278294
}
295+
296+
private static function generateTargetUrl($originalTargetUrl)
297+
{
298+
return !KnownUser::isQueueAjaxCall() ?
299+
$originalTargetUrl :
300+
urldecode(KnownUser::getHttpRequestProvider()->getHeaderArray()[KnownUser::QueueITAjaxHeaderKey]);
301+
}
302+
private static function isQueueAjaxCall()
303+
{
304+
return array_key_exists(KnownUser::QueueITAjaxHeaderKey,
305+
KnownUser::getHttpRequestProvider()->getHeaderArray());
306+
307+
}
308+
309+
279310
}
280311

281312
class CookieManager implements ICookieManager

Models.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class RequestValidationResult
4646
public $queueId;
4747
public $actionType;
4848
public $redirectType;
49-
49+
public $isAjaxResult;
5050
function __construct($actionType, $eventId, $queueId, $redirectUrl, $redirectType) {
5151
$this->actionType = $actionType;
5252
$this->eventId = $eventId;
@@ -57,7 +57,18 @@ function __construct($actionType, $eventId, $queueId, $redirectUrl, $redirectTyp
5757

5858
public function doRedirect() {
5959
return !Utils::isNullOrEmptyString($this->redirectUrl);
60-
}
60+
}
61+
public function getAjaxQueueRedirectHeaderKey()
62+
{
63+
return "x-queueit-redirect";
64+
}
65+
public function getAjaxRedirectUrl() {
66+
if (!Utils::isNullOrEmptyString($this->redirectUrl))
67+
{
68+
return urlencode($this->redirectUrl);
69+
}
70+
return "";
71+
}
6172
}
6273

6374
class KnownUserException extends \Exception

0 commit comments

Comments
 (0)