| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* SendFox Record Api |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\SendFox; |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
|
| 12 |
/** |
| 13 |
* Provide functionality for Record insert, upsert |
| 14 |
*/ |
| 15 |
class RecordApiHelper |
| 16 |
{ |
| 17 |
private $_integrationID; |
| 18 |
private $logID; |
| 19 |
|
| 20 |
private $_logResponse; |
| 21 |
|
| 22 |
public function __construct($auth_token, $integId, $logID, $entryID) |
| 23 |
{ |
| 24 |
$this->_integrationID = $integId; |
| 25 |
$this->_logResponse = new UtilApiResponse(); |
| 26 |
$this->logID = $logID; |
| 27 |
} |
| 28 |
|
| 29 |
public function addContact($access_token, $listId, $finalData) |
| 30 |
{ |
| 31 |
$apiEndpoints = 'https://api.sendfox.com/contacts'; |
| 32 |
$listId = explode(',', $listId); |
| 33 |
$header = [ |
| 34 |
'Authorization' => "Bearer {$access_token}", |
| 35 |
'Accept' => 'application/json', |
| 36 |
]; |
| 37 |
|
| 38 |
$data = [ |
| 39 |
'email' => $finalData['email'], |
| 40 |
'first_name' => $finalData['first_name'], |
| 41 |
'last_name' => $finalData['last_name'], |
| 42 |
'lists' => $listId, |
| 43 |
]; |
| 44 |
|
| 45 |
return HttpHelper::post($apiEndpoints, $data, $header); |
| 46 |
} |
| 47 |
|
| 48 |
public function createContactList($access_token, $finalData) |
| 49 |
{ |
| 50 |
$apiEndpoints = 'https://api.sendfox.com/lists'; |
| 51 |
|
| 52 |
$header = [ |
| 53 |
'Authorization' => "Bearer {$access_token}", |
| 54 |
'Accept' => 'application/json', |
| 55 |
]; |
| 56 |
|
| 57 |
$data = [ |
| 58 |
'name' => $finalData['name'], |
| 59 |
]; |
| 60 |
|
| 61 |
return HttpHelper::post($apiEndpoints, $data, $header); |
| 62 |
} |
| 63 |
|
| 64 |
public function generateReqDataFromFieldMap($data, $fieldMap) |
| 65 |
{ |
| 66 |
$dataFinal = []; |
| 67 |
|
| 68 |
foreach ($fieldMap as $key => $value) { |
| 69 |
$triggerValue = $value->formField; |
| 70 |
$actionValue = $value->sendFoxFormField; |
| 71 |
if ('custom' === $triggerValue) { |
| 72 |
$dataFinal[$actionValue] = $value->customValue; |
| 73 |
} elseif (!is_null($data[$triggerValue])) { |
| 74 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 75 |
} |
| 76 |
} |
| 77 |
return $dataFinal; |
| 78 |
} |
| 79 |
|
| 80 |
public function generateListReqDataFromFieldMap($data, $fieldMap) |
| 81 |
{ |
| 82 |
$dataFinal = []; |
| 83 |
|
| 84 |
foreach ($fieldMap as $key => $value) { |
| 85 |
$triggerValue = $value->formField; |
| 86 |
$actionValue = $value->sendFoxListFormField; |
| 87 |
if ('custom' === $triggerValue) { |
| 88 |
$dataFinal[$actionValue] = $value->customValue; |
| 89 |
} elseif (!is_null($data[$triggerValue])) { |
| 90 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 91 |
} |
| 92 |
} |
| 93 |
return $dataFinal; |
| 94 |
} |
| 95 |
|
| 96 |
public function generateReqUnsubscribeDataFromFieldMap($data, $fieldMap) |
| 97 |
{ |
| 98 |
$dataFinal = []; |
| 99 |
|
| 100 |
foreach ($fieldMap as $key => $value) { |
| 101 |
$triggerValue = $value->formField; |
| 102 |
$actionValue = $value->sendFoxUnsubscribeFormField; |
| 103 |
if ('custom' === $triggerValue) { |
| 104 |
$dataFinal[$actionValue] = $value->customValue; |
| 105 |
} elseif (!is_null($data[$triggerValue])) { |
| 106 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 107 |
} |
| 108 |
} |
| 109 |
return $dataFinal; |
| 110 |
} |
| 111 |
|
| 112 |
public function unsubscribeContact($access_token, $finalData) |
| 113 |
{ |
| 114 |
$apiEndpoints = 'https://api.sendfox.com/unsubscribe'; |
| 115 |
|
| 116 |
$header = [ |
| 117 |
'Authorization' => "Bearer {$access_token}", |
| 118 |
'Accept' => 'application/json', |
| 119 |
]; |
| 120 |
|
| 121 |
$data = [ |
| 122 |
'email' => $finalData['email'], |
| 123 |
]; |
| 124 |
return HttpHelper::request($apiEndpoints, 'PATCH', $data, $header); |
| 125 |
} |
| 126 |
|
| 127 |
public function execute( |
| 128 |
$listId, |
| 129 |
$mainAction, |
| 130 |
$fieldValues, |
| 131 |
$fieldMap, |
| 132 |
$access_token, |
| 133 |
$integrationDetails |
| 134 |
) { |
| 135 |
$fieldData = []; |
| 136 |
$apiResponse = null; |
| 137 |
if ('1' === $integrationDetails->mainAction) { |
| 138 |
$type_name = 'Create List'; |
| 139 |
$finalData = $this->generateListReqDataFromFieldMap($fieldValues, $integrationDetails->field_map_list); |
| 140 |
$apiResponse = $this->createContactList($access_token, $finalData); |
| 141 |
|
| 142 |
if (property_exists($apiResponse, 'id')) { |
| 143 |
$this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse); |
| 144 |
} else { |
| 145 |
$this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse); |
| 146 |
} |
| 147 |
} |
| 148 |
if ('2' === $integrationDetails->mainAction) { |
| 149 |
$type_name = 'Create Contact'; |
| 150 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); |
| 151 |
$apiResponse = $this->addContact($access_token, $listId, $finalData); |
| 152 |
if (property_exists($apiResponse, 'errors')) { |
| 153 |
$this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse); |
| 154 |
} else { |
| 155 |
$this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if ('3' === $integrationDetails->mainAction) { |
| 160 |
$type_name = 'Unsubscribe'; |
| 161 |
$finalData = $this->generateReqUnsubscribeDataFromFieldMap($fieldValues, $integrationDetails->field_map_unsubscribe); |
| 162 |
$apiResponse = $this->unsubscribeContact($access_token, $finalData); |
| 163 |
if (property_exists($apiResponse, 'id')) { |
| 164 |
$this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse); |
| 165 |
} else { |
| 166 |
$this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return $apiResponse; |
| 171 |
} |
| 172 |
} |
| 173 |
|