Skip to content

Commit 37238fc

Browse files
committed
refactor: bento event record executions
1 parent cd9d0ee commit 37238fc

File tree

3 files changed

+40
-60
lines changed

3 files changed

+40
-60
lines changed

includes/Actions/Bento/BentoController.php

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ class BentoController
1616
{
1717
protected $_defaultHeader;
1818

19-
protected $_apiEndpoint = 'https://app.bentonow.com/api/v1/fetch/';
20-
2119
public function authentication($fieldsRequestParams)
2220
{
23-
$this->checkValidation($fieldsRequestParams);
24-
$this->setHeaders($fieldsRequestParams->publishable_key, $fieldsRequestParams->secret_key);
21+
BentoHelper::checkValidation($fieldsRequestParams);
2522

26-
$apiEndpoint = $this->setEndpoint('tags', $fieldsRequestParams->site_uuid);
27-
$response = HttpHelper::get($apiEndpoint, null, $this->_defaultHeader);
23+
$headers = BentoHelper::setHeaders($fieldsRequestParams->publishable_key, $fieldsRequestParams->secret_key);
24+
$apiEndpoint = BentoHelper::setEndpoint('tags', $fieldsRequestParams->site_uuid);
25+
$response = HttpHelper::get($apiEndpoint, null, $headers);
2826

2927
if (BentoHelper::checkResponseCode()) {
3028
wp_send_json_success(__('Authentication successful', 'bit-integrations'), 200);
31-
} else {
32-
wp_send_json_error(!empty($response) ? $response : __('Please enter valid Publishable Key, Secret Key & Site UUID', 'bit-integrations'), 400);
29+
30+
return;
3331
}
32+
33+
wp_send_json_error(!empty($response) ? $response : __('Please enter valid Publishable Key, Secret Key & Site UUID', 'bit-integrations'), 400);
3434
}
3535

3636
public function getAllFields($fieldsRequestParams)
3737
{
38-
$this->checkValidation($fieldsRequestParams, $fieldsRequestParams->action ?? '');
38+
BentoHelper::checkValidation($fieldsRequestParams, $fieldsRequestParams->action ?? '');
3939

4040
switch ($fieldsRequestParams->action) {
4141
case 'add_people':
@@ -59,7 +59,7 @@ public function getAllFields($fieldsRequestParams)
5959

6060
public function getAlTags($fieldsRequestParams)
6161
{
62-
$this->checkValidation($fieldsRequestParams);
62+
BentoHelper::checkValidation($fieldsRequestParams);
6363

6464
$tags = apply_filters('btcbi_bento_get_all_tags', [], $fieldsRequestParams);
6565

@@ -88,33 +88,6 @@ public function execute($integrationData, $fieldValues)
8888
$siteUUID
8989
);
9090

91-
$bentoApiResponse = $recordApiHelper->execute($fieldValues, $fieldMap, $action);
92-
93-
if (is_wp_error($bentoApiResponse)) {
94-
return $bentoApiResponse;
95-
}
96-
97-
return $bentoApiResponse;
98-
}
99-
100-
private function checkValidation($fieldsRequestParams, $customParam = '**')
101-
{
102-
if (empty($fieldsRequestParams->publishable_key) || empty($fieldsRequestParams->secret_key || empty($fieldsRequestParams->site_uuid)) || empty($customParam)) {
103-
wp_send_json_error(__('Requested parameter is empty', 'bit-integrations'), 400);
104-
}
105-
}
106-
107-
private function setHeaders($publishableKey, $secretKey)
108-
{
109-
$this->_defaultHeader = [
110-
'Authorization' => 'Basic ' . base64_encode("{$publishableKey}:{$secretKey}"),
111-
'Accept' => 'application/json',
112-
'Content-Type' => 'application/json'
113-
];
114-
}
115-
116-
private function setEndpoint($endpoint, $siteUUID)
117-
{
118-
return "{$this->_apiEndpoint}{$endpoint}?site_uuid={$siteUUID}";
91+
return $recordApiHelper->execute($fieldValues, $fieldMap, $action);
11992
}
12093
}

includes/Actions/Bento/BentoHelper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,25 @@ public static function setReqParams($siteUUID, $publishableKey, $secretKey)
2626
'secret_key' => $secretKey,
2727
];
2828
}
29+
30+
public static function setEndpoint($endpoint, $siteUUID)
31+
{
32+
return "https://app.bentonow.com/api/v1/fetch/{$endpoint}?site_uuid={$siteUUID}";
33+
}
34+
35+
public static function checkValidation($fieldsRequestParams, $customParam = '**')
36+
{
37+
if (empty($fieldsRequestParams->publishable_key) || empty($fieldsRequestParams->secret_key || empty($fieldsRequestParams->site_uuid)) || empty($customParam)) {
38+
wp_send_json_error(__('Requested parameter is empty', 'bit-integrations'), 400);
39+
}
40+
}
41+
42+
public static function setHeaders($publishableKey, $secretKey)
43+
{
44+
return [
45+
'Authorization' => 'Basic ' . base64_encode("{$publishableKey}:{$secretKey}"),
46+
'Accept' => 'application/json',
47+
'Content-Type' => 'application/json'
48+
];
49+
}
2950
}

includes/Actions/Bento/RecordApiHelper.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace BitCode\FI\Actions\Bento;
88

9-
use BitCode\FI\Log\LogHandler;
109
use BitCode\FI\Core\Util\Common;
1110
use BitCode\FI\Core\Util\HttpHelper;
11+
use BitCode\FI\Log\LogHandler;
1212

1313
/**
1414
* Provide functionality for Record insert, upsert
@@ -19,8 +19,6 @@ class RecordApiHelper
1919

2020
private $integrationId;
2121

22-
private $apiUrl;
23-
2422
private $defaultHeader;
2523

2624
private $siteUUID;
@@ -29,10 +27,6 @@ class RecordApiHelper
2927

3028
private $secretKey;
3129

32-
private $type;
33-
34-
private $typeName;
35-
3630
public function __construct(
3731
$integrationDetails,
3832
$integId,
@@ -45,28 +39,19 @@ public function __construct(
4539
$this->siteUUID = $siteUUID;
4640
$this->publishableKey = $publishableKey;
4741
$this->secretKey = $secretKey;
48-
$this->apiUrl = 'https://app.bentonow.com/api/v1/fetch/';
49-
50-
$this->defaultHeader = [
51-
'Authorization' => 'Basic ' . base64_encode("{$publishableKey}:{$secretKey}"),
52-
'Accept' => 'application/json',
53-
'Content-Type' => 'application/json'
54-
];
42+
$this->defaultHeader = BentoHelper::setHeaders($publishableKey, $secretKey);
5543
}
5644

5745
public function addPeople($finalData)
5846
{
59-
$this->type = 'User';
60-
$this->typeName = 'Create User';
61-
6247
if (empty($finalData['email'])) {
6348
return ['success' => false, 'message' => __('Required field Email is empty', 'bit-integrations'), 'code' => 400];
6449
}
6550

6651
$email = $finalData['email'];
6752
unset($finalData['email']);
6853

69-
$apiEndpoint = "{$this->apiUrl}subscribers?site_uuid={$this->siteUUID}";
54+
$apiEndpoint = BentoHelper::setEndpoint('subscribers', $this->siteUUID);
7055
$response = HttpHelper::post($apiEndpoint, wp_json_encode(['email' => $email]), $this->defaultHeader);
7156

7257
if (!BentoHelper::checkResponseCode()) {
@@ -89,9 +74,6 @@ public function addPeople($finalData)
8974

9075
public function addEvent($finalData)
9176
{
92-
$this->type = 'User';
93-
$this->typeName = 'Create User';
94-
9577
if (empty($finalData['email']) || empty($finalData['type'])) {
9678
return ['success' => false, 'message' => __('Required field Email is empty', 'bit-integrations'), 'code' => 400];
9779
}
@@ -122,11 +104,15 @@ public function execute($fieldValues, $fieldMap, $action)
122104

123105
switch ($action) {
124106
case 'add_people':
107+
$type = 'People';
108+
$typeName = 'Add People';
125109
$apiResponse = $this->addPeople($finalData);
126110
$logStatus = (!BentoHelper::checkResponseCode() || empty($apiResponse->data)) ? 'error' : 'success';
127111

128112
break;
129113
case 'add_event':
114+
$type = 'Event';
115+
$typeName = 'Add Event';
130116
$apiResponse = $this->addEvent($finalData);
131117
$logStatus = (!BentoHelper::checkResponseCode() || empty($apiResponse->results)) ? 'error' : 'success';
132118

@@ -136,7 +122,7 @@ public function execute($fieldValues, $fieldMap, $action)
136122
break;
137123
}
138124

139-
LogHandler::save($this->integrationId, wp_json_encode(['type' => $this->type, 'type_name' => $this->typeName]), $logStatus, wp_json_encode($apiResponse));
125+
LogHandler::save($this->integrationId, wp_json_encode(['type' => $type, 'type_name' => $typeName]), $logStatus, wp_json_encode($apiResponse));
140126

141127
return $apiResponse;
142128
}

0 commit comments

Comments
 (0)