Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve API result values #93

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions api/v3/TwingleDonation/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
* @see civicrm_api3_create_success
* @see civicrm_api3_create_error
*/
function civicrm_api3_twingle_donation_Submit($params) {

Check warning on line 276 in api/v3/TwingleDonation/Submit.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

Function's nesting level (6) exceeds 5; consider refactoring the function

Check failure on line 276 in api/v3/TwingleDonation/Submit.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

Function's cyclomatic complexity (81) exceeds allowed maximum of 20
// Log call if debugging is enabled within civicrm.settings.php.
if (defined('TWINGLE_API_LOGGING') && TWINGLE_API_LOGGING) {
Civi::log()->debug('TwingleDonation.Submit: ' . json_encode($params, JSON_PRETTY_PRINT));
Expand Down Expand Up @@ -526,7 +526,7 @@

// If usage of double opt-in is selected, use MailingEventSubscribe.create
// to add contact to newsletter groups defined in the profile
$result_values['newsletter']['newsletter_double_opt_in']
$result_values['newsletter_double_opt_in']
= (bool) $profile->getAttribute('newsletter_double_opt_in')
? 'true'
: 'false';
Expand All @@ -535,6 +535,7 @@
&& (bool) ($params['newsletter'] ?? FALSE)
&& is_array($groups = $profile->getAttribute('newsletter_groups'))
) {
// TODO: Ensure the values being integers.
$group_memberships = array_column(
civicrm_api3(
'GroupContact',
Expand All @@ -555,7 +556,7 @@
]
)['visibility'] == 'Public Pages';
if (!in_array($group_id, $group_memberships, FALSE) && $is_public_group) {
$result_values['newsletter'][][$group_id] = civicrm_api3(
$result = civicrm_api3(
'MailingEventSubscribe',
'create',
[
Expand All @@ -564,9 +565,12 @@
'contact_id' => $contact_id,
]
);
$subscription = reset($result['values']);
$subscription['group_id'] = $group_id;
$result_values['newsletter_subscriptions'][] = $subscription;
}
elseif ($is_public_group) {
$result_values['newsletter'][] = $group_id;
$result_values['newsletter_group_ids'][] = $group_id;
}
}
// If requested, add contact to newsletter groups defined in the profile.
Expand All @@ -584,8 +588,7 @@
'contact_id' => $contact_id,
]
);

$result_values['newsletter'][] = $group_id;
$result_values['newsletter_group_ids'][] = $group_id;
}
}

Expand Down Expand Up @@ -616,8 +619,7 @@
'group_id' => $group_id,
'contact_id' => $organisation_id ?? $contact_id,
]);

$result_values['donation_receipt'][] = $group_id;
$result_values['donation_receipt_group_ids'][] = $group_id;
}
}

Expand Down Expand Up @@ -724,7 +726,7 @@
// Create the mandate.
$mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data);

$result_values['sepa_mandate'] = $mandate['values'];
$result_values['sepa_mandate'] = reset($mandate['values']);
}
else {
// Set financial type depending on donation rhythm. This applies for
Expand Down Expand Up @@ -797,12 +799,15 @@
}

$contribution = civicrm_api3('Contribution', 'create', $contribution_data);
if ($contribution['is_error']) {
/** @phpstan-var array{'values': array<int, array<mixed>>, 'is_error'?: string} $contribution */
if ((bool) ($contribution['is_error'] ?? FALSE)) {
throw new CRM_Core_Exception(
E::ts('Could not create contribution'),
'api_error'
);
}
$contribution = reset($contribution['values']);
/** @phpstan-var array{'id': int} $contribution */

// Add notes to the contribution.
/** @phpstan-var array<string> $contribution_note_mappings */
Expand All @@ -815,13 +820,13 @@
) {
Note::create(FALSE)
->addValue('entity_table', 'civicrm_contribution')
->addValue('entity_id', reset($contribution['values'])['id'])
->addValue('note', reset($params[$target]))
->addValue('entity_id', $contribution['id'])
->addValue('note', $params[$target])
->execute();
}
}

$result_values['contribution'] = $contribution['values'];
$result_values['contribution'] = $contribution;
}

// MEMBERSHIP CREATION
Expand Down
Loading