View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Service;
use Drupal\Component\Serialization\Json;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_client\Entity\Remote;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Timer;
use Drupal\entity_share_client\Entity\RemoteInterface;
class EntityShareClientCliService {
protected $stringTranslation;
protected $remoteManager;
protected $jsonapiHelper;
protected $errors;
protected $entityTypeManager;
protected $requestService;
public function __construct(TranslationInterface $string_translation, RemoteManagerInterface $remote_manager, JsonapiHelperInterface $jsonapi_helper, EntityTypeManagerInterface $entity_type_manager, RequestServiceInterface $request_service) {
$this->stringTranslation = $string_translation;
$this->remoteManager = $remote_manager;
$this->jsonapiHelper = $jsonapi_helper;
$this->entityTypeManager = $entity_type_manager;
$this->requestService = $request_service;
$this->errors = [];
}
public function ioPull($remote_id, $channel_id, $io, callable $t) {
Timer::start('io-pull');
$remotes = Remote::loadMultiple();
if (!isset($remotes[$remote_id])) {
$io
->error($t('There is no remote website configured with the id: @remote_id.', [
'@remote_id' => $remote_id,
]));
return;
}
$remote = $remotes[$remote_id];
$channel_infos = $this->remoteManager
->getChannelsInfos($remote);
if (!isset($channel_infos[$channel_id])) {
$io
->error($t('There is no channel configured or accessible with the id: @channel_id.', [
'@channel_id' => $channel_id,
]));
return;
}
$this
->pull($remote, $channel_infos[$channel_id]['url']);
Timer::stop('io-pull');
$io
->success($t('Channel successfully pulled. Execution time @time ms.', [
'@time' => Timer::read('io-pull'),
]));
}
public function pull(RemoteInterface $remote, $channel_url) {
$this->jsonapiHelper
->setRemote($remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($remote);
while ($channel_url) {
$response = $this->requestService
->request($http_client, 'GET', $channel_url);
$json = Json::decode((string) $response
->getBody());
$this->jsonapiHelper
->importEntityListData(EntityShareUtility::prepareData($json['data']));
if (isset($json['links']['next']['href'])) {
$channel_url = $json['links']['next']['href'];
}
else {
$channel_url = FALSE;
}
}
}
public function ioPullUpdates($remote_id, $channel_id, $io, callable $t) {
Timer::start('io-pull-updates');
$remotes = Remote::loadMultiple();
if (!isset($remotes[$remote_id])) {
$io
->error($t('There is no remote website configured with the id: @remote_id.', [
'@remote_id' => $remote_id,
]));
return;
}
$remote = $remotes[$remote_id];
$channel_infos = $this->remoteManager
->getChannelsInfos($remote);
if (!isset($channel_infos[$channel_id])) {
$io
->error($t('There is no channel configured or accessible with the id: @channel_id.', [
'@channel_id' => $channel_id,
]));
return;
}
$update_count = $this
->pullUpdates($remote, $channel_infos[$channel_id]['url'], $channel_infos[$channel_id]['url_uuid'], $channel_infos[$channel_id]['channel_entity_type']);
Timer::stop('io-pull-updates');
$io
->success($t('Channel successfully pulled. Number of updated entities: @count, execution time: @time ms', [
'@count' => $update_count,
'@time' => Timer::read('io-pull-updates'),
]));
}
public function pullUpdates(RemoteInterface $remote, $channel_url, $channel_url_uuid, $entity_type_id) {
$this->jsonapiHelper
->setRemote($remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($remote);
$original_channel_url = $channel_url;
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$offset = 0;
$update_count = 0;
while ($channel_url) {
$parsed_url = UrlHelper::parse($channel_url_uuid);
$parsed_url['query']['page']['offset'] = $offset;
$query = UrlHelper::buildQuery($parsed_url['query']);
$revisions_url = $parsed_url['path'] . '?' . $query;
$response = $this->requestService
->request($http_client, 'GET', $revisions_url);
$revisions_json = Json::decode((string) $response
->getBody());
$uuids = [];
foreach ($revisions_json['data'] as $row) {
$changed_datetime_timestamp = 0;
if (is_numeric($row['attributes']['changed'])) {
$changed_datetime_timestamp = (int) $row['attributes']['changed'];
}
elseif ($changed_datetime = \DateTime::createFromFormat(\DateTime::RFC3339, $row['attributes']['changed'])) {
$changed_datetime_timestamp = $changed_datetime
->getTimestamp();
}
$entity_changed = $storage
->getQuery()
->condition('uuid', $row['id'])
->condition('changed', $changed_datetime_timestamp)
->count()
->execute();
if ($entity_changed == 0) {
$uuids[] = $row['id'];
}
}
if (!empty($uuids)) {
$filter = [
'filter' => [
'uuid-filter' => [
'condition' => [
'path' => 'id',
'operator' => 'IN',
'value' => $uuids,
],
],
],
];
$parsed_original_channel_url = UrlHelper::parse($original_channel_url);
$filter_query = $parsed_original_channel_url['query'];
$filter_query = array_merge_recursive($filter_query, $filter);
$filter_query = UrlHelper::buildQuery($filter_query);
$filtered_url = $parsed_original_channel_url['path'] . '?' . $filter_query;
$response = $this->requestService
->request($http_client, 'GET', $filtered_url);
$json = Json::decode((string) $response
->getBody());
$imported_entities = $this->jsonapiHelper
->importEntityListData(EntityShareUtility::prepareData($json['data']));
$update_count += count($imported_entities);
}
if (isset($revisions_json['links']['next']['href'])) {
$channel_url = $revisions_json['links']['next']['href'];
}
else {
$channel_url = FALSE;
}
$offset += 50;
}
return $update_count;
}
public function getErrors() {
return $this->errors;
}
}