Open Data Catalog v2.0.0
Data.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Contracts\BulkRetrieverInterface;
6 use Contracts\RemoverInterface;
7 use Contracts\RetrieverInterface;
8 use Contracts\StorerInterface;
9 use DateTime;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use HTMLPurifier;
12 
16 class Data implements StorerInterface, RetrieverInterface, BulkRetrieverInterface, RemoverInterface {
17 
23  private $entityTypeManager;
24 
30  private $schemaId;
31 
38  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
39  $this->entityTypeManager = $entityTypeManager;
40  }
41 
48  public function setSchema($schema_id) {
49  $this->schemaId = $schema_id;
50  }
51 
57  public function retrieveAll(): array {
58 
59  if (!isset($this->schemaId)) {
60  throw new \Exception("Data schemaId not set in retrieveAll().");
61  }
62 
63  $nodeStorage = $this->getNodeStorage();
64 
65  $node_ids = $nodeStorage->getQuery()
66  ->condition('type', $this->getType())
67  ->condition('field_data_type', $this->schemaId)
68  ->execute();
69 
70  $all = [];
71  foreach ($node_ids as $nid) {
72  /* @var $node \Drupal\node\NodeInterface */
73  $node = $nodeStorage->load($nid);
74  $fieldList = $node->get('field_json_metadata');
75  $field = $fieldList->get(0);
76  $data = $field->getValue();
77  $all[] = $data['value'];
78  }
79  return $all;
80  }
81 
87  public function retrieve(string $id): ?string {
88 
89  if (!isset($this->schemaId)) {
90  throw new \Exception("Data schemaId not set in retrieve().");
91  }
92 
93  if (FALSE !== ($node = $this->getNodeByUuid($id))) {
94  $value = $node->get('field_json_metadata')->get(0)->getValue();
95  return $value['value'];
96  }
97 
98  throw new \Exception("No data with the identifier {$id} was found.");
99  }
100 
106  public function remove(string $id) {
107 
108  if (FALSE !== ($node = $this->getNodeByUuid($id))) {
109  return $node->delete();
110  }
111  }
112 
118  public function store($data, string $id = NULL): string {
119 
120  if (!isset($this->schemaId)) {
121  throw new \Exception("Data schemaId not set in store().");
122  }
123 
124  $data = json_decode($data);
125  $data = $this->filterHtml($data);
126 
127  $id = (!$id && isset($data->identifier)) ? $data->identifier : $id;
128 
129  if ($id) {
130  $node = $this->getNodeByUuid($id);
131  }
132 
133  /* @var $node \Drupal\node\NodeInterface */
134  if ($node) {
135  return $this->updateExistingNode($node, $data);
136  }
137  // Create new node.
138  else {
139  return $this->createNewNode($id, $data);
140  }
141  }
142 
146  private function updateExistingNode($node, $data) {
147  $node->field_data_type = $this->schemaId;
148  $new_data = json_encode($data);
149  $node->field_json_metadata = $new_data;
150  // Create a new revision.
151  $node->setNewRevision(TRUE);
152  $node->isDefaultRevision(TRUE);
153  $node->setRevisionLogMessage("Updated on " . $this->formattedTimestamp());
154 
155  $node->save();
156  return $node->uuid();
157  }
158 
162  private function createNewNode($id, $data) {
163  $title = isset($data->title) ? $data->title : $data->name;
164  $node = $this->getNodeStorage()
165  ->create(
166  [
167  'title' => $title,
168  'type' => 'data',
169  'uuid' => $id,
170  'field_data_type' => $this->schemaId,
171  'field_json_metadata' => json_encode($data),
172  ]
173  );
174  $node->setRevisionLogMessage("Created on " . $this->formattedTimestamp());
175 
176  $node->save();
177  return $node->uuid();
178  }
179 
186  private function getNodeStorage() {
187  return $this->entityTypeManager
188  ->getStorage('node');
189  }
190 
197  private function getType() {
198  return 'data';
199  }
200 
207  private function getNodeByUuid($uuid) {
208 
209  $nodes = $this->getNodeStorage()->loadByProperties(
210  [
211  'type' => $this->getType(),
212  'uuid' => $uuid,
213  ]
214  );
215  // Uuid should be universally unique and always return
216  // a single node.
217  return current($nodes);
218  }
219 
229  private function filterHtml($input) {
230  switch (gettype($input)) {
231  case "string":
232  return $this->htmlPurifier($input);
233 
234  case "array":
235  case "object":
236  foreach ($input as &$value) {
237  $value = $this->filterHtml($value);
238  }
239  return $input;
240 
241  default:
242  // Leave integers, floats or boolean unchanged.
243  return $input;
244  }
245  }
246 
260  private function htmlPurifier(string $input) {
261  $filter = new HTMLPurifier();
262  return $filter->purify($input);
263  }
264 
271  private function formattedTimestamp() : string {
272  $now = new \DateTime('now');
273  return $now->format(DateTime::ATOM);
274  }
275 
276 }
Drupal\dkan_data\Storage\Data\retrieve
retrieve(string $id)
Definition: Data.php:87
Drupal\dkan_data\Storage\Data
Definition: Data.php:16
Drupal\dkan_data\Storage\Data\setSchema
setSchema($schema_id)
Definition: Data.php:48
Drupal\dkan_data\Storage\Data\__construct
__construct(EntityTypeManagerInterface $entityTypeManager)
Definition: Data.php:38
Drupal\dkan_data\Storage\Data\retrieveAll
retrieveAll()
Definition: Data.php:57
Drupal\dkan_data\Storage\Data\store
store($data, string $id=NULL)
Definition: Data.php:118
Drupal\dkan_data\Storage
Definition: Data.php:3