Open Data Catalog v
ReferenceLookup.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
9 use Contracts\FactoryInterface;
10 use Drupal\Core\Cache\Cache;
11 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
12 use Drupal\Core\Extension\ModuleHandlerInterface;
13 use RootedData\RootedJsonData;
14 
19  use HelperTrait;
20  use LoggerTrait;
21 
27  protected $metastoreStorage;
28 
35 
41  public function __construct(
42  FactoryInterface $metastoreStorage,
44  CacheTagsInvalidatorInterface $invalidator,
45  ModuleHandlerInterface $moduleHandler
46  ) {
47  $this->metastoreStorage = $metastoreStorage;
48  $this->metastoreItemFactory = $metastoreItemFactory;
49  $this->invalidator = $invalidator;
50  $this->moduleHandler = $moduleHandler;
51  }
52 
58  public function getReferencers(string $schemaId, string $referenceId, string $propertyId) {
59  // This will give us a smaller subset of metastore items to parse through.
60  $metastoreItems = $this->metastoreStorage->getInstance($schemaId)->retrieveContains($referenceId);
61 
62  $referencers = [];
63  foreach ($metastoreItems as $item) {
64  [$identifier, $metadata] = $this->decodeJsonMetadata($item);
65  $propertyValue = $metadata->{$propertyId};
66  // Check if uuid is found either directly or in an array.
67  $idIsValue = $referenceId == $propertyValue;
68  $idInArray = is_array($propertyValue) && in_array($referenceId, $propertyValue);
69  if ($idIsValue || $idInArray) {
70  $referencers[] = $identifier;
71  }
72  }
73 
74  return $referencers;
75  }
76 
87  public function invalidateReferencerCacheTags(string $schemaId, string $referenceId, string $propertyId) {
88  $referencers = $this->getReferencers($schemaId, $referenceId, $propertyId);
89  $tags = [];
90  foreach ($referencers as $identifier) {
91  $item = $this->metastoreItemFactory->getInstance($identifier);
92  $tags = Cache::mergeTags($tags, $item->getCacheTags());
93  }
94  $this->invalidator->invalidateTags($tags);
95  }
96 
106  protected function decodeJsonMetadata(string $json): array {
107  // Decode the supplied JSON metadata string.
108  $metadata = json_decode($json);
109  // Determine the path to the legacy metadata schema file.
110  $module_path = $this->moduleHandler->getModule(get_module_name())->getPath();
111  $legacy_schema_path = $module_path . '/docs/legacy_metadata.json';
112  // Fetch the legacy metadata schema.
113  $legacy_schema = file_get_contents($legacy_schema_path);
114  // Record metadata identifier.
115  $identifier = $metadata->identifier;
116  // Get raw metadata using identifier.
117  $metadata = $this->metastoreItemFactory->getInstance($identifier)->getRawMetadata();
118  // Validate JSON against legacy schema.
119  $validation_result = RootedJsonData::validate(json_encode($metadata), $legacy_schema);
120  // If the JSON metadata matches the legacy schema, extract the content of
121  // the "data" property.
122  if ($validation_result->isValid()) {
123  $metadata = $metadata->data;
124  }
125 
126  return [$identifier, $metadata];
127  }
128 
129 }
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\metastore\Factory\MetastoreItemFactoryInterface
Definition: MetastoreItemFactoryInterface.php:15
Drupal\metastore\Reference\ReferenceLookup\__construct
__construct(FactoryInterface $metastoreStorage, MetastoreItemFactoryInterface $metastoreItemFactory, CacheTagsInvalidatorInterface $invalidator, ModuleHandlerInterface $moduleHandler)
Definition: ReferenceLookup.php:41
Drupal\metastore\Reference\ReferenceLookup\$metastoreItemFactory
$metastoreItemFactory
Definition: ReferenceLookup.php:34
Drupal\metastore\Reference\HelperTrait
trait HelperTrait
Definition: HelperTrait.php:11
Drupal\metastore\Reference\ReferenceLookup\invalidateReferencerCacheTags
invalidateReferencerCacheTags(string $schemaId, string $referenceId, string $propertyId)
Definition: ReferenceLookup.php:87
Drupal\metastore\Reference\ReferenceLookup
Definition: ReferenceLookup.php:18
Drupal\metastore\Reference
Definition: Dereferencer.php:3
Drupal\metastore\Reference\ReferenceLookup\$metastoreStorage
$metastoreStorage
Definition: ReferenceLookup.php:27
Drupal\metastore\ReferenceLookupInterface
Definition: ReferenceLookupInterface.php:8
Drupal\metastore\Reference\ReferenceLookup\decodeJsonMetadata
decodeJsonMetadata(string $json)
Definition: ReferenceLookup.php:106
Drupal\metastore\Reference\ReferenceLookup\getReferencers
getReferencers(string $schemaId, string $referenceId, string $propertyId)
Definition: ReferenceLookup.php:58