| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\ElasticEmail; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
|
| 8 |
class RecordApiHelper |
| 9 |
{ |
| 10 |
private $_defaultHeader; |
| 11 |
|
| 12 |
private $_logID; |
| 13 |
|
| 14 |
private $_logResponse; |
| 15 |
|
| 16 |
private $integrationDetails; |
| 17 |
|
| 18 |
public function __construct($api_key, $integId, $logID, $integrationDetails) |
| 19 |
{ |
| 20 |
$this->integrationDetails = $integrationDetails; |
| 21 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 22 |
$this->_defaultHeader['Authorization'] = "Bearer $api_key"; |
| 23 |
$this->_logResponse = new UtilApiResponse(); |
| 24 |
$this->_logID = $logID; |
| 25 |
} |
| 26 |
|
| 27 |
public function createContact($data, $listName, $apiKey) |
| 28 |
{ |
| 29 |
$tmpData = \is_string($data) ? $data : \json_encode([(object) $data]); |
| 30 |
$header = [ |
| 31 |
'X-ElasticEmail-ApiKey' => $apiKey, |
| 32 |
'Content-Type' => 'application/json', |
| 33 |
]; |
| 34 |
|
| 35 |
$insertRecordEndpoint = "https://api.elasticemail.com/v4/contacts?$listName"; |
| 36 |
return HttpHelper::post($insertRecordEndpoint, $tmpData, $header); |
| 37 |
} |
| 38 |
|
| 39 |
public function generateReqDataFromFieldMap($data, $fieldMap) |
| 40 |
{ |
| 41 |
$dataFinal = []; |
| 42 |
|
| 43 |
foreach ($fieldMap as $value) { |
| 44 |
$triggerValue = $value->formField; |
| 45 |
$actionValue = $value->elasticEmailField; |
| 46 |
if ('custom' === $triggerValue) { |
| 47 |
$dataFinal[$actionValue] = $value->customValue; |
| 48 |
} elseif (!is_null($data[$triggerValue])) { |
| 49 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 50 |
} |
| 51 |
} |
| 52 |
if ($this->integrationDetails->actions) { |
| 53 |
if (property_exists($this->integrationDetails->actions, 'status') && $this->integrationDetails->actions->status) { |
| 54 |
$dataFinal['Status'] = $this->integrationDetails->status; |
| 55 |
} |
| 56 |
} |
| 57 |
return $dataFinal; |
| 58 |
} |
| 59 |
|
| 60 |
public function execute($integId, $fieldValues, $fieldMap, $integrationDetails) |
| 61 |
{ |
| 62 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); |
| 63 |
$listName = $integrationDetails->list_id; |
| 64 |
$query = ''; |
| 65 |
|
| 66 |
foreach ($listName as $key => $val) { |
| 67 |
$query .= 'listnames=' . $val . '&'; |
| 68 |
}; |
| 69 |
if (strlen($query)) { |
| 70 |
$query = substr($query, 0, -1); |
| 71 |
} |
| 72 |
$api_key = $integrationDetails->api_key; |
| 73 |
$apiResponse = $this->createContact($finalData, $query, $api_key); |
| 74 |
|
| 75 |
if (isset($apiResponse->Error)) { |
| 76 |
$this->_logResponse->apiResponse($this->_logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'errors', $apiResponse); |
| 77 |
} else { |
| 78 |
$this->_logResponse->apiResponse($this->_logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'success', $apiResponse); |
| 79 |
} |
| 80 |
return $apiResponse; |
| 81 |
} |
| 82 |
} |
| 83 |
|