View source
<?php
namespace Drupal\gatsby_fastbuilds;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\gatsby_fastbuilds\Entity\GatsbyLogEntityInterface;
use Drupal\gatsby_instantpreview\GatsbyInstantPreview;
class GatsbyEntityLogger {
protected $config;
protected $entityTypeManager;
protected $gatsbyInstantPreview;
public function __construct(ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager, GatsbyInstantPreview $gatsbyInstantPreview) {
$this->config = $config
->get('gatsby.settings');
$this->entityTypeManager = $entity_type_manager;
$this->gatsbyInstantPreview = $gatsbyInstantPreview;
}
public function logEntity(ContentEntityInterface $entity, string $action) {
$this
->deleteLoggedEntity($entity
->uuid(), $entity
->language()
->getId());
$json = [];
if ($action !== 'delete') {
$json = $this->gatsbyInstantPreview
->getJson($entity);
if (!$json) {
return;
}
}
$json['id'] = $entity
->uuid();
$json['action'] = $action;
$json['langcode'] = $entity
->language()
->getId();
$json['attributes'] = [
'langcode' => $entity
->language()
->getId(),
'drupal_internal__revision_id' => $entity
->getRevisionId(),
];
$log_entry = [
'entity_uuid' => $entity
->uuid(),
'title' => $entity
->label(),
'entity' => $entity
->getEntityTypeId(),
'bundle' => $entity
->bundle(),
'langcode' => $entity
->language()
->getId(),
'action' => $action,
'json' => json_encode($json),
];
$log = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->create($log_entry);
$log
->save();
}
public function logEntityWithRelationships(ContentEntityInterface $entity, string $action) {
$this
->deleteLoggedEntity($entity
->uuid(), $entity
->language()
->getId());
$json = [];
if ($action !== 'delete') {
$json = $this->gatsbyInstantPreview
->getJson($entity);
if (!$json) {
return;
}
if (!empty($json['data']['relationships'])) {
$entity_data = [];
$this->gatsbyInstantPreview
->buildRelationshipJson($json['data']['relationships'], $entity_data);
if (!empty($entity_data)) {
$entity_data = array_values($entity_data);
$original_data = $json['data'];
$entity_data[] = $original_data;
$json['data'] = $entity_data;
}
}
}
$json['id'] = $entity
->uuid();
$json['action'] = $action;
$log_entry = [
'entity_uuid' => $entity
->uuid(),
'title' => $entity
->label(),
'entity' => $entity
->getEntityTypeId(),
'bundle' => $entity
->bundle(),
'langcode' => $entity
->language()
->getId(),
'action' => $action,
'json' => json_encode($json),
];
$log = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->create($log_entry);
$log
->save();
}
public function deleteLoggedEntity($uuid, $langcode = 'en') {
$query = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->getQuery()
->accessCheck(FALSE);
$entity_uuids = $query
->condition('entity_uuid', $uuid)
->condition('langcode', $langcode)
->execute();
$entities = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->loadMultiple($entity_uuids);
foreach ($entities as $entity) {
$entity
->delete();
}
}
public function deleteExpiredLoggedEntities($timestamp) {
$query = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->getQuery()
->accessCheck(FALSE);
$entity_uuids = $query
->condition('created', $timestamp, '<')
->execute();
$entities = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->loadMultiple($entity_uuids);
foreach ($entities as $entity) {
$entity
->delete();
}
}
public function getOldestLoggedEntityTimestamp() {
$query = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->getQuery()
->accessCheck(FALSE);
$entity_uuids = $query
->sort('created')
->range(0, 1)
->execute();
$entities = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->loadMultiple($entity_uuids);
if (!empty($entities)) {
$entity = array_pop($entities);
return $entity
->getCreatedTime();
}
return FALSE;
}
public function getSync($last_fetch) {
$query = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->getQuery()
->accessCheck(FALSE);
$entity_uuids = $query
->condition('created', $last_fetch, '>')
->sort('created')
->execute();
$entities = $this->entityTypeManager
->getStorage('gatsby_log_entity')
->loadMultiple($entity_uuids);
$sync_data = [
'timestamp' => time(),
'entities' => [],
];
foreach ($entities as $entity) {
if ($entity instanceof GatsbyLogEntityInterface) {
$sync_data['timestamp'] = $entity
->getCreatedTime();
$sync_data['entities'][] = json_decode($entity
->get('json')->value);
}
}
return $sync_data;
}
}