Open Data Catalog v2.0.0
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;
8 use stdClass;
9 
14  use HelperTrait;
15  use LoggerTrait;
16 
22  protected $queueService;
23 
27  public function __construct(ConfigFactoryInterface $configService, QueueFactory $queueService) {
28  $this->queueService = $queueService;
29  $this->setConfigService($configService);
30  }
31 
47  public function processReferencesInDeletedDataset(stdClass $data) {
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(stdClass $old_dataset, stdClass $new_dataset) {
65  // Cycle through the dataset properties being referenced, check for orphans.
66  foreach ($this->getPropertyList() as $property_id) {
67  if (!isset($old_dataset->{$property_id})) {
68  // The old dataset had no value for this property, thus no references
69  // could be deleted. Safe to skip checking for orphan reference.
70  continue;
71  }
72  $oldProperty = $old_dataset->{$property_id};
73  $newProperty = (!isset($new_dataset->{$property_id})) ?
74  $this->emptyPropertyOfSameType($oldProperty) :
75  $new_dataset->{$property_id};
76 
77  $this->processReferencesInUpdatedProperty($property_id, $oldProperty, $newProperty);
78  }
79  }
80 
89  private function processReferencesInDeletedProperty($property_id, $uuids) {
90  // Treat single uuid as an array of one uuid.
91  if (!is_array($uuids)) {
92  $uuids = [$uuids];
93  }
94  foreach ($uuids as $uuid) {
95  $this->queueReferenceForRemoval($property_id, $uuid);
96  }
97  }
98 
109  private function queueReferenceForRemoval($property_id, $uuid) {
110  $this->queueService->get('orphan_reference_processor')
111  ->createItem([
112  $property_id,
113  $uuid,
114  ]);
115  }
116 
127  private function processReferencesInUpdatedProperty($property_id, $old_value, $new_value) {
128  if (!is_array($old_value)) {
129  $old_value = [$old_value];
130  $new_value = [$new_value];
131  }
132  foreach (array_diff($old_value, $new_value) as $removed_reference) {
133  $this->queueReferenceForRemoval($property_id, $removed_reference);
134  }
135  }
136 
137 }
Drupal\dkan_data\Reference\OrphanChecker\processReferencesInDeletedDataset
processReferencesInDeletedDataset(stdClass $data)
Definition: OrphanChecker.php:47
Drupal\dkan_data\Reference
Definition: Dereferencer.php:3
Drupal\dkan_data\Reference\HelperTrait
trait HelperTrait
Definition: HelperTrait.php:11
Drupal\dkan_data\Reference\OrphanChecker\processReferencesInUpdatedDataset
processReferencesInUpdatedDataset(stdClass $old_dataset, stdClass $new_dataset)
Definition: OrphanChecker.php:64
Drupal\dkan_data\Reference\OrphanChecker
Definition: OrphanChecker.php:13
Drupal\dkan_data\Reference\OrphanChecker\$queueService
$queueService
Definition: OrphanChecker.php:22
Drupal\dkan_data\Reference\OrphanChecker\__construct
__construct(ConfigFactoryInterface $configService, QueueFactory $queueService)
Definition: OrphanChecker.php:27
Drupal\dkan_common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11