Open Data Catalog v
Dereferencer.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Contracts\FactoryInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
9 
13 class Dereferencer {
14  use HelperTrait;
15  use LoggerTrait;
16 
22  private $storageFactory;
23 
27  public function __construct(ConfigFactoryInterface $configService, FactoryInterface $storageFactory) {
28  $this->setConfigService($configService);
29  $this->storageFactory = $storageFactory;
30  }
31 
41  public function dereference($data) {
42  $this->validate($data);
43 
44  // Cycle through the dataset properties we seek to dereference.
45  foreach ($this->getPropertyList() as $propertyId) {
46  if (isset($data->{$propertyId})) {
47  $this->dereferenceProperty($propertyId, $data);
48  }
49  }
50  return $data;
51  }
52 
61  private function dereferenceProperty(string $propertyId, $data) {
62  $referenceProperty = "%Ref:{$propertyId}";
63  $ref = NULL;
64  $actual = NULL;
65  [$ref, $actual] = $this->dereferencePropertyUuid($propertyId, $data->{$propertyId});
66  if (!empty($ref) && !empty($actual)) {
67  $data->{$referenceProperty} = $ref;
68  $data->{$propertyId} = $actual;
69  }
70  else {
71  unset($data->{$propertyId});
72  }
73  }
74 
86  private function dereferencePropertyUuid(string $property_id, $uuid) {
87  if (is_array($uuid)) {
88  return $this->dereferenceMultiple($property_id, $uuid);
89  }
90  elseif (is_string($uuid) && $this->getUuidService()->isValid($uuid)) {
91  return $this->dereferenceSingle($property_id, $uuid);
92  }
93  else {
94  $this->log('value_referencer', 'Unexpected data type when dereferencing property_id: @property_id with uuid: @uuid',
95  [
96  '@property_id' => $property_id,
97  '@uuid' => var_export($uuid, TRUE),
98  ]);
99  return NULL;
100  }
101  }
102 
114  private function dereferenceMultiple(string $property_id, array $uuids) : array {
115  $result = [];
116  $reference = [];
117  $ref = NULL;
118  $actual = NULL;
119  foreach ($uuids as $uuid) {
120  [$ref, $actual] = $this->dereferenceSingle($property_id, $uuid);
121  if (NULL !== $ref && NULL !== $actual) {
122  $result[] = $actual;
123  $reference[] = $ref;
124  }
125  }
126  return [$reference, $result];
127  }
128 
143  private function dereferenceSingle(string $property_id, string $uuid) {
144  $storage = $this->storageFactory->getInstance($property_id);
145  try {
146  $value = $storage->retrieve($uuid);
147  }
148  catch (MissingObjectException $exception) {
149  $value = FALSE;
150  }
151 
152  if ($value) {
153  $metadata = json_decode($value);
154  return [$metadata, $metadata->data];
155  }
156 
157  // If a property node was not found, it most likely means it was deleted
158  // while still being referenced.
159  $this->log(
160  'value_referencer',
161  'Property @property_id reference @uuid not found',
162  [
163  '@property_id' => $property_id,
164  '@uuid' => var_export($uuid, TRUE),
165  ]
166  );
167 
168  return [NULL, NULL];
169  }
170 
179  private function validate($data) {
180  if (!is_object($data)) {
181  throw new \Exception("data must be an object.");
182  }
183  }
184 
185 }
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\metastore\Reference\Dereferencer\dereference
dereference($data)
Definition: Dereferencer.php:41
Drupal\metastore\Reference\HelperTrait
trait HelperTrait
Definition: HelperTrait.php:11
Drupal\metastore\Reference
Definition: Dereferencer.php:3
Drupal\metastore\Reference\Dereferencer
Definition: Dereferencer.php:13
Drupal\metastore\Exception\MissingObjectException
Definition: MissingObjectException.php:10
Drupal\metastore\Reference\Dereferencer\__construct
__construct(ConfigFactoryInterface $configService, FactoryInterface $storageFactory)
Definition: Dereferencer.php:27