class MailWizzJob
{
//unique main list id
CONST LIST_ID = 'XXXXX';
CONST SOURCE = 'api';
CONST STATUS_CONFIRMED = 'confirmed';
CONST STATUS_UNCONFIRMED = 'unconfirmed';
CONST STATUS_UNSUBSCRIBED = 'unsubscribed';
public $list_fields = null;
/**
* Default action for Job object
*
* @param $job
* @param $data
*/
public function run(Job $job, $data)
{
echo "success default \n";
}
/**
* @param Job $job
* @param $data
* @throws \Exception
*/
public function createUpdate(Job $job, $data)
{
try {
if (isset($data['object'])) {
//print_r($data['object']);
//set user subscription status if checked newsletter or not
$options = [
'details' => array(
'ip_address' => \Tools::getRemoteAddr(),
'status' => ($data['object']['newsletter'] == 1) ? self::STATUS_UNCONFIRMED : self::STATUS_UNSUBSCRIBED,
'source' => self::SOURCE,
),
];
//get list fields and make one array based on remote fields
$sendData = $this->prepareFields($data['object']);
//merge optional data
$sendData = array_merge($sendData, $options);
print_r($sendData);
$endpoint = new \MailWizzApi_Endpoint_ListSubscribers();
//create profile
$response = $endpoint->createUpdate(self::LIST_ID, $sendData);
if (!$this->isSuccess($response)) {
throw new \Exception('MailWizzAPI.createUpdate: ' . $this->getResponseErrorMsg($response) . ' Job data: ' . print_r($data,
true));
}
} else {
throw new \Exception(__METHOD__ . ': object is empty!');
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* @param $inputFields
* @return array
* @throws \Exception
*/
public function prepareFields($inputFields)
{
//get list fields from api
$this->getListFields();
//make upper case and remove unknown fields from field list
$sendFields = array_intersect_key(array_change_key_case($inputFields, CASE_UPPER), $this->list_fields);
return $sendFields;
}
/**
* Get list fields from api
*
* @return mixed
* @throws \Exception
*/
public function getListFields()
{
try {
if (!$this->list_fields) {
$endpoint = new \MailWizzApi_Endpoint_ListFields();
$response = $endpoint->getFields(self::LIST_ID);
if ($this->isSuccess($response)) {
$fields = $this->getResponseDataFieldValue($response, 'records');
$this->list_fields = [];
foreach ($fields as $field) {
$this->list_fields[$field['tag']] = '';
}
unset($endpoint);
unset($response);
} else {
throw new \Exception('MailWizzAPI.getFields: ' . $this->getResponseErrorMsg($response));
}
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* @param $response
* @return bool
*/
protected function isSuccess($response)
{
return ($response->body['status'] == 'success') ? true : false;
}
/**
* @param $response
* @param $name
* @return mixed
* @throws \Exception
*/
protected function getResponseDataFieldValue($response, $name)
{
if (isset($response->body['data'][$name])) {
return $response->body['data'][$name];
} else {
throw new \Exception('getResponseDataFieldValue: ' . $name . ' is not set in response: ' . print_r($response,
true));
}
}
/**
* @param $response
* @return string
*/
protected function getResponseErrorMsg($response)
{
return ($response->body['status'] == 'error') ? $response->body['error'] : 'no error message';
}
/**
* @param Job $job
* @param $data
* @throws \Exception
*/
public function orderStats(Job $job, $data)
{
try {
//print_r($data);
if (isset($data['object'])) {
$customer = new \Customer($data['object']['id_customer']);
$data = array_merge($customer->getFields(), $customer->getStats());
$data['id'] = $customer->id;
$endpoint = new \MailWizzApi_Endpoint_ListSubscribers();
//get list fields and make one array based on remote fields
$sendData = $this->prepareFields($data);
//create profile
$response = $endpoint->createUpdate(self::LIST_ID, $sendData);
if (!$this->isSuccess($response)) {
throw new \Exception('MailWizzAPI.orderStats: ' . $this->getResponseErrorMsg($response) . ' Job data: ' . print_r($data,
true));
}
} else {
throw new \Exception(__METHOD__ . ': object is empty!');
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* @param Job $job
* @param $data
* @throws \Exception
*/
public function delete(Job $job, $data)
{
try {
if (isset($data['object'])) {
$endpoint = new \MailWizzApi_Endpoint_ListSubscribers();
if (isset($data['object']['email'])) {
$response = $endpoint->deleteByEmail(self::LIST_ID, $data['object']['email']);
if (!$this->isSuccess($response)) {
echo 'MailWizzAPI.delete: ' . $this->getResponseErrorMsg($response) . "\n";
}
}
} else {
throw new \Exception(__METHOD__ . ': object is empty!');
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* Fallback action for Job object
*
* @param $data
*/
public function failed($data)
{
echo __METHOD__ . "\n";
echo 'Job Data: ' . print_r($data, true);
}