entity_share_client_test.module in Entity Share 8.2
File
modules/entity_share_client/tests/modules/entity_share_client_test/entity_share_client_test.module
View source
<?php
declare (strict_types=1);
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\UrlHelper;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_client\Entity\Remote;
function entity_share_client_test_cron() {
$remote_manager = \Drupal::service('entity_share_client.remote_manager');
$remotes = Remote::loadMultiple();
$jsonapi_helper = \Drupal::service('entity_share_client.jsonapi_helper');
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory
->get('entity_share_client_test');
$queue
->createQueue();
$state = \Drupal::state();
$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();
if ($last_uuid_check + 604800 < $current_time) {
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'];
while ($url_uuid) {
$response = $request_service
->request($http_client, 'GET', $url_uuid);
$json = Json::decode((string) $response
->getBody());
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);
}
$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;
}
}
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) {
$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']));
}
}
}