From 43be624bf6704e00075dbe45aab47118ccf4cdcf Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Thu, 3 Aug 2023 14:52:30 +0200 Subject: [PATCH] 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 = [];