Open Data Catalog v2.0.0
Service.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\dkan_metastore;
4 
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 
20 class Service implements ContainerInjectionInterface {
22 
28  private $saeFactory;
29 
35  private $schemaRetriever;
36 
42  public static function create(ContainerInterface $container) {
43  return new Service(
44  $container->get('dkan_schema.schema_retriever'),
45  $container->get('dkan_metastore.sae_factory')
46  );
47  }
48 
52  public function __construct(SchemaRetriever $schemaRetriever, Sae $saeFactory) {
53  $this->schemaRetriever = $schemaRetriever;
54  $this->saeFactory = $saeFactory;
55  }
56 
63  public function setDataModifierPlugins(DataModifierManager $pluginManager) {
64  $this->pluginManager = $pluginManager;
65  $this->plugins = $this->discover();
66  }
67 
71  public function getSchemas() {
72  $schemas = [];
73  foreach ($this->schemaRetriever->getAllIds() as $id) {
74  $schema = $this->schemaRetriever->retrieve($id);
75  $schemas[$id] = json_decode($schema);
76  }
77  return $schemas;
78  }
79 
83  public function getSchema($identifier) {
84  $schema = $this->schemaRetriever->retrieve($identifier);
85  $schema = json_decode($schema);
86 
87  return $schema;
88  }
89 
99  public function getAll($schema_id): array {
100 
101  $datasets = $this->getEngine($schema_id)->get();
102 
103  // $datasets is an array of JSON encoded string. Needs to be unflattened.
104  $unflattened = array_map(
105  function ($json_string) use ($schema_id) {
106  if (!empty($this->plugins)) {
107  $json_string = $this->modifyData($schema_id, $json_string);
108  }
109  return json_decode($json_string);
110  },
111  $datasets
112  );
113 
114  return $unflattened;
115  }
116 
128  public function get($schema_id, $identifier): string {
129  $data = $this->getEngine($schema_id)
130  ->get($identifier);
131  if (!empty($this->plugins)) {
132  $data = $this->modifyData($schema_id, $data);
133  }
134  return $data;
135  }
136 
148  private function modifyData(string $schema_id, string $data) {
149  $dataObj = json_decode($data);
150 
151  foreach ($this->plugins as $plugin) {
152  if ($plugin->requiresModification($schema_id, $dataObj)) {
153  $dataObj = $plugin->modify($schema_id, $dataObj);
154  }
155  }
156 
157  return json_encode($dataObj);
158  }
159 
171  public function getResources($schema_id, $identifier): array {
172 
173  // Load this dataset's metadata with both data and identifiers.
174  if (function_exists('drupal_static')) {
175  drupal_static('dkan_data_dereference_method', Dereferencer::DEREFERENCE_OUTPUT_REFERENCE_IDS);
176  }
177 
178  $json = $this->getEngine($schema_id)
179  ->get($identifier);
180  $data = json_decode($json);
181  /* @todo decouple from POD. */
182  $resources = $data->distribution;
183 
184  return $resources;
185  }
186 
198  public function post($schema_id, string $data): string {
199  // If resource already exists, return HTTP 409 Conflict and existing uri.
200  $decoded = json_decode($data, TRUE);
201  if (isset($decoded['identifier'])) {
202  $identifier = $decoded['identifier'];
203  if ($this->objectExists($schema_id, $identifier)) {
204  throw new ExistingObjectException("{$schema_id}/{$identifier} already exists.");
205  }
206  }
207 
208  return $this->getEngine($schema_id)->post($data);
209  }
210 
224  public function put($schema_id, $identifier, string $data): array {
225  $obj = json_decode($data);
226  if (isset($obj->identifier) && $obj->identifier != $identifier) {
227  throw new CannotChangeUuidException("Identifier cannot be modified");
228  }
229  elseif ($this->objectExists($schema_id, $identifier) && $this->objectIsEquivalent($schema_id, $identifier, $data)) {
230  throw new UnmodifiedObjectException("No changes to {$schema_id} with identifier {$identifier}.");
231  }
232  else {
233  return $this->proceedWithPut($schema_id, $identifier, $data);
234  }
235  }
236 
250  private function proceedWithPut($schema_id, $identifier, string $data): array {
251  if ($this->objectExists($schema_id, $identifier)) {
252  $this->getEngine($schema_id)->put($identifier, $data);
253  return ['identifier' => $identifier, 'new' => FALSE];
254  }
255  else {
256  $this->getEngine($schema_id)->post($data);
257  return ['identifier' => $identifier, 'new' => TRUE];
258  }
259  }
260 
274  public function patch($schema_id, $identifier, $data) {
275  $engine = $this->getEngine($schema_id);
276  if ($this->objectExists($schema_id, $identifier)) {
277  $engine->patch($identifier, $data);
278  return $identifier;
279  }
280 
281  throw new MissingObjectException("No data with the identifier {$identifier} was found.");
282  }
283 
295  public function delete($schema_id, $identifier) {
296  $engine = $this->getEngine($schema_id);
297 
298  $engine->delete($identifier);
299 
300  return $identifier;
301  }
302 
309  public function getCatalog() {
310  $catalog = $this->getSchema('catalog');
311  $catalog->dataset = $this->getAll('dataset');
312 
313  return $catalog;
314  }
315 
319  private function objectExists($schemaId, $identifier) {
320  try {
321  $this->getEngine($schemaId)->get($identifier);
322  return TRUE;
323  }
324  catch (\Exception $e) {
325  return FALSE;
326  }
327  }
328 
346  private function objectIsEquivalent(string $schema_id, string $identifier, string $new_metadata) {
347  $existing_metadata = $this->getEngine($schema_id)->get($identifier);
348  return json_decode($new_metadata) == json_decode($existing_metadata);
349  }
350 
354  private function getEngine($schemaId) {
355  return $this->saeFactory->getInstance($schemaId);
356  }
357 
358 }
Drupal\dkan_metastore\Service\setDataModifierPlugins
setDataModifierPlugins(DataModifierManager $pluginManager)
Definition: Service.php:63
Drupal\dkan_metastore\Exception\ExistingObjectException
Definition: ExistingObjectException.php:10
Drupal\dkan_metastore
Drupal\dkan_metastore\Service\__construct
__construct(SchemaRetriever $schemaRetriever, Sae $saeFactory)
Definition: Service.php:52
Drupal\dkan_metastore\Service\put
put($schema_id, $identifier, string $data)
Definition: Service.php:224
Drupal\dkan_data\Reference\Dereferencer\DEREFERENCE_OUTPUT_REFERENCE_IDS
const DEREFERENCE_OUTPUT_REFERENCE_IDS
Definition: Dereferencer.php:29
Drupal\dkan_metastore\Factory\Sae
Definition: Sae.php:13
Drupal\dkan_metastore\Service\getResources
getResources($schema_id, $identifier)
Definition: Service.php:171
Drupal\dkan_metastore\Exception\CannotChangeUuidException
Definition: CannotChangeUuidException.php:10
Drupal\dkan_common\DataModifierPluginTrait
trait DataModifierPluginTrait
Definition: DataModifierPluginTrait.php:8
Drupal\dkan_metastore\Service\getSchema
getSchema($identifier)
Definition: Service.php:83
Drupal\dkan_metastore\Service\getCatalog
getCatalog()
Definition: Service.php:309
Drupal\dkan_metastore\Service\create
static create(ContainerInterface $container)
Definition: Service.php:42
Drupal\dkan_metastore\Exception\UnmodifiedObjectException
Definition: UnmodifiedObjectException.php:10
Drupal\dkan_schema\SchemaRetriever
Definition: SchemaRetriever.php:11
Drupal\dkan_metastore\Service\getSchemas
getSchemas()
Definition: Service.php:71
Drupal\dkan_metastore\Service\post
post($schema_id, string $data)
Definition: Service.php:198
Drupal\dkan_common\Plugin\DataModifierManager
Definition: DataModifierManager.php:12
Drupal\dkan_metastore\Service\getAll
getAll($schema_id)
Definition: Service.php:99
Drupal\dkan_metastore\Service
Definition: Service.php:20
Drupal\dkan_metastore\Service\patch
patch($schema_id, $identifier, $data)
Definition: Service.php:274
Drupal\dkan_data\Reference\Dereferencer
Definition: Dereferencer.php:13
Drupal\dkan_metastore\Exception\MissingObjectException
Definition: MissingObjectException.php:10