improve method inheritance

This commit is contained in:
Marc Michalsky forumZFD 2020-12-17 09:10:40 +01:00
parent 986f70c454
commit a745081b35
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
3 changed files with 37 additions and 117 deletions

View file

@ -126,53 +126,6 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
}
/**
* Instantiate an existing campaign by its id
*
* @param $id
*
* @return TwingleProject|TwingleEvent|TwingleCampaign|NULL
* @throws CiviCRM_API3_Exception
* @throws Exception
*/
public static function fetch($id) {
$result = civicrm_api3('Campaign', 'getsingle', [
'sequential' => 1,
'id' => $id,
]);
$twingle_campaign_types = Cache::getInstance()
->getCampaigns()['campaign_types'];
$twingle_campaign_type_values = [];
foreach ($twingle_campaign_types as $twingle_campaign_type) {
$twingle_campaign_type_values[$twingle_campaign_type['name']] =
CampaignType::fetch($twingle_campaign_type['name'])->getValue();
}
switch ($result->values['campaign_type_id']) {
case $twingle_campaign_type_values['twingle_project']:
return new TwingleProject(
$result['values'],
self::CIVICRM
);
case $twingle_campaign_type_values['twingle_event']:
return new TwingleEvent(
$result['values'],
self::CIVICRM
);
case $twingle_campaign_type_values['twingle_campaign']:
return new TwingleCampaign(
$result['values'],
self::CIVICRM
);
default:
return NULL;
}
}
/**
* Deactivate all duplicates of a campaign but the newest one
*
@ -193,12 +146,26 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
}
}
/**
* Translate values between CiviCRM Campaigns and Twingle format
*
* @param array $values
* array of which values shall be translated
*
* @param string $direction
* TwingleProject::IN -> translate array values from Twingle to CiviCRM <br>
* TwingleProject::OUT -> translate array values from CiviCRM to Twingle
*
* @throws Exception
*/
public static abstract function formatValues(array &$values, string $direction);
/**
* Translate array keys between CiviCRM Campaigns and Twingle
*
* @param array $values
* array of which keys shall be translated
* array of which keys to translate
*
* @param string $direction
* Campaign::IN -> translate array keys from Twingle format into
@ -312,7 +279,7 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
*
* @throws CiviCRM_API3_Exception
*/
public function deactivate() {
public function deactivate(): bool {
return self::deactivateByid($this->id);
@ -329,7 +296,7 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
*
* @throws CiviCRM_API3_Exception
*/
public static function deactivateById($id) {
public static function deactivateById($id): bool {
$result = civicrm_api3('Campaign', 'getsingle', [
'id' => $id,
@ -363,15 +330,7 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
* @return array
* Returns a response array that contains title, id, project_id and status
*/
public function getResponse(string $status) {
return [
'title' => $this->values['name'],
'id' => (int) $this->id,
'project_id' => (int) $this->values['id'],
'project_type' => $this->values['type'],
'status' => $status,
];
}
public abstract function getResponse(string $status): array;
/**
* Validates $input to be either a DateTime string or an Unix timestamp
@ -382,7 +341,7 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
* @return int
* Returns a Unix timestamp or NULL if $input is invalid
*/
public static function getTimestamp($input) {
public static function getTimestamp($input): ?int {
// Check whether $input is a Unix timestamp
if (
@ -403,6 +362,13 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
}
/**
* Return a timestamp of the last update of the Campaign
*
* @return int|string|null
*/
public abstract function lastUpdate();
/**
* Validates $input to be either a DateTime string or an Unix timestamp

View file

@ -49,7 +49,7 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
TwingleApiCall &$twingleApi,
int $user,
bool $is_test = FALSE
) {
): ?array {
// If $values is an array
if (is_array($values)) {
@ -147,49 +147,18 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* @return array
* Returns a response array that contains title, id, project_id and status
*
* @throws \CiviCRM_API3_Exception
* @throws CiviCRM_API3_Exception
* @throws Exception
*/
public function create(bool $is_test = FALSE) {
public function create(bool $is_test = FALSE): array {
// Create campaign only if it does not already exist
if (!$is_test) {
// Prepare project values for import into database
$values_prepared_for_import = $this->values;
self::formatValues(
$values_prepared_for_import,
self::IN
);
self::translateKeys(
$values_prepared_for_import,
self::IN
);
$formattedValues = $values_prepared_for_import;
$this->translateCustomFields(
$values_prepared_for_import,
self::IN
);
// Set id
$values_prepared_for_import['id'] = $this->id;
// Create campaign
$result = civicrm_api3('Campaign', 'create', $values_prepared_for_import);
// Update id
$this->id = $result['id'];
// Check if campaign was created successfully
if ($result['is_error'] == 0) {
$response = $this->getResponse("$this->className created");
}
else {
$response = $this->getResponse("$this->className creation failed");
}
$response = parent::create($is_test);
// Start a case for event initiator
if (
$result['is_error'] == 0 &&
$response['status'] == 'TwingleEvent created' &&
Configuration::get('twinglecampaign_start_case')
) {
$result = civicrm_api3('Case', 'create', [
@ -405,7 +374,7 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
/**
* Return a timestamp of the last update of the Campaign
*
* @return int|null
* @return int|string|null
*/
public function lastUpdate() {

View file

@ -271,7 +271,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
/**
* Translate values between CiviCRM Campaigns and Twingle
* Translate values between CiviCRM Campaigns and Twingle format
*
* @param array $values
* array of which values shall be translated
@ -385,21 +385,6 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
}
/**
* Deactivate this TwingleProject campaign
*
* @return bool
* TRUE if deactivation was successful
*
* @throws CiviCRM_API3_Exception
*/
public function deactivate() {
return self::deactivateByid($this->id);
}
/**
* Get a response that describes the status of a TwingleProject
*
@ -409,7 +394,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
* @return array
* Returns a response array that contains title, id, project_id and status
*/
public function getResponse(string $status) {
public function getResponse(string $status): array {
$project_type = empty($this->values['type']) ? 'default' : $this->values['type'];
return [
'title' => $this->values['name'],
@ -423,7 +408,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
/**
* Return a timestamp of the last update of the Campaign
*
* @return int|null
* @return int|string|null
*/
public function lastUpdate() {
@ -436,7 +421,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
*
* @return int
*/
public function getProjectId() {
public function getProjectId(): int {
return (int) $this->values['id'];
}