function entity_share_client_test_cron in Entity Share 8.2
Same name and namespace in other branches
- 8 modules/entity_share_client/tests/modules/entity_share_client_test/entity_share_client_test.module \entity_share_client_test_cron()
Implements hook_cron().
Example of basic import in cron. Import all channels of all remotes.
File
- modules/
entity_share_client/ tests/ modules/ entity_share_client_test/ entity_share_client_test.module, line 20
Code
function entity_share_client_test_cron() {
/** @var \Drupal\entity_share_client\Service\RemoteManagerInterface $remote_manager */
$remote_manager = \Drupal::service('entity_share_client.remote_manager');
$remotes = Remote::loadMultiple();
/** @var \Drupal\entity_share_client\Service\JsonapiHelperInterface $jsonapi_helper */
$jsonapi_helper = \Drupal::service('entity_share_client.jsonapi_helper');
/** @var \Drupal\Core\Queue\QueueFactory $queue_factory */
$queue_factory = \Drupal::service('queue');
// Retrieve and init the queue in case it is not already created.
$queue = $queue_factory
->get('entity_share_client_test');
$queue
->createQueue();
/** @var \Drupal\Core\State\StateInterface $state */
$state = \Drupal::state();
/** @var \Drupal\entity_share_client\Service\RequestServiceInterface $request_service */
$request_service = \Drupal::service('entity_share_client.request');
$last_uuid_check = $state
->get('entity_share_client_test.last_uuid_check', 0);
$current_time = \Drupal::time()
->getRequestTime();
// Check the UUIDs every week (3600x24x7).
if ($last_uuid_check + 604800 < $current_time) {
// Scan the channels to store UUIDs in the queue.
foreach ($remotes as $remote_id => $remote) {
$jsonapi_helper
->setRemote($remote);
$channel_infos = $remote_manager
->getChannelsInfos($remote);
foreach ($channel_infos as $channel_id => $channel_info) {
$http_client = $remote_manager
->prepareJsonApiClient($remote);
$url_uuid = $channel_info['url_uuid'];
// Loop on pagination.
while ($url_uuid) {
$response = $request_service
->request($http_client, 'GET', $url_uuid);
$json = Json::decode((string) $response
->getBody());
// Add items to the queue.
foreach (EntityShareUtility::prepareData($json['data']) as $uuid_data) {
$queue
->createItem([
'remote_id' => $remote_id,
'channel_id' => $channel_id,
'uuid' => $uuid_data['id'],
]);
}
if (isset($json['links']['next']['href'])) {
$url_uuid = $json['links']['next']['href'];
}
else {
$url_uuid = FALSE;
}
}
}
}
$state
->set('entity_share_client_test.last_uuid_check', $current_time);
}
// Prepare an array of info to limit the number of request.
$request_info = [];
for ($iteration = 1; $iteration <= 50; $iteration++) {
if ($queue_item = $queue
->claimItem()) {
$queue_item_data = $queue_item->data;
$remote_id = $queue_item_data['remote_id'];
$channel_id = $queue_item_data['channel_id'];
$uuid = $queue_item_data['uuid'];
if (!isset($request_info[$remote_id])) {
$request_info[$remote_id] = [];
}
if (!isset($request_info[$remote_id][$channel_id])) {
$request_info[$remote_id][$channel_id] = [];
}
$request_info[$remote_id][$channel_id][] = $uuid;
$queue
->deleteItem($queue_item);
}
else {
break;
}
}
// Process the import of content.
foreach ($request_info as $remote_id => $remote_channels) {
$remote = $remotes[$remote_id];
$channel_infos = $remote_manager
->getChannelsInfos($remote);
$jsonapi_helper
->setRemote($remote);
$http_client = $remote_manager
->prepareJsonApiClient($remote);
foreach ($remote_channels as $channel_id => $uuids) {
// Prepare the URL to restrict to the item.
$url = $channel_infos[$channel_id]['url'];
$parsed_url = UrlHelper::parse($url);
$query = $parsed_url['query'];
$query['filter']['uuid-filter'] = [
'condition' => [
'path' => 'id',
'operator' => 'IN',
'value' => $uuids,
],
];
$query = UrlHelper::buildQuery($query);
$prepared_url = $parsed_url['path'] . '?' . $query;
$response = $request_service
->request($http_client, 'GET', $prepared_url);
$json = Json::decode((string) $response
->getBody());
$jsonapi_helper
->importEntityListData(EntityShareUtility::prepareData($json['data']));
}
}
}