Open Data Catalog v
OrphanChecker.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Queue\QueueFactory;
7 
12  use HelperTrait;
13 
19  protected $queueService;
20 
24  public function __construct(ConfigFactoryInterface $configService, QueueFactory $queueService) {
25  $this->queueService = $queueService;
26  $this->setConfigService($configService);
27  }
28 
44  public function processReferencesInDeletedDataset($data) {
45  if (!is_object($data)) {
46  throw new \Exception("data must be an object.");
47  }
48  // Cycle through the dataset properties we seek to reference.
49  foreach ($this->getPropertyList() as $property_id) {
50  if (isset($data->{$property_id})) {
51  $this->processReferencesInDeletedProperty($property_id, $data->{$property_id});
52  }
53  }
54  }
55 
64  public function processReferencesInUpdatedDataset($old_dataset, $new_dataset) {
65  $this->objectsCheck([$old_dataset, $new_dataset]);
66  // Cycle through the dataset properties being referenced, check for orphans.
67  foreach ($this->getPropertyList() as $property_id) {
68  if (!isset($old_dataset->{$property_id})) {
69  // The old dataset had no value for this property, thus no references
70  // could be deleted. Safe to skip checking for orphan reference.
71  continue;
72  }
73  $oldProperty = $old_dataset->{$property_id};
74  $newProperty = (!isset($new_dataset->{$property_id})) ?
75  $this->emptyPropertyOfSameType($oldProperty) :
76  $new_dataset->{$property_id};
77 
78  $this->processReferencesInUpdatedProperty($property_id, $oldProperty, $newProperty);
79  }
80  }
81 
90  private function processReferencesInDeletedProperty($property_id, $uuids) {
91  // Treat single uuid as an array of one uuid.
92  if (!is_array($uuids)) {
93  $uuids = [$uuids];
94  }
95  foreach ($uuids as $uuid) {
96  $this->queueReferenceForRemoval($property_id, $uuid);
97  }
98  }
99 
110  private function queueReferenceForRemoval($property_id, $uuid) {
111  $this->queueService->get('orphan_reference_processor')
112  ->createItem([
113  $property_id,
114  $uuid,
115  ]);
116  }
117 
128  private function processReferencesInUpdatedProperty($property_id, $old_value, $new_value) {
129  if (!is_array($old_value)) {
130  $old_value = [$old_value];
131  $new_value = [$new_value];
132  }
133 
134  foreach (array_diff($old_value, $new_value) as $removed_reference) {
135  $this->queueReferenceForRemoval($property_id, $removed_reference);
136  }
137  }
138 
142  private function objectsCheck($objects) {
143  foreach ($objects as $object) {
144  if (!is_object($object)) {
145  throw new \Exception("data given must be an object.");
146  }
147  }
148  }
149 
150 }
Drupal\metastore\Reference\OrphanChecker\__construct
__construct(ConfigFactoryInterface $configService, QueueFactory $queueService)
Definition: OrphanChecker.php:24
Drupal\metastore\Reference\HelperTrait
trait HelperTrait
Definition: HelperTrait.php:11
Drupal\metastore\Reference\OrphanChecker\$queueService
$queueService
Definition: OrphanChecker.php:19
Drupal\metastore\Reference\OrphanChecker\processReferencesInUpdatedDataset
processReferencesInUpdatedDataset($old_dataset, $new_dataset)
Definition: OrphanChecker.php:64
Drupal\metastore\Reference
Definition: Dereferencer.php:3
Drupal\metastore\Reference\OrphanChecker
Definition: OrphanChecker.php:11
Drupal\metastore\Reference\OrphanChecker\processReferencesInDeletedDataset
processReferencesInDeletedDataset($data)
Definition: OrphanChecker.php:44