From b15e9bcb59f4680a736e34dabdb059682fc7b8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 28 Jun 2023 15:13:35 +0200 Subject: [PATCH] SingleOptin add unsetStatusWithHashcode method --- apiConfig.readonly.json | 2 +- changelog.md | 2 ++ composer.json | 2 +- src/CrmApiV2.php | 17 +++++++++++++++++ src/Models/SingleOptin.php | 24 +++++++++++++++++++++++- src/Utils.php | 29 ++++++++++++++++++++++++++++- src/routes.json | 6 ++++++ 7 files changed, 78 insertions(+), 4 deletions(-) diff --git a/apiConfig.readonly.json b/apiConfig.readonly.json index 01e5b1d..26a7377 100644 --- a/apiConfig.readonly.json +++ b/apiConfig.readonly.json @@ -1,5 +1,5 @@ { - "version": "3.0.5", + "version": "3.0.6", "timestampTimeout": 30, "apiRequestRatePerMinute": 256, "servicesDBCache": 10 diff --git a/changelog.md b/changelog.md index 65f9d29..97b070b 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/composer.json b/composer.json index 3c23247..27e0516 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/CrmApiV2.php b/src/CrmApiV2.php index 2726068..bd33ddc 100644 --- a/src/CrmApiV2.php +++ b/src/CrmApiV2.php @@ -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 * diff --git a/src/Models/SingleOptin.php b/src/Models/SingleOptin.php index 45e1160..02c3fdc 100644 --- a/src/Models/SingleOptin.php +++ b/src/Models/SingleOptin.php @@ -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()); + } } diff --git a/src/Utils.php b/src/Utils.php index d4938d0..23b85e5 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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 * diff --git a/src/routes.json b/src/routes.json index 44d264c..7670f93 100644 --- a/src/routes.json +++ b/src/routes.json @@ -12,6 +12,12 @@ "POST" ] }, + "apiV2.optout-hashcode": { + "uri": "api/v2/optout", + "methods": [ + "POST" + ] + }, "apiV2.customers.store": { "uri": "api/v2/customers", "methods": [