Merge branch 'use_custom_exeptions'

[#72] Create custom exceptions
This commit is contained in:
Jens Schuppe 2024-03-25 15:52:15 +01:00
commit 1d9e973e46
5 changed files with 187 additions and 16 deletions

View file

@ -16,6 +16,7 @@
declare(strict_types = 1);
use CRM_Twingle_ExtensionUtil as E;
use Civi\Twingle\Exceptions\ProfileException as ProfileException;
/**
* Form controller class
@ -558,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
@ -633,23 +634,34 @@ 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();
}
elseif ($this->_op == 'delete') {
$this->profile->deleteProfile();
}
$this->profile->saveProfile();
}
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();
}

View file

@ -16,6 +16,7 @@
declare(strict_types = 1);
use CRM_Twingle_ExtensionUtil as E;
use Civi\Twingle\Exceptions\ProfileException as ProfileException;
/**
* Profiles define how incoming submissions from the Twingle API are
@ -142,12 +143,15 @@ class CRM_Twingle_Profile {
* @param string $attribute_name
* @param mixed $value
*
* @throws \Exception
* @throws \Civi\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 +434,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 = [];

View file

@ -0,0 +1,65 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types = 1);
namespace Civi\Twingle\Exceptions;
use CRM_Twingle_ExtensionUtil as E;
/**
* A simple custom exception class that indicates a problem within a class
* of the Twingle API extension.
*/
class BaseException extends \Exception {
/**
* @var int|string
*/
protected $code;
protected string $log_message;
/**
* BaseException Constructor
* @param string $message
* Error message
* @param string $error_code
* A meaningful error code
*/
public function __construct(string $message = '', string $error_code = '') {
parent::__construct($message, 1);
$this->log_message = !empty($message) ? E::LONG_NAME . ': ' . $message : '';
$this->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->code;
}
}

View file

@ -0,0 +1,35 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types = 1);
namespace Civi\Twingle\Exceptions;
/**
* A simple custom exception that indicates a problem within the
* CRM_Twingle_Profile class
*/
class ProfileException extends BaseException {
public const ERROR_CODE_PROFILE_NOT_FOUND = 'profile_not_found';
public const ERROR_CODE_DEFAULT_PROFILE_NOT_FOUND = 'default_profile_not_found';
public const ERROR_CODE_COULD_NOT_SAVE_PROFILE = 'could_not_save_profile';
public const ERROR_CODE_COULD_NOT_RESET_PROFILE = 'could_not_reset_profile';
public const ERROR_CODE_COULD_NOT_DELETE_PROFILE = 'could_not_delete_profile';
public const ERROR_CODE_UNKNOWN_PROFILE_ATTRIBUTE = 'unknown_profile_attribute';
}

View file

@ -0,0 +1,54 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types = 1);
namespace Civi\Twingle\Exceptions;
/**
* A simple custom error indicating a problem with the validation of the
* CRM_Twingle_Profile
*/
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
* @param string $affected_field_name
* The name of the profile field which caused the exception
* @param string $message
* Error message
* @param string $error_code
* A meaningful error code
*/
public function __construct(string $affected_field_name, string $message = '', string $error_code = '') {
parent::__construct($message, $error_code);
$this->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;
}
}