View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Service;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\entity_share\EntityShareUtility;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
class StateInformation implements StateInformationInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $resourceTypeRepository;
protected $time;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ResourceTypeRepositoryInterface $resource_type_repository, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->resourceTypeRepository = $resource_type_repository;
$this->time = $time;
}
public function getStatusInfo(array $data) {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_UNDEFINED);
$parsed_type = explode('--', $data['type']);
$entity_type_id = $parsed_type[0];
try {
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
} catch (\Exception $exception) {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_UNKNOWN);
return $status_info;
}
$existing_entities = $entity_storage
->loadByProperties([
'uuid' => $data['id'],
]);
if (empty($existing_entities)) {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_NEW);
}
else {
$existing_entity = array_shift($existing_entities);
$resource_type = $this->resourceTypeRepository
->get($parsed_type[0], $parsed_type[1]);
$changed_public_name = FALSE;
if ($resource_type
->hasField('changed')) {
$changed_public_name = $resource_type
->getPublicName('changed');
}
if (!empty($data['attributes'][$changed_public_name]) && method_exists($existing_entity, 'getChangedTime')) {
$entity_changed_time = EntityShareUtility::convertChangedTime($data['attributes'][$changed_public_name]);
$entity_keys = $entity_storage
->getEntityType()
->getKeys();
if (isset($entity_keys['langcode']) && !empty($entity_keys['langcode'])) {
$entity_language_id = $data['attributes'][$resource_type
->getPublicName($entity_keys['langcode'])];
if ($existing_entity
->hasTranslation($entity_language_id)) {
$existing_translation = $existing_entity
->getTranslation($entity_language_id);
if ($this
->entityHasChanged($existing_translation, $entity_changed_time)) {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_CHANGED, $existing_entity);
}
else {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_SYNCHRONIZED, $existing_entity);
}
}
else {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_NEW_TRANSLATION, $existing_entity);
}
}
else {
if ($this
->entityHasChanged($existing_entity, $entity_changed_time)) {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_CHANGED, $existing_entity);
}
else {
$status_info = $this
->statusInfoArray(StateInformationInterface::INFO_ID_SYNCHRONIZED, $existing_entity);
}
}
}
}
return $status_info;
}
public function getStatusDefinition(string $status_info_id) {
$definitions = [
StateInformationInterface::INFO_ID_UNDEFINED => [
'label' => $this
->t('Undefined'),
'class' => 'undefined',
],
StateInformationInterface::INFO_ID_UNKNOWN => [
'label' => $this
->t('Unknown entity type'),
'class' => 'undefined',
],
StateInformationInterface::INFO_ID_NEW => [
'label' => $this
->t('New entity'),
'class' => 'new',
],
StateInformationInterface::INFO_ID_NEW_TRANSLATION => [
'label' => $this
->t('New translation'),
'class' => 'new',
],
StateInformationInterface::INFO_ID_CHANGED => [
'label' => $this
->t('Entities not synchronized'),
'class' => 'changed',
],
StateInformationInterface::INFO_ID_SYNCHRONIZED => [
'label' => $this
->t('Entities synchronized'),
'class' => 'up-to-date',
],
];
return $definitions[$status_info_id] ?? $definitions[StateInformationInterface::INFO_ID_UNDEFINED];
}
protected function statusInfoArray(string $status_info_id, ContentEntityInterface $entity = NULL) {
$status_definition = $this
->getStatusDefinition($status_info_id);
$status_info = [
'label' => $status_definition['label'],
'class' => 'entity-share-' . $status_definition['class'],
'info_id' => $status_info_id,
'local_entity_link' => NULL,
'local_revision_id' => NULL,
];
if ($entity instanceof ContentEntityInterface) {
try {
$status_info['local_entity_link'] = $entity
->toUrl();
} catch (UndefinedLinkTemplateException $exception) {
}
$status_info['local_revision_id'] = $entity
->getRevisionId();
}
return $status_info;
}
protected function entityHasChanged(ContentEntityInterface $entity, int $remote_changed_time) {
$import_status_entity = $this
->getImportStatusOfEntity($entity);
if ($import_status_entity) {
return $import_status_entity
->getLastImport() < $remote_changed_time;
}
else {
return $entity
->getChangedTime() != $remote_changed_time;
}
}
public function createImportStatusOfEntity(ContentEntityInterface $entity, array $parameters) {
try {
$entity_storage = $this->entityTypeManager
->getStorage('entity_import_status');
$entity_import_status_data = [
'entity_id' => $entity
->id(),
'entity_uuid' => $entity
->uuid(),
'entity_type_id' => $entity
->getEntityTypeId(),
'entity_bundle' => $entity
->bundle(),
'last_import' => $this->time
->getRequestTime(),
];
if ($entity_storage
->getEntityType()
->hasKey('langcode')) {
$entity_import_status_data['langcode'] = $entity
->language()
->getId();
}
foreach ([
'remote_website',
'channel_id',
'policy',
] as $additional_parameter) {
if (!empty($parameters[$additional_parameter])) {
$entity_import_status_data[$additional_parameter] = $parameters[$additional_parameter];
}
}
$import_status_entity = $entity_storage
->create($entity_import_status_data);
$import_status_entity
->save();
return $import_status_entity;
} catch (\Exception $e) {
return FALSE;
}
}
public function getImportStatusByParameters(string $uuid, string $entity_type_id, string $langcode = NULL) {
$search_criteria = [
'entity_uuid' => $uuid,
'entity_type_id' => $entity_type_id,
];
if ($langcode) {
$search_criteria['langcode'] = $langcode;
}
$entity_storage = $this->entityTypeManager
->getStorage('entity_import_status');
$import_status_entities = $entity_storage
->loadByProperties($search_criteria);
if (!empty($import_status_entities)) {
return current($import_status_entities);
}
return FALSE;
}
public function getImportStatusOfEntity(ContentEntityInterface $entity) {
$entity_storage = $this->entityTypeManager
->getStorage('entity_import_status');
$langcode = NULL;
if ($entity_storage
->getEntityType()
->hasKey('langcode')) {
$langcode = $entity
->language()
->getId();
}
return $this
->getImportStatusByParameters($entity
->uuid(), $entity
->getEntityTypeId(), $langcode);
}
public function deleteImportStatusOfEntity(EntityInterface $entity, string $langcode = NULL) {
if (!$entity instanceof ContentEntityInterface) {
return;
}
if (in_array($entity
->getEntityTypeId(), [
'user',
'entity_import_status',
])) {
return;
}
if (!$entity
->uuid()) {
return;
}
$entity_storage = $this->entityTypeManager
->getStorage('entity_import_status');
$search_criteria = [
'entity_id' => $entity
->id(),
'entity_type_id' => $entity
->getEntityTypeId(),
];
if ($entity_storage
->getEntityType()
->hasKey('uuid')) {
$search_criteria['entity_uuid'] = $entity
->uuid();
}
if ($langcode && $entity_storage
->getEntityType()
->hasKey('langcode')) {
$search_criteria['langcode'] = $langcode;
}
$import_status_entities = $entity_storage
->loadByProperties($search_criteria);
if ($import_status_entities) {
foreach ($import_status_entities as $import_status_entity) {
$import_status_entity
->delete();
}
}
}
}