DeployLogger.php in Build Hooks 3.x
File
src/DeployLogger.php
View source
<?php
namespace Drupal\build_hooks;
use Drupal\build_hooks\Entity\DeploymentInterface;
use Drupal\build_hooks\Entity\FrontendEnvironment;
use Drupal\build_hooks\Entity\FrontendEnvironmentInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem;
class DeployLogger {
const LOGGER_CHANNEL_NAME = 'build_hooks_logger';
protected $configFactory;
protected $entityTypeManager;
protected $time;
public function __construct(ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager, TimeInterface $time) {
$this->configFactory = $configFactory;
$this->entityTypeManager = $entityTypeManager;
$this->time = $time;
}
public function isEntityTypeLoggable(ContentEntityInterface $entity) {
$entityType = $entity
->getEntityTypeId();
$selectedEntityTypes = $this->configFactory
->get('build_hooks.settings')
->get('logging.entity_types');
return in_array($entityType, array_values($selectedEntityTypes), TRUE);
}
public function logEntityCreated(ContentEntityInterface $entity) {
$deployment_storage = $this->entityTypeManager
->getStorage('build_hooks_deployment');
foreach ($this->entityTypeManager
->getStorage('frontend_environment')
->loadMultiple() as $environment) {
$deployment = $deployment_storage
->getOrCreateNextDeploymentForEnvironment($environment);
if (!$this
->entityAlreadyExistsInDeployment($deployment, $entity)) {
$deployment->contents[] = [
'target_id' => $entity
->id(),
'target_type' => $entity
->getEntityTypeId(),
];
$deployment
->save();
}
}
}
public function logEntityUpdated(ContentEntityInterface $entity) {
$this
->logEntityCreated($entity);
}
public function logEntityDeleted(ContentEntityInterface $entity) {
$deployment_storage = $this->entityTypeManager
->getStorage('build_hooks_deployment');
foreach ($this->entityTypeManager
->getStorage('frontend_environment')
->loadMultiple() as $environment) {
$deployment = $deployment_storage
->getOrCreateNextDeploymentForEnvironment($environment);
$deployment->deleted[] = sprintf('%s (%s)', $entity
->label(), $this->entityTypeManager
->getDefinition($entity
->getEntityTypeId())
->getLabel());
$deployment
->save();
}
}
public function setLastDeployTimeForEnvironment(FrontendEnvironmentInterface $environment, int $timestamp = NULL) {
$deployment_storage = $this->entityTypeManager
->getStorage('build_hooks_deployment');
$deployment = $deployment_storage
->getOrCreateNextDeploymentForEnvironment($environment);
$deployment->deployed = $timestamp ?: $this->time
->getCurrentTime();
$deployment->status = 1;
$deployment
->save();
}
public function getLastDeployTimeForEnvironment(FrontendEnvironment $environment) {
$deployment_storage = $this->entityTypeManager
->getStorage('build_hooks_deployment');
if ($last = $deployment_storage
->getLastDeploymentForEnvironment($environment)) {
return $last->deployed->value;
}
return 0;
}
public function getLogItemsSinceTimestamp($timestamp) {
@trigger_error(__METHOD__ . ' is deprecated in build_hooks:8.x-2.4 and is removed from build_hooks:8.x-3.0. There is no replacement, instead work with the deployment content entity. See https://www.drupal.org/node/3172327', E_USER_DEPRECATED);
return [];
}
public function getNumberOfItemsSinceLastDeploymentForEnvironment(FrontendEnvironment $environment) {
$deployment_storage = $this->entityTypeManager
->getStorage('build_hooks_deployment');
$next = $deployment_storage
->getOrCreateNextDeploymentForEnvironment($environment);
return count(array_filter(iterator_to_array($next
->get('contents')), function (DynamicEntityReferenceItem $item) {
return $item->entity;
})) + $next
->get('deleted')
->count();
}
protected function entityAlreadyExistsInDeployment(DeploymentInterface $deployment, ContentEntityInterface $entity) : bool {
if ($deployment
->get('contents')
->isEmpty()) {
return FALSE;
}
$existing_items = array_column(array_filter($deployment
->get('contents')
->getValue(), function (array $item) use ($entity) {
return $item['target_type'] === $entity
->getEntityTypeId();
}), 'target_id');
$string_ids = array_map(function ($id) {
return (string) $id;
}, $existing_items);
if (in_array((string) $entity
->id(), $string_ids, TRUE)) {
return TRUE;
}
return FALSE;
}
}