| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Rapidmail; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Provide functionality for Record insert,upsert |
| 10 |
*/ |
| 11 |
class RecordApiHelper { |
| 12 |
protected $_defaultHeader; |
| 13 |
public static $apiBaseUri = 'https://apiv3.emailsys.net/v1'; |
| 14 |
|
| 15 |
private $integrationDetails; |
| 16 |
|
| 17 |
private $logID; |
| 18 |
|
| 19 |
private $_logResponse; |
| 20 |
|
| 21 |
public function __construct($integrationDetails, $username, $password, $logID) { |
| 22 |
$this->integrationDetails = $integrationDetails; |
| 23 |
$this->_defaultHeader = [ |
| 24 |
'Authorization' => 'Basic ' . base64_encode("$username:$password"), |
| 25 |
'Accept' => '*/*', |
| 26 |
'Content-Type' => 'application/json', |
| 27 |
'verify' => false |
| 28 |
]; |
| 29 |
$this->_logResponse = new UtilApiResponse(); |
| 30 |
$this->logID = $logID; |
| 31 |
} |
| 32 |
|
| 33 |
public function insertRecipientRecord($data) { |
| 34 |
$insertRecordEndpoint = self::$apiBaseUri . '/recipients'; |
| 35 |
$data = \is_string($data) ? $data : \json_encode((object)$data); |
| 36 |
$response = HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 37 |
return $response; |
| 38 |
} |
| 39 |
|
| 40 |
public function generateReqDataFromFieldMap($data, $fieldMap) { |
| 41 |
$dataFinal = []; |
| 42 |
|
| 43 |
foreach ($fieldMap as $value) { |
| 44 |
$triggerValue = $value->formField; |
| 45 |
$actionValue = $value->rapidmailFormField; |
| 46 |
if ('custom' === $triggerValue) { |
| 47 |
$dataFinal[$actionValue] = $value->customValue; |
| 48 |
} elseif (!is_null($data[$triggerValue])) { |
| 49 |
if (strtotime($data[$triggerValue])) { |
| 50 |
$dataFinal[$actionValue] = date('Y-m-d', strtotime($data[$triggerValue])); |
| 51 |
} else { |
| 52 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$selectedRecipientList = $this->integrationDetails->recipient_id; |
| 58 |
if ($this->integrationDetails->actions) { |
| 59 |
if (property_exists($this->integrationDetails->actions, 'send_activationmail') && $this->integrationDetails->actions->send_activationmail) { |
| 60 |
$dataFinal['send_activationmail'] = 'yes'; |
| 61 |
} |
| 62 |
} |
| 63 |
$dataFinal['recipientlist_id'] = (int)$selectedRecipientList; |
| 64 |
return $dataFinal; |
| 65 |
} |
| 66 |
|
| 67 |
public function executeRecordApi($integId, $defaultConf, $recipientLists, $fieldValues, $fieldMap, $actions) { |
| 68 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); |
| 69 |
$apiResponse = $this->insertRecipientRecord($finalData); |
| 70 |
|
| 71 |
if (!property_exists($apiResponse, 'recipientlist_id')) { |
| 72 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'recipient', 'type_name' => 'add'], 'errors', $apiResponse); |
| 73 |
} else { |
| 74 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'recipient', 'type_name' => 'add'], 'success', $apiResponse); |
| 75 |
} |
| 76 |
return $apiResponse; |
| 77 |
} |
| 78 |
} |
| 79 |
|