From f19338fc7d354dd4c6060e70411f88ade7698f34 Mon Sep 17 00:00:00 2001 From: Chris MacDonald <31731869+chrismacdonaldw@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:26:50 -0400 Subject: [PATCH] Delete FixityCheck entity if file entity doesn't exist --- src/Entity/FixityCheck.php | 8 +++++++- src/Plugin/QueueWorker/FixityCheckWorker.php | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Entity/FixityCheck.php b/src/Entity/FixityCheck.php index 1c95791..4ea5099 100644 --- a/src/Entity/FixityCheck.php +++ b/src/Entity/FixityCheck.php @@ -208,7 +208,13 @@ class FixityCheck extends ContentEntityBase implements FixityCheckInterface { public function getFile(): ?File { /** @var \Drupal\Core\Field\EntityReferenceFieldItemList $file */ $file = $this->file; - return $file->isEmpty() ? NULL : $file->referencedEntities()[0]; + + if ($file->isEmpty()) { + return NULL; + } + + $referenced_entities = $file->referencedEntities(); + return !empty($referenced_entities) ? reset($referenced_entities) : NULL; } /** diff --git a/src/Plugin/QueueWorker/FixityCheckWorker.php b/src/Plugin/QueueWorker/FixityCheckWorker.php index 2bc09b5..2872688 100644 --- a/src/Plugin/QueueWorker/FixityCheckWorker.php +++ b/src/Plugin/QueueWorker/FixityCheckWorker.php @@ -7,6 +7,7 @@ use Drupal\Core\Queue\QueueWorkerBase; use Drupal\dgi_fixity\FixityCheckServiceInterface; use Drupal\dgi_fixity\FixityCheckInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use function PHPUnit\Framework\isEmpty; /** * Performs a fixity check. @@ -60,6 +61,10 @@ class FixityCheckWorker extends QueueWorkerBase implements ContainerFactoryPlugi */ public function processItem($data) { if ($data instanceof FixityCheckInterface) { + if (isEmpty($data->getFile())) { + $data->delete(); + return; + } /** @var \Drupal\dgi_fixity\FixityCheckInterface $data */ $this->fixity->check($data->getFile()); }