Open Data Catalog v2.0.0
OrphanReferenceProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types = 1);
4 
6 
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\Core\Queue\QueueWorkerBase;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 
23 class OrphanReferenceProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {
24 
30  protected $entityTypeManager;
31 
44  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
45  parent::__construct($configuration, $plugin_id, $plugin_definition);
46  $this->entityTypeManager = $entity_type_manager;
47  }
48 
54  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55  return new static(
56  $configuration,
57  $plugin_id,
58  $plugin_definition,
59  $container->get('entity_type.manager')
60  );
61  }
62 
68  public function processItem($data) {
69  $property_id = $data[0];
70  $uuid = $data[1];
71 
72  // Search datasets using this uuid for this property id.
73  $datasets = $this->entityTypeManager->getStorage('node')
74  ->loadByProperties(
75  [
76  'field_data_type' => 'dataset',
77  ]
78  );
79  foreach ($datasets as $dataset) {
80  $data = json_decode($dataset->referenced_metadata);
81  $value = $data->{$property_id};
82  // Check if uuid is found either directly or in an array.
83  $uuid_is_value = $uuid == $value;
84  $uuid_found_in_array = is_array($value) && in_array($uuid, $value);
85  if ($uuid_is_value || $uuid_found_in_array) {
86  // Uuid found in use, abort.
87  return;
88  }
89  }
90 
91  // Value reference uuid not found in any dataset, therefore safe to delete.
92  $this->deleteReference($property_id, $uuid);
93  }
94 
103  protected function deleteReference(string $property_id, string $uuid) {
104  $references = $this->entityTypeManager->getStorage('node')
105  ->loadByProperties(
106  [
107  'field_data_type' => $property_id,
108  'uuid' => $uuid,
109  ]
110  );
111  if (FALSE !== ($reference = reset($references))) {
112  $reference->delete();
113  }
114  }
115 
116 }
Drupal\dkan_data\Plugin\QueueWorker\OrphanReferenceProcessor
Definition: OrphanReferenceProcessor.php:23
Drupal\dkan_data\Plugin\QueueWorker\OrphanReferenceProcessor\deleteReference
deleteReference(string $property_id, string $uuid)
Definition: OrphanReferenceProcessor.php:103
Drupal\dkan_data\Plugin\QueueWorker\OrphanReferenceProcessor\processItem
processItem($data)
Definition: OrphanReferenceProcessor.php:68
Drupal\dkan_data\Plugin\QueueWorker
Definition: OrphanReferenceProcessor.php:5
Drupal\dkan_data\Plugin\QueueWorker\OrphanReferenceProcessor\create
static create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
Definition: OrphanReferenceProcessor.php:54
Drupal\dkan_data\Plugin\QueueWorker\OrphanReferenceProcessor\$entityTypeManager
$entityTypeManager
Definition: OrphanReferenceProcessor.php:30
Drupal\dkan_data\Plugin\QueueWorker\OrphanReferenceProcessor\__construct
__construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager)
Definition: OrphanReferenceProcessor.php:44