| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Getgist; |
| 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 $_logResponse; |
| 13 |
|
| 14 |
private $logID; |
| 15 |
|
| 16 |
public function __construct($api_key, $integId, $logID) |
| 17 |
{ |
| 18 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 19 |
$this->_defaultHeader['Authorization'] = "Bearer $api_key"; |
| 20 |
$this->_logResponse = new UtilApiResponse(); |
| 21 |
$this->logID = $logID; |
| 22 |
} |
| 23 |
|
| 24 |
public function createContact($data) |
| 25 |
{ |
| 26 |
$data = \is_string($data) ? $data : \json_encode((object)$data); |
| 27 |
$insertRecordEndpoint = 'https://api.getgist.com/contacts'; |
| 28 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 29 |
} |
| 30 |
|
| 31 |
public function generateReqDataFromFieldMap($data, $fieldMap, $integrationDetails) |
| 32 |
{ |
| 33 |
$dataFinal = []; |
| 34 |
|
| 35 |
foreach ($fieldMap as $value) { |
| 36 |
$triggerValue = $value->formField; |
| 37 |
$actionValue = $value->getgistFormField; |
| 38 |
if ('custom' === $triggerValue) { |
| 39 |
$dataFinal[$actionValue] = $value->customValue; |
| 40 |
} elseif (!is_null($data[$triggerValue])) { |
| 41 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if (property_exists($integrationDetails, 'user_type') && property_exists($integrationDetails, 'userId') && 'User' === $integrationDetails->user_type) { |
| 46 |
$dataFinal['user_id'] = $integrationDetails->userId; |
| 47 |
} |
| 48 |
|
| 49 |
if ($integrationDetails->actions) { |
| 50 |
if (property_exists($integrationDetails->actions, 'tags') && $integrationDetails->actions->tags) { |
| 51 |
$dataFinal['tags'] = explode(',', $integrationDetails->tags); |
| 52 |
} |
| 53 |
} |
| 54 |
return $dataFinal; |
| 55 |
} |
| 56 |
|
| 57 |
public function execute($integId, $fieldValues, $fieldMap, $integrationDetails) |
| 58 |
{ |
| 59 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap, $integrationDetails); |
| 60 |
$apiResponse = $this->createContact($finalData); |
| 61 |
|
| 62 |
if (!property_exists($apiResponse, 'contact') || property_exists($apiResponse, 'errors')) { |
| 63 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'errors', $apiResponse); |
| 64 |
} else { |
| 65 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'success', $apiResponse); |
| 66 |
} |
| 67 |
return $apiResponse; |
| 68 |
} |
| 69 |
} |
| 70 |
|