2021-08-09 09:18:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace LDC\LDCCRM\Models;
|
|
|
|
|
|
|
|
|
|
use LDC\LDCCRM\Enums\ApiStatus;
|
|
|
|
|
use LDC\LDCCRM\Utils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class allows to handle single optins
|
|
|
|
|
*
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If an error occurs
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If the CRM encounters an error
|
|
|
|
|
*
|
|
|
|
|
* @category Api Models
|
|
|
|
|
* @package LDCCrmClient
|
|
|
|
|
* @subpackage models
|
|
|
|
|
*/
|
|
|
|
|
abstract class SingleOptin extends CrmClientModel
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validation rules
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $validation = [
|
2021-08-13 10:24:57 +02:00
|
|
|
'email' => 'required|email',
|
2021-08-09 09:18:46 +02:00
|
|
|
'opt_in' => 'required|bool',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the single optin status for an email
|
|
|
|
|
*
|
|
|
|
|
* @param string $email
|
|
|
|
|
* @param boolean $opt_in
|
|
|
|
|
* @return boolean true if succeeded
|
|
|
|
|
* @throws ApiErrorException ApiError::singleOptinCustomerExists If single optin already exists
|
2021-08-12 14:49:27 +02:00
|
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException
|
|
|
|
|
* if an error occurs on server side or on getting response
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
|
|
|
|
|
* If any error or \LDC\LDCCRM\Enums\ApiError occurs
|
2021-08-09 09:18:46 +02:00
|
|
|
*/
|
|
|
|
|
public static function setStatus(string $email, bool $opt_in): bool
|
|
|
|
|
{
|
|
|
|
|
self::validateValue('email', $email);
|
|
|
|
|
static::setUpApiInterface(); // set apiInterface
|
|
|
|
|
$request = Utils::getResquest('apiV2.optin', [], [
|
|
|
|
|
'email' => $email,
|
|
|
|
|
'opt_in' => $opt_in
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = static::$apiInterface->call($request);
|
|
|
|
|
return $response->status->equals(ApiStatus::OK());
|
|
|
|
|
}
|
|
|
|
|
}
|