View source
<?php
namespace Drupal\apigee_edge_apiproduct_rbac;
use Apigee\Edge\Exception\ApiException;
use Apigee\Edge\Exception\ApiResponseException;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Session\AccountInterface;
final class RoleBasedAccessSettingsBatch {
public static function batchOperation(array $product_name_display_name_map, array $product_name_rids_map, string $attribute_name, string $original_attribute_name, array &$context) : void {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($product_name_display_name_map);
}
$controller = \Drupal::service('apigee_edge.controller.api_product');
foreach (array_slice($product_name_display_name_map, $context['sandbox']['progress'], 5) as $product_name => $product_display_name) {
$context['message'] = t('Updating %d API Product...', [
'%d' => $product_display_name,
]);
$rids = $product_name_rids_map[$product_name] ?? [];
try {
$attributes = $controller
->getAttributes($product_name);
$attributes
->delete($original_attribute_name);
if ($rids) {
$normalized_rids = [];
if (in_array(AccountInterface::AUTHENTICATED_ROLE, $normalized_rids)) {
$normalized_rids[] = AccountInterface::AUTHENTICATED_ROLE;
if (in_array(AccountInterface::ANONYMOUS_ROLE, $rids)) {
$normalized_rids[] = AccountInterface::ANONYMOUS_ROLE;
}
}
else {
$normalized_rids = $rids;
}
$attributes
->add($attribute_name, implode(APIGEE_EDGE_APIPRODUCT_RBAC_ATTRIBUTE_VALUE_DELIMITER, $normalized_rids));
}
$controller
->updateAttributes($product_name, $attributes);
$context['results']['success'][$product_name] = Xss::filter($product_display_name);
} catch (ApiException $e) {
$message = Xss::filter($product_display_name);
if ($e instanceof ApiResponseException) {
$message = t('@product (Reason: @reason.)', [
'@product' => $product_display_name,
'@reason' => $e
->getMessage(),
]);
}
$context['results']['failed'][$product_name] = $message;
} finally {
$context['sandbox']['progress']++;
}
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public static function batchFinishedCallback(bool $success, array $results, array $operations) {
$updated = $results['success'] ?? [];
$failed = $results['failed'] ?? [];
if ($success && !empty($updated) && empty($failed)) {
\Drupal::messenger()
->addStatus(t('All API product attributes have been updated successfully.'));
}
elseif (!empty($updated) || !empty($failed)) {
if (!empty($updated)) {
$items = [
'#theme' => 'item_list',
'#items' => $updated,
];
$message = \Drupal::translation()
->formatPlural(count($updated), '@product API product successfully updated.', '@count API products successfully updated: @products.', [
'@product' => reset($updated),
'@products' => \Drupal::service('renderer')
->render($items),
]);
\Drupal::messenger()
->addStatus($message);
}
if (!empty($failed)) {
$items = [
'#theme' => 'item_list',
'#items' => $failed,
];
$message = \Drupal::translation()
->formatPlural(count($failed), 'An API product failed failed: @product.', '@count API products could not be updated: @products.', [
'@product' => reset($failed),
'@products' => \Drupal::service('renderer')
->render($items),
]);
\Drupal::messenger()
->addError($message);
}
}
else {
\Drupal::messenger()
->addStatus(t('No operation performed.'));
}
}
}