80 lines
2.8 KiB
PHP
80 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace LDC\LDCCRM\Models;
|
|
|
|
use LDC\LDCCRM\Enums\ApiStatus;
|
|
use LDC\LDCCRM\Utils;
|
|
|
|
/**
|
|
* This class allows to handle single opt in
|
|
*
|
|
* @category Api Models
|
|
* @package LDCCrmClient
|
|
* @subpackage models
|
|
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
|
|
*/
|
|
abstract class SingleOptin extends ClientModel
|
|
{
|
|
/**
|
|
* Validation rules
|
|
*
|
|
* @var array
|
|
*/
|
|
protected static array $validation = [
|
|
'email' => 'required|email',
|
|
'hashcode' => 'required|string|size:36',
|
|
'opt_in' => 'required|bool',
|
|
];
|
|
|
|
/**
|
|
* Set the single opt in status for an email,
|
|
* with an optionnal answer associated.
|
|
*
|
|
* @param string $email
|
|
* @param boolean $opt_in
|
|
* @param array<int,int>|null $answerId
|
|
* @return boolean true if succeeded
|
|
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a field email could not be validated.
|
|
* @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.
|
|
* Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
|
|
*/
|
|
public static function setStatus(#[\SensitiveParameter] string $email, bool $opt_in, ?array $answerId = null): bool
|
|
{
|
|
self::validateValue('email', $email);
|
|
static::setUpApiInterface();
|
|
|
|
$payload = [
|
|
'email' => $email,
|
|
'opt_in' => $opt_in,
|
|
'answer_id' => (!empty($answerId)) ? $answerId : null,
|
|
];
|
|
|
|
$response = static::$apiInterface->call(
|
|
Utils::getResquest('apiV4.optin', [], array_filter($payload, fn($v) => !is_null($v)))
|
|
);
|
|
return $response->status->equals(ApiStatus::OK);
|
|
}
|
|
|
|
/**
|
|
* Unset the single opt in for a hashcode (unsubscribe)
|
|
*
|
|
* @param string $hashcode
|
|
* @return boolean true if succeeded
|
|
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a field hashcode could not be validated.
|
|
* @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.
|
|
* Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
|
|
*/
|
|
public static function unsetStatusWithHashcode(string $hashcode): bool
|
|
{
|
|
self::validateValue('hashcode', $hashcode);
|
|
static::setUpApiInterface();
|
|
$request = Utils::getResquest('apiV4.optout-hashcode', [], [
|
|
'hashcode' => $hashcode
|
|
]);
|
|
|
|
$response = static::$apiInterface->call($request);
|
|
return $response->status->equals(ApiStatus::OK);
|
|
}
|
|
}
|