Open Data Catalog v2.0.0
LifeCycle.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\common\EventDispatcherTrait;
8 use Drupal\Core\Datetime\DateFormatter;
9 use Drupal\Core\Queue\QueueFactory;
16 
20 class LifeCycle {
21  use EventDispatcherTrait;
22 
23  const EVENT_DATASET_UPDATE = 'dkan_metastore_dataset_update';
24  const EVENT_PRE_REFERENCE = 'dkan_metastore_metadata_pre_reference';
25 
31  protected $referencer;
32 
38  protected $dereferencer;
39 
45  protected $orphanChecker;
46 
52  protected $resourceMapper;
53 
59  protected $dateFormatter;
60 
66  protected $dataFactory;
67 
73  protected $queueFactory;
74 
78  public function __construct(
83  DateFormatter $dateFormatter,
85  QueueFactory $queueFactory
86  ) {
87  $this->referencer = $referencer;
88  $this->dereferencer = $dereferencer;
89  $this->orphanChecker = $orphanChecker;
90  $this->resourceMapper = $resourceMapper;
91  $this->dateFormatter = $dateFormatter;
92  $this->dataFactory = $dataFactory;
93  $this->queueFactory = $queueFactory;
94  }
95 
104  public function go($stage, MetastoreItemInterface $data) {
105  $stage = ucwords($stage);
106  $method = "{$data->getSchemaId()}{$stage}";
107  if (method_exists($this, $method)) {
108  $this->{$method}($data);
109  }
110  }
111 
115  protected function datasetPredelete(MetastoreItemInterface $data) {
116  $raw = $data->getRawMetadata();
117 
118  if (is_object($raw)) {
119  $this->orphanChecker->processReferencesInDeletedDataset($raw);
120  }
121  }
122 
126  protected function datasetLoad(MetastoreItemInterface $data) {
127  $metadata = $data->getMetaData();
128 
129  // Dereference dataset properties.
130  $metadata = $this->dereferencer->dereference($metadata);
131  $metadata = $this->addDatasetModifiedDate($metadata, $data->getModifiedDate());
132 
133  $data->setMetadata($metadata);
134  }
135 
139  protected function datasetUpdate(MetastoreItemInterface $data) {
140  $this->dispatchEvent(self::EVENT_DATASET_UPDATE, $data);
141  }
142 
148  protected function distributionLoad(MetastoreItemInterface $data) {
149  $metadata = $data->getMetaData();
150 
151  if (!isset($metadata->data->downloadURL)) {
152  return;
153  }
154 
155  $downloadUrl = $metadata->data->downloadURL;
156 
157  if (isset($downloadUrl) && !filter_var($downloadUrl, FILTER_VALIDATE_URL)) {
158  $resourceIdentifier = $downloadUrl;
159  $ref = NULL;
160  $original = NULL;
161  [$ref, $original] = $this->retrieveDownloadUrlFromResourceMapper($resourceIdentifier);
162 
163  $downloadUrl = isset($original) ? $original : "";
164 
165  $refProperty = "%Ref:downloadURL";
166  $metadata->data->{$refProperty} = count($ref) == 0 ? NULL : $ref;
167  }
168 
169  if (is_string($downloadUrl)) {
170  $downloadUrl = UrlHostTokenResolver::resolve($downloadUrl);
171  }
172 
173  $metadata->data->downloadURL = $downloadUrl;
174 
175  $data->setMetadata($metadata);
176  }
177 
181  protected function distributionPredelete(MetastoreItemInterface $data) {
182  $distributionUuid = $data->getIdentifier();
183 
184  $storage = $this->dataFactory->getInstance('distribution');
185  $resource = $storage->retrieve($distributionUuid);
186  $resource = json_decode($resource);
187 
188  $id = $resource->data->{'%Ref:downloadURL'}[0]->data->identifier;
189  $perspective = $resource->data->{'%Ref:downloadURL'}[0]->data->perspective;
190  $version = $resource->data->{'%Ref:downloadURL'}[0]->data->version;
191 
192  // Ensure a valid resource ID was found since it's required.
193  if (isset($id)) {
194  $this->queueFactory->get('orphan_resource_remover')->createItem([
195  $id,
196  $perspective,
197  $version,
198  ]);
199  }
200  }
201 
211  private function retrieveDownloadUrlFromResourceMapper(string $resourceIdentifier) {
212  $reference = [];
213  $original = NULL;
214 
215  $info = Resource::parseUniqueIdentifier($resourceIdentifier);
216 
217  // Load resource object.
218  $sourceResource = $this->resourceMapper->get($info['identifier'], Resource::DEFAULT_SOURCE_PERSPECTIVE, $info['version']);
219 
220  if (!$sourceResource) {
221  return [$reference, $original];
222  }
223 
224  $reference[] = $this->createResourceReference($sourceResource);
225  $perspective = resource_mapper_display();
226  $resource = $sourceResource;
227 
228  if (
229  $perspective != Resource::DEFAULT_SOURCE_PERSPECTIVE &&
230  $new = $this->resourceMapper->get($info['identifier'], $perspective, $info['version'])
231  ) {
232  $resource = $new;
233  $reference[] = $this->createResourceReference($resource);
234  }
235  $original = $resource->getFilePath();
236 
237  return [$reference, $original];
238  }
239 
243  private function createResourceReference(Resource $resource): object {
244  return (object) [
245  "identifier" => $resource->getUniqueIdentifier(),
246  "data" => $resource,
247  ];
248  }
249 
253  protected function datasetPresave(MetastoreItemInterface $data) {
254  $metadata = $data->getMetaData();
255 
256  $title = isset($metadata->title) ? $metadata->title : $metadata->name;
257  $data->setTitle($title);
258 
259  // If there is no uuid add one.
260  if (!isset($metadata->identifier)) {
261  $metadata->identifier = $data->getIdentifier();
262  }
263  // If one exists in the uuid it should be the same in the table.
264  else {
265  $data->setIdentifier($metadata->identifier);
266  }
267 
268  $this->dispatchEvent(self::EVENT_PRE_REFERENCE, $data, function ($data) {
269  return $data instanceof MetastoreItemInterface;
270  });
271 
272  $metadata = $this->referencer->reference($metadata);
273 
274  $data->setMetadata($metadata);
275 
276  // Check for possible orphan property references when updating a dataset.
277  if (!$data->isNew()) {
278  $raw = $data->getRawMetadata();
279  $this->orphanChecker->processReferencesInUpdatedDataset($raw, $metadata);
280  }
281 
282  }
283 
287  protected function distributionPresave(MetastoreItemInterface $data) {
288  $metadata = $data->getMetaData();
289  $data->setMetadata($metadata);
290  }
291 
295  private function addDatasetModifiedDate($metadata, $date) {
296  $formattedChangedDate = $this->dateFormatter->format($date, 'html_datetime');
297  $metadata->{'%modified'} = $formattedChangedDate;
298  return $metadata;
299  }
300 
301 }
Drupal\metastore\LifeCycle\LifeCycle\__construct
__construct(Referencer $referencer, Dereferencer $dereferencer, OrphanChecker $orphanChecker, ResourceMapper $resourceMapper, DateFormatter $dateFormatter, DataFactory $dataFactory, QueueFactory $queueFactory)
Definition: LifeCycle.php:78
Drupal\metastore\MetastoreItemInterface\setIdentifier
setIdentifier($identifier)
Drupal\metastore\LifeCycle\LifeCycle\$resourceMapper
$resourceMapper
Definition: LifeCycle.php:52
Drupal\metastore\MetastoreItemInterface\setMetadata
setMetadata($metadata)
Drupal\metastore\LifeCycle\LifeCycle\$referencer
$referencer
Definition: LifeCycle.php:31
Drupal\metastore\LifeCycle\LifeCycle
Definition: LifeCycle.php:20
Drupal\metastore\LifeCycle\LifeCycle\$dereferencer
$dereferencer
Definition: LifeCycle.php:38
Drupal\metastore\LifeCycle\LifeCycle\$dataFactory
$dataFactory
Definition: LifeCycle.php:66
Drupal\common\Resource
Definition: Resource.php:29
Drupal\metastore\LifeCycle
Definition: LifeCycle.php:3
Drupal\metastore\LifeCycle\LifeCycle\$queueFactory
$queueFactory
Definition: LifeCycle.php:73
Drupal\metastore\LifeCycle\LifeCycle\distributionLoad
distributionLoad(MetastoreItemInterface $data)
Definition: LifeCycle.php:148
Drupal\metastore\LifeCycle\LifeCycle\$orphanChecker
$orphanChecker
Definition: LifeCycle.php:45
Drupal\metastore\LifeCycle\LifeCycle\go
go($stage, MetastoreItemInterface $data)
Definition: LifeCycle.php:104
Drupal\metastore\MetastoreItemInterface\getRawMetadata
getRawMetadata()
Drupal\metastore\LifeCycle\LifeCycle\datasetUpdate
datasetUpdate(MetastoreItemInterface $data)
Definition: LifeCycle.php:139
Drupal\common\UrlHostTokenResolver\resolve
static resolve(string $resourceUrl)
Definition: UrlHostTokenResolver.php:37
Drupal\metastore\LifeCycle\LifeCycle\datasetPresave
datasetPresave(MetastoreItemInterface $data)
Definition: LifeCycle.php:253
Drupal\metastore\Reference\Dereferencer
Definition: Dereferencer.php:13
Drupal\metastore\Storage\DataFactory
Definition: DataFactory.php:11
Drupal\metastore\LifeCycle\LifeCycle\EVENT_DATASET_UPDATE
const EVENT_DATASET_UPDATE
Definition: LifeCycle.php:23
Drupal\metastore\Reference\OrphanChecker
Definition: OrphanChecker.php:11
Drupal\metastore\MetastoreItemInterface\getIdentifier
getIdentifier()
Drupal\metastore\ResourceMapper
Definition: ResourceMapper.php:15
Drupal\metastore\MetastoreItemInterface
Definition: MetastoreItemInterface.php:10
Drupal\metastore\MetastoreItemInterface\setTitle
setTitle($title)
Drupal\metastore\Reference\Referencer
Definition: Referencer.php:20
Drupal\metastore\MetastoreItemInterface\getModifiedDate
getModifiedDate()
Drupal\common\UrlHostTokenResolver
Definition: UrlHostTokenResolver.php:10
Drupal\metastore\LifeCycle\LifeCycle\EVENT_PRE_REFERENCE
const EVENT_PRE_REFERENCE
Definition: LifeCycle.php:24
Drupal\metastore\LifeCycle\LifeCycle\datasetPredelete
datasetPredelete(MetastoreItemInterface $data)
Definition: LifeCycle.php:115
Drupal\metastore\MetastoreItemInterface\isNew
isNew()
Drupal\metastore\LifeCycle\LifeCycle\distributionPredelete
distributionPredelete(MetastoreItemInterface $data)
Definition: LifeCycle.php:181
Drupal\metastore\LifeCycle\LifeCycle\datasetLoad
datasetLoad(MetastoreItemInterface $data)
Definition: LifeCycle.php:126
Drupal\metastore\LifeCycle\LifeCycle\distributionPresave
distributionPresave(MetastoreItemInterface $data)
Definition: LifeCycle.php:287
Drupal\metastore\LifeCycle\LifeCycle\$dateFormatter
$dateFormatter
Definition: LifeCycle.php:59