From 43be624bf6704e00075dbe45aab47118ccf4cdcf Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Thu, 3 Aug 2023 14:52:30 +0200 Subject: [PATCH 1/9] create custom exceptions --- CRM/Twingle/Exceptions/BaseException.php | 43 +++++++++++++++++++ CRM/Twingle/Exceptions/ProfileException.php | 16 +++++++ .../Exceptions/ProfileValidationError.php | 34 +++++++++++++++ CRM/Twingle/Form/Profile.php | 40 +++++++++++------ CRM/Twingle/Profile.php | 10 ++++- 5 files changed, 127 insertions(+), 16 deletions(-) create mode 100644 CRM/Twingle/Exceptions/BaseException.php create mode 100644 CRM/Twingle/Exceptions/ProfileException.php create mode 100644 CRM/Twingle/Exceptions/ProfileValidationError.php diff --git a/CRM/Twingle/Exceptions/BaseException.php b/CRM/Twingle/Exceptions/BaseException.php new file mode 100644 index 0000000..f61a4fe --- /dev/null +++ b/CRM/Twingle/Exceptions/BaseException.php @@ -0,0 +1,43 @@ +log_message = !empty($message) ? E::LONG_NAME . ': ' . $message : ''; + $this->error_code = $error_code; + } + + /** + * Returns the error message, but with the extension name prefixed. + * @return string + */ + public function getLogMessage() { + return $this->log_message; + } + + /** + * Returns the error code. + * @return string + */ + public function getErrorCode() { + return $this->error_code; + } + +} diff --git a/CRM/Twingle/Exceptions/ProfileException.php b/CRM/Twingle/Exceptions/ProfileException.php new file mode 100644 index 0000000..1141d10 --- /dev/null +++ b/CRM/Twingle/Exceptions/ProfileException.php @@ -0,0 +1,16 @@ +affected_field_name = $affected_field_name; + } + + /** + * Returns the name of the profile field that caused the exception. + * @return string + */ + public function getAffectedFieldName() { + return $this->affected_field_name; + } + +} diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index c4a556b..3bc43e0 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -16,6 +16,8 @@ declare(strict_types = 1); use CRM_Twingle_ExtensionUtil as E; +use CRM_Twingle_Exceptions_ProfileException as ProfileException; +use CRM_Twingle_Exceptions_ProfileValidationError as ProfileValidationError; /** * Form controller class @@ -633,23 +635,33 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { */ public function postProcess() { $values = $this->exportValues(); - if (in_array($this->_op, ['create', 'edit', 'copy'])) { - if (empty($values['name'])) { - $values['name'] = 'default'; - } - $this->profile->setName($values['name']); - foreach ($this->profile->getData() as $element_name => $value) { - if ($element_name == 'newsletter_double_opt_in') { - $values[$element_name] = (int) isset($values[$element_name]); + try { + if (in_array($this->_op, ['create', 'edit', 'copy'])) { + if (empty($values['name'])) { + $values['name'] = 'default'; } - if (isset($values[$element_name])) { - $this->profile->setAttribute($element_name, $values[$element_name]); + $this->profile->setName($values['name']); + foreach ($this->profile->getData() as $element_name => $value) { + if ($element_name == 'newsletter_double_opt_in') { + $values[$element_name] = (int) isset($values[$element_name]); + } + if (isset($values[$element_name])) { + $this->profile->setAttribute($element_name, $values[$element_name]); + } } + $this->profile->saveProfile(); } - $this->profile->saveProfile(); - } - elseif ($this->_op == 'delete') { - $this->profile->deleteProfile(); + elseif ($this->_op == 'delete') { + $this->profile->deleteProfile(); + } + } catch (ProfileException $e) { + Civi::log()->error($e->getLogMessage()); + CRM_Core_Session::setStatus( + E::ts('Error'), + $e->getMessage(), + 'error', + ['unique' => TRUE] + ); } parent::postProcess(); } diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index d85c9f3..437d4af 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -16,6 +16,8 @@ declare(strict_types = 1); use CRM_Twingle_ExtensionUtil as E; +use CRM_Twingle_Exceptions_ProfileException as ProfileException; +use CRM_Twingle_Exceptions_ProfileValidationError as ProfileValidationError; /** * Profiles define how incoming submissions from the Twingle API are @@ -142,12 +144,15 @@ class CRM_Twingle_Profile { * @param string $attribute_name * @param mixed $value * - * @throws \Exception + * @throws \CRM_Twingle_Exceptions_ProfileException * When the attribute name is not known. */ public function setAttribute($attribute_name, $value) { if (!in_array($attribute_name, self::allowedAttributes())) { - throw new Exception(E::ts('Unknown attribute %1.', [1 => $attribute_name])); + throw new ProfileException( + E::ts('Unknown attribute %1.', [1 => $attribute_name]), + ProfileException::ERROR_CODE_UNKNOWN_PROFILE_ATTRIBUTE + ); } // TODO: Check if value is acceptable. $this->data[$attribute_name] = $value; @@ -430,6 +435,7 @@ class CRM_Twingle_Profile { * Get the stats (access_count, last_access) for all twingle profiles * * @return CRM_Twingle_Profile[] + * @throws \Civi\Core\Exception\DBQueryException */ public static function getProfileStats() { $stats = []; From 27675b7219064497052be15e76650ea714beab80 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Wed, 16 Aug 2023 14:03:44 +0200 Subject: [PATCH 2/9] use new namespace style --- CRM/Twingle/Exceptions/BaseException.php | 4 +++- CRM/Twingle/Exceptions/ProfileException.php | 4 +++- CRM/Twingle/Exceptions/ProfileValidationError.php | 4 +++- CRM/Twingle/Form/Profile.php | 4 ++-- CRM/Twingle/Profile.php | 4 ++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CRM/Twingle/Exceptions/BaseException.php b/CRM/Twingle/Exceptions/BaseException.php index f61a4fe..eeb5354 100644 --- a/CRM/Twingle/Exceptions/BaseException.php +++ b/CRM/Twingle/Exceptions/BaseException.php @@ -1,12 +1,14 @@ Date: Wed, 16 Aug 2023 17:02:48 +0200 Subject: [PATCH 3/9] override the $code property inherited from Exception in BaseException --- CRM/Twingle/Exceptions/BaseException.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/CRM/Twingle/Exceptions/BaseException.php b/CRM/Twingle/Exceptions/BaseException.php index eeb5354..cebf692 100644 --- a/CRM/Twingle/Exceptions/BaseException.php +++ b/CRM/Twingle/Exceptions/BaseException.php @@ -10,8 +10,11 @@ use CRM_Twingle_ExtensionUtil as E; */ class BaseException extends \Exception { - private string $error_code; - private string $log_message; + /** + * @var int|string + */ + protected $code; + protected string $log_message; /** * BaseException Constructor @@ -23,7 +26,7 @@ class BaseException extends \Exception { public function __construct(string $message = '', string $error_code = '') { parent::__construct($message, 1); $this->log_message = !empty($message) ? E::LONG_NAME . ': ' . $message : ''; - $this->error_code = $error_code; + $this->code = $error_code; } /** @@ -39,7 +42,7 @@ class BaseException extends \Exception { * @return string */ public function getErrorCode() { - return $this->error_code; + return $this->code; } } From e83a898cb8f1445b927f47eb32d5118c9c1cbe6f Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Thu, 17 Aug 2023 10:29:00 +0200 Subject: [PATCH 4/9] add error code for profile validation warning --- CRM/Twingle/Exceptions/ProfileValidationError.php | 1 + 1 file changed, 1 insertion(+) diff --git a/CRM/Twingle/Exceptions/ProfileValidationError.php b/CRM/Twingle/Exceptions/ProfileValidationError.php index e00c6c2..97a50ab 100644 --- a/CRM/Twingle/Exceptions/ProfileValidationError.php +++ b/CRM/Twingle/Exceptions/ProfileValidationError.php @@ -10,6 +10,7 @@ class ProfileValidationError extends BaseException { private string $affected_field_name; public const ERROR_CODE_PROFILE_VALIDATION_FAILED = 'profile_validation_failed'; + public const ERROR_CODE_PROFILE_VALIDATION_WARNING = 'profile_validation_warning'; /** * ProfileValidationError Constructor From 187586173548ba48aeb94e59b2a3157373e9f751 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Thu, 17 Aug 2023 12:51:28 +0200 Subject: [PATCH 5/9] pass $error_code to parent BaseException --- CRM/Twingle/Exceptions/ProfileValidationError.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Twingle/Exceptions/ProfileValidationError.php b/CRM/Twingle/Exceptions/ProfileValidationError.php index 97a50ab..b7a8f52 100644 --- a/CRM/Twingle/Exceptions/ProfileValidationError.php +++ b/CRM/Twingle/Exceptions/ProfileValidationError.php @@ -22,7 +22,7 @@ class ProfileValidationError extends BaseException { * A meaningful error code */ public function __construct(string $affected_field_name, string $message = '', string $error_code = '') { - parent::__construct($message, 1); + parent::__construct($message, $error_code); $this->affected_field_name = $affected_field_name; } From b0d5bdefa5463bb9da860fff511394847be0b618 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Mon, 16 Oct 2023 15:00:47 +0200 Subject: [PATCH 6/9] use correct namespace --- CRM/Twingle/Form/Profile.php | 5 ++--- CRM/Twingle/Profile.php | 3 +-- {CRM/Twingle => Civi}/Exceptions/BaseException.php | 2 +- {CRM/Twingle => Civi}/Exceptions/ProfileException.php | 2 +- {CRM/Twingle => Civi}/Exceptions/ProfileValidationError.php | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) rename {CRM/Twingle => Civi}/Exceptions/BaseException.php (96%) rename {CRM/Twingle => Civi}/Exceptions/ProfileException.php (94%) rename {CRM/Twingle => Civi}/Exceptions/ProfileValidationError.php (96%) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index 243af97..6d545fe 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -16,8 +16,7 @@ declare(strict_types = 1); use CRM_Twingle_ExtensionUtil as E; -use CRM\Twingle\Exceptions\ProfileException as ProfileException; -use CRM\Twingle\Exceptions\ProfileValidationError as ProfileValidationError; +use Civi\Twingle\Exceptions\ProfileException as ProfileException; /** * Form controller class @@ -560,7 +559,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { E::ts('Could not parse custom field mapping.') ); } - list($twingle_field_name, $custom_field_name) = $custom_field_map; + [$twingle_field_name, $custom_field_name] = $custom_field_map; $custom_field_id = substr($custom_field_name, strlen('custom_')); // Check for custom field existence diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index aeeb880..9fd6dd1 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -16,8 +16,7 @@ declare(strict_types = 1); use CRM_Twingle_ExtensionUtil as E; -use CRM\Twingle\Exceptions\ProfileException as ProfileException; -use CRM\Twingle\Exceptions\ProfileValidationError as ProfileValidationError; +use Civi\Twingle\Exceptions\ProfileException as ProfileException; /** * Profiles define how incoming submissions from the Twingle API are diff --git a/CRM/Twingle/Exceptions/BaseException.php b/Civi/Exceptions/BaseException.php similarity index 96% rename from CRM/Twingle/Exceptions/BaseException.php rename to Civi/Exceptions/BaseException.php index cebf692..57e4660 100644 --- a/CRM/Twingle/Exceptions/BaseException.php +++ b/Civi/Exceptions/BaseException.php @@ -1,6 +1,6 @@ Date: Mon, 25 Mar 2024 14:33:52 +0100 Subject: [PATCH 7/9] Move exception class files into correct directory according to namespace --- Civi/{ => Twingle}/Exceptions/BaseException.php | 0 Civi/{ => Twingle}/Exceptions/ProfileException.php | 0 Civi/{ => Twingle}/Exceptions/ProfileValidationError.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename Civi/{ => Twingle}/Exceptions/BaseException.php (100%) rename Civi/{ => Twingle}/Exceptions/ProfileException.php (100%) rename Civi/{ => Twingle}/Exceptions/ProfileValidationError.php (100%) diff --git a/Civi/Exceptions/BaseException.php b/Civi/Twingle/Exceptions/BaseException.php similarity index 100% rename from Civi/Exceptions/BaseException.php rename to Civi/Twingle/Exceptions/BaseException.php diff --git a/Civi/Exceptions/ProfileException.php b/Civi/Twingle/Exceptions/ProfileException.php similarity index 100% rename from Civi/Exceptions/ProfileException.php rename to Civi/Twingle/Exceptions/ProfileException.php diff --git a/Civi/Exceptions/ProfileValidationError.php b/Civi/Twingle/Exceptions/ProfileValidationError.php similarity index 100% rename from Civi/Exceptions/ProfileValidationError.php rename to Civi/Twingle/Exceptions/ProfileValidationError.php From 53745a3e076a24005c46a42f27903f5d4af990d5 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Mon, 25 Mar 2024 15:49:01 +0100 Subject: [PATCH 8/9] PHP Code Sniffer fixes --- CRM/Twingle/Form/Profile.php | 3 ++- Civi/Twingle/Exceptions/BaseException.php | 17 +++++++++++++++++ Civi/Twingle/Exceptions/ProfileException.php | 17 +++++++++++++++++ .../Exceptions/ProfileValidationError.php | 17 +++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index 6d545fe..efffdce 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -653,7 +653,8 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { elseif ($this->_op == 'delete') { $this->profile->deleteProfile(); } - } catch (ProfileException $e) { + } + catch (ProfileException $e) { Civi::log()->error($e->getLogMessage()); CRM_Core_Session::setStatus( E::ts('Error'), diff --git a/Civi/Twingle/Exceptions/BaseException.php b/Civi/Twingle/Exceptions/BaseException.php index 57e4660..f1ce9e3 100644 --- a/Civi/Twingle/Exceptions/BaseException.php +++ b/Civi/Twingle/Exceptions/BaseException.php @@ -1,4 +1,21 @@ . + */ + +declare(strict_types = 1); namespace Civi\Twingle\Exceptions; diff --git a/Civi/Twingle/Exceptions/ProfileException.php b/Civi/Twingle/Exceptions/ProfileException.php index b9e5954..7581381 100644 --- a/Civi/Twingle/Exceptions/ProfileException.php +++ b/Civi/Twingle/Exceptions/ProfileException.php @@ -1,4 +1,21 @@ . + */ + +declare(strict_types = 1); namespace Civi\Twingle\Exceptions; diff --git a/Civi/Twingle/Exceptions/ProfileValidationError.php b/Civi/Twingle/Exceptions/ProfileValidationError.php index a678ba7..da29705 100644 --- a/Civi/Twingle/Exceptions/ProfileValidationError.php +++ b/Civi/Twingle/Exceptions/ProfileValidationError.php @@ -1,4 +1,21 @@ . + */ + +declare(strict_types = 1); namespace Civi\Twingle\Exceptions; From fe0ce3f57db11367f08630e257413a1cab5a004e Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Mon, 25 Mar 2024 15:51:58 +0100 Subject: [PATCH 9/9] Fix @throws tag with wrong class name --- CRM/Twingle/Profile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index 9fd6dd1..a78f576 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -143,7 +143,7 @@ class CRM_Twingle_Profile { * @param string $attribute_name * @param mixed $value * - * @throws \CRM_Twingle_Exceptions_ProfileException + * @throws \Civi\Twingle\Exceptions\ProfileException * When the attribute name is not known. */ public function setAttribute($attribute_name, $value) {