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 
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\Core\Queue\QueueWorkerBase;
10 use Drupal\node\NodeStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\common\EventDispatcherTrait;
14 
26 class OrphanReferenceProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {
27  use LoggerTrait;
28  use EventDispatcherTrait;
29 
30  const EVENT_ORPHANING_DISTRIBUTION = 'metastore_orphaning_distribution';
31 
37  protected $nodeStorage;
38 
53  public function __construct(
54  array $configuration,
55  $plugin_id,
56  $plugin_definition,
57  NodeStorageInterface $nodeStorage,
58  ReferenceLookupInterface $referenceLookup) {
59  parent::__construct($configuration, $plugin_id, $plugin_definition);
60  $this->referenceLookup = $referenceLookup;
61  $this->nodeStorage = $nodeStorage;
62  }
63 
69  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
70  $me = new static(
71  $configuration,
72  $plugin_id,
73  $plugin_definition,
74  $container->get('dkan.common.node_storage'),
75  $container->get('dkan.metastore.reference_lookup')
76  );
77  $me->setLoggerFactory($container->get('logger.factory'));
78  return $me;
79  }
80 
86  public function processItem($data) {
87  $metadataProperty = $data[0];
88  $identifier = $data[1];
89  $referencers = $this->referenceLookup->getReferencers('dataset', $identifier, $metadataProperty);
90 
91  if (!empty($referencers)) {
92  return;
93  }
94 
95  // Value reference uuid not found in any dataset, therefore safe to delete.
96  $this->unpublishReference($metadataProperty, $identifier);
97  }
98 
107  protected function unpublishReference(string $property_id, string $uuid) {
108  $references = $this->nodeStorage->loadByProperties(
109  [
110  'uuid' => $uuid,
111  'field_data_type' => $property_id,
112  ]
113  );
114  // The reference might be deleted manually beforehand.
115  if (FALSE !== ($reference = reset($references))) {
116  // When orphaning distribution nodes, trigger database clean up.
117  if ($property_id === 'distribution') {
118  $this->dispatchEvent(self::EVENT_ORPHANING_DISTRIBUTION, $uuid);
119  }
120  $reference->set('moderation_state', 'orphaned');
121  $reference->save();
122  }
123  }
124 
125 }
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor\__construct
__construct(array $configuration, $plugin_id, $plugin_definition, NodeStorageInterface $nodeStorage, ReferenceLookupInterface $referenceLookup)
Definition: OrphanReferenceProcessor.php:53
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor\$nodeStorage
$nodeStorage
Definition: OrphanReferenceProcessor.php:37
Drupal\metastore\Plugin\QueueWorker
Definition: OrphanReferenceProcessor.php:5
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor\create
static create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
Definition: OrphanReferenceProcessor.php:69
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor
Definition: OrphanReferenceProcessor.php:26
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor\unpublishReference
unpublishReference(string $property_id, string $uuid)
Definition: OrphanReferenceProcessor.php:107
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor\EVENT_ORPHANING_DISTRIBUTION
const EVENT_ORPHANING_DISTRIBUTION
Definition: OrphanReferenceProcessor.php:30
Drupal\metastore\ReferenceLookupInterface
Definition: ReferenceLookupInterface.php:8
Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor\processItem
processItem($data)
Definition: OrphanReferenceProcessor.php:86