Open Data Catalog v2.0.0
DataNodeLifeCycle.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
12  use LoggerTrait;
13 
19  public function insert() {
20  $entity = $this->node;
21  if ($this->getDataType() != 'distribution') {
22  return;
23  }
24 
25  if ($this->isDataStorable()) {
26  try {
27  /* @var $datastoreService \Drupal\dkan_datastore\Service */
28  $datastoreService = \Drupal::service('dkan_datastore.service');
29  $datastoreService->import($entity->uuid(), TRUE);
30  }
31  catch (\Exception $e) {
32  $this->setLoggerFactory(\Drupal::service('logger.factory'));
33  $this->log('dkan_datastore', $e->getMessage());
34  }
35  }
36 
37  }
38 
45  public function predelete() {
46  $entity = $this->node;
47  if ($this->getDataType() != 'distribution') {
48  return;
49  }
50 
51  try {
52  /* @var $datastoreService \Drupal\dkan_datastore\Service */
53  $datastoreService = \Drupal::service('dkan_datastore.service');
54  $datastoreService->drop($entity->uuid());
55  }
56  catch (\Exception $e) {
57  $this->setLoggerFactory(\Drupal::service('logger.factory'));
58  $this->log('dkan_datastore', $e->getMessage());
59  }
60 
61  $metadata = $this->getMetaData();
62  $data = $metadata->data;
63  if (isset($data->downloadURL)) {
64  $url = $data->downloadURL;
65  $pieces = explode('sites/default/files/', $url);
66  $path = "public://" . end($pieces);
68  $fileSystemService = \Drupal::service('file_system');
69  $fileSystemService->delete($path);
70  }
71  }
72 
76  private function isDataStorable() : bool {
77  $metadata = $this->getMetaData();
78  $data = $metadata->data;
79 
80  if (isset($data->downloadURL) && isset($data->mediaType)) {
81  return in_array($data->mediaType, [
82  'text/csv',
83  'text/tab-separated-values',
84  ]);
85  }
86 
87  return FALSE;
88  }
89 
90 }
Drupal\dkan_datastore\DataNodeLifeCycle
Definition: DataNodeLifeCycle.php:11
Drupal\dkan_datastore
Definition: DataNodeLifeCycle.php:3
Drupal\dkan_common\setLoggerFactory
setLoggerFactory(LoggerChannelFactory $loggerService)
Definition: LoggerTrait.php:25
Drupal\dkan_common\AbstractDataNodeLifeCycle\$node
$node
Definition: AbstractDataNodeLifeCycle.php:13
Drupal\dkan_common\AbstractDataNodeLifeCycle\getDataType
getDataType()
Definition: AbstractDataNodeLifeCycle.php:26
Drupal\dkan_common\AbstractDataNodeLifeCycle\getMetaData
getMetaData()
Definition: AbstractDataNodeLifeCycle.php:40
Drupal\dkan_common\AbstractDataNodeLifeCycle
Definition: AbstractDataNodeLifeCycle.php:12
Drupal\dkan_datastore\DataNodeLifeCycle\insert
insert()
Definition: DataNodeLifeCycle.php:19
Drupal\dkan_common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11