sitemap_status-1.0.0-alpha4/src/LocationBatch.php
src/LocationBatch.php
<?php
namespace Drupal\sitemap_status;
use Drupal\Core\Render\Markup;
/**
* Process and finish callback for locations batch.
*
* @todo check issue status https://www.drupal.org/project/drupal/issues/1797526
*/
class LocationBatch {
/**
* Processes locations one by one.
*
* @param string $location
* @param array $context
*/
public static function batchLocationProcess($location, array &$context) {
if (!array_key_exists('processed', $context['results'])) {
$context['results']['processed'] = [];
}
/** @var SitemapStatusInterface $sitemapStatus */
$sitemapStatus = \Drupal::service('sitemap_status');
// $context['results']['type'] = SitemapStatus::LOCATION;.
$locationStatus = $sitemapStatus->fetchLocationStatus($location);
if ($sitemapStatus->stopOnError() && $sitemapStatus->hasLocationStatusError($locationStatus)) {
// @todo implement batch finished.
// $context['finished'] = 1;
// \Drupal::messenger()->addError(t('The sitemap status check stopped on the first error met.'));
// _batch_finished();
}
$context['results']['processed'][$location] = $locationStatus;
$context['message'] = t('Checking status for <em>@location</em>.', ['@location' => $location]);
}
/**
* Processes locations by groups (async).
*
* @param array $locations_group
* @param array $context
*/
public static function batchLocationsGroupProcess(array $locations_group, array &$context) {
if (!array_key_exists('processed', $context['results'])) {
$context['results']['processed'] = [];
}
/** @var SitemapStatusInterface $sitemapStatus */
$sitemapStatus = \Drupal::service('sitemap_status');
// $context['results']['type'] = SitemapStatus::LOCATIONS_GROUP;.
$context['results']['processed'] = array_merge(
$context['results']['processed'],
$sitemapStatus->fetchLocationStatusMultiple($locations_group)
);
// @todo implement stop on error.
$context['message'] = t('Checking statuses for a group of @count locations.', ['@count' => count($locations_group)]);
}
/**
* Callback when the batch is finished.
*
* @param array $success
* @param array $results
* @param array $operations
*/
public static function batchFinished($success, $results, $operations) {
$t = \Drupal::translation();
$messenger = \Drupal::messenger();
// Process results summary.
if (!empty($results['processed'])) {
$messenger->addMessage(
$t->formatPlural(
count($results['processed']),
'One location status checked.',
'@count locations status checked.'
)
);
/** @var SitemapStatusInterface $sitemapStatus */
$sitemapStatus = \Drupal::service('sitemap_status');
$groupedLocations = $sitemapStatus->groupLocationsByStatus($results['processed']);
/** @var \Drupal\Core\Cache\CacheBackendInterface $cacheBackend */
$cacheBackend = \Drupal::service('cache.backend.sitemap_status');
$cacheBackend->set(SitemapStatusInterface::CACHE_ID, $groupedLocations);
// $summary = $sitemapStatus->getStatusSummary();
// /** @var \Drupal\Core\Render\RendererInterface $renderer */
// $renderer = \Drupal::service('renderer');
// $messenger->addMessage($renderer->render($summary));
}
elseif (!isset($results['processed'])) {
$messenger->addWarning($t->translate('No locations to process, the sitemap might need to be generated first.'));
}
if (!empty($results['errors'])) {
$messenger->addError(
$t->formatPlural(
count($results['errors']),
'Errors @errors',
'Errors: <ul><li>@errors</li></ul>',
[
'@errors' => Markup::create(implode('</li><li>', $results['errors'])),
]
)
);
}
}
}
