SingleOptin add unsetStatusWithHashcode method

This commit is contained in:
Jérémy Munsch 2023-06-28 15:13:35 +02:00
parent 9b53a6aae2
commit b15e9bcb59
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
7 changed files with 78 additions and 4 deletions

View file

@ -1,5 +1,5 @@
{
"version": "3.0.5",
"version": "3.0.6",
"timestampTimeout": 30,
"apiRequestRatePerMinute": 256,
"servicesDBCache": 10

View file

@ -2,6 +2,8 @@
### All notable changes to `CRM CLient V2` will be documented in this file.
# version 3.0.6
- Added: SingleOptin::unsetStatusWithHashcode to allow customers to unsubscribe using a hashcode, now single optins do have hashcodes.
# version 3.0.5
- Bumped to follow CRM updates

View file

@ -3,7 +3,7 @@
"type": "library",
"description": "LDC CRM Client V2",
"license": "MIT",
"version": "3.0.5",
"version": "3.0.6",
"authors": [
{
"name": "Jérémy Munsch",

View file

@ -95,6 +95,7 @@ class CrmApiV2
* @param string $email
* @param boolean $opt_in
* @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.
@ -105,6 +106,22 @@ class CrmApiV2
return SingleOptin::setStatus($email, $opt_in);
}
/**
* Set the single optin status for an email
*
* @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 : - modelNotFound, If a single optin does not exists.
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
public static function unsetStatusWithHashcode(string $hashcode): bool
{
return SingleOptin::unsetStatusWithHashcode($hashcode);
}
/**
* Test a customer credentials to see if it can login
*

View file

@ -22,6 +22,7 @@ abstract class SingleOptin extends CrmClientModel
*/
protected static $validation = [
'email' => 'required|email',
'hashcode' => 'required|string|size:36',
'opt_in' => 'required|bool',
];
@ -35,7 +36,6 @@ abstract class SingleOptin extends CrmClientModel
* @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.
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
public static function setStatus(string $email, bool $opt_in): bool
{
@ -49,4 +49,26 @@ abstract class SingleOptin extends CrmClientModel
$response = static::$apiInterface->call($request);
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('apiV2.optout-hashcode', [], [
'hashcode' => $hashcode
]);
$response = static::$apiInterface->call($request);
return $response->status->equals(ApiStatus::OK());
}
}

View file

@ -105,7 +105,7 @@ class Utils
$attribute,
$data[$attribute] ?? null
);
case in_array($rule, ['min', 'max']):
case in_array($rule, ['size', 'min', 'max']):
return self::{sprintf('validation%s', \ucfirst($rule))}(
$attribute,
$data[$attribute],
@ -260,6 +260,33 @@ class Utils
];
}
/**
* Make Size validator
*
* @param string $attribute
* @param mixed $value
* @param array $params
* @return array
*/
private static function validationSize(string $attribute, $value, array $params): array
{
return [
'type' => 'min',
'error' => sprintf(
'%s doit avoir %s caratères',
self::translate($attribute),
$params[0],
$params[0]
),
'validate' => function () use ($value, $params) {
if (\is_string($value)) {
return \strlen($value) === (int)$params[0];
}
return false;
}
];
}
/**
* Make Min validator
*

View file

@ -12,6 +12,12 @@
"POST"
]
},
"apiV2.optout-hashcode": {
"uri": "api/v2/optout",
"methods": [
"POST"
]
},
"apiV2.customers.store": {
"uri": "api/v2/customers",
"methods": [