Open Data Catalog v2.0.0
MetastoreController.php
Go to the documentation of this file.
1 <?php
2 
4 
10 use Symfony\Component\HttpFoundation\JsonResponse;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
17 
23 class MetastoreController implements ContainerInjectionInterface {
25 
31  private $requestStack;
32 
38  private $service;
39 
45  private $docs;
46 
52  public static function create(ContainerInterface $container) {
53  return new static(
54  $container->get('dkan.metastore.api_response'),
55  $container->get('dkan.metastore.service'),
56  $container->get('dkan.metastore.dataset_api_docs')
57  );
58  }
59 
63  public function __construct(MetastoreApiResponse $apiResponse, Service $service, DatasetApiDocs $docs) {
64  $this->apiResponse = $apiResponse;
65  $this->service = $service;
66  $this->docs = $docs;
67  }
68 
72  public function getSchemas() {
73  return $this->apiResponse->cachedJsonResponse($this->service->getSchemas());
74  }
75 
79  public function getSchema(string $identifier) {
80  try {
81  return $this->apiResponse->cachedJsonResponse($this->service->getSchema($identifier));
82  }
83  catch (\Exception $e) {
84  return $this->getResponseFromException($e, 404);
85  }
86  }
87 
99  public function getAll(string $schema_id, Request $request) {
100  $keepRefs = $this->wantObjectWithReferences($request);
101 
102  $output = array_map(function ($object) use ($keepRefs) {
103  $modified_object = $keepRefs
104  ? $this->service->swapReferences($object)
105  : $this->service->removeReferences($object);
106  return (object) $modified_object->get('$');
107  }, $this->service->getAll($schema_id));
108 
109  $output = array_values($output);
110  return $this->apiResponse->cachedJsonResponse($output, 200, [$schema_id], $request->query);
111  }
112 
126  public function get(string $schema_id, string $identifier, Request $request) {
127  try {
128  $object = $this->service->get($schema_id, $identifier);
129  if ($this->wantObjectWithReferences($request)) {
130  $object = $this->service->swapReferences($object);
131  }
132  else {
133  $object = Service::removeReferences($object);
134  }
135  $object = (object) $object->get('$');
136  return $this->apiResponse->cachedJsonResponse($object, 200, [$schema_id => [$identifier]], $request->query);
137  }
138  catch (\Exception $e) {
139  return $this->getResponseFromException($e, 404);
140  }
141  }
142 
152  private function wantObjectWithReferences(Request $request) {
153  $param = $request->get('show-reference-ids', FALSE);
154  $param2 = $request->get('show_reference_ids', FALSE);
155  if ($param === FALSE && $param2 === FALSE) {
156  return FALSE;
157  }
158  return TRUE;
159  }
160 
172  public function getResources(string $schema_id, string $identifier) {
173  try {
174  return $this->apiResponse->cachedJsonResponse($this->service->getResources($schema_id, $identifier));
175  }
176  catch (\Exception $e) {
177  return $this->getResponseFromException($e, 404);
178  }
179  }
180 
192  public function post(string $schema_id, Request $request) {
193  try {
194  $data = $request->getContent();
195  $this->checkIdentifier($data);
196  $data = $this->service->getValidMetadataFactory()->get($data, $schema_id, ['method' => 'POST']);
197  $identifier = $this->service->post($schema_id, $data);
198  return $this->apiResponse->cachedJsonResponse([
199  "endpoint" => "{$request->getRequestUri()}/{$identifier}",
200  "identifier" => $identifier,
201  ], 201);
202  }
203  catch (MetastoreException $e) {
204  return $this->getResponseFromException($e, $e->httpCode());
205  }
206  catch (\Exception $e) {
207  return $this->getResponseFromException($e, 400);
208  }
209  }
210 
224  public function publish(string $schema_id, string $identifier, Request $request) {
225  try {
226  $this->service->publish($schema_id, $identifier);
227  return $this->apiResponse->cachedJsonResponse((object) [
228  "endpoint" => "{$request->getRequestUri()}/publish",
229  "identifier" => $identifier,
230  ]);
231  }
232  catch (MetastoreException $e) {
233  return $this->getResponseFromException($e, $e->httpCode());
234  }
235  catch (\Exception $e) {
236  return $this->getResponseFromException($e, 400);
237  }
238  }
239 
253  public function put($schema_id, string $identifier, Request $request) {
254  try {
255  $data = $request->getContent();
256  $this->checkIdentifier($data, $identifier);
257  $data = $this->service->getValidMetadataFactory()->get($data, $schema_id);
258  $info = $this->service->put($schema_id, $identifier, $data);
259  $code = ($info['new'] == TRUE) ? 201 : 200;
260  return $this->apiResponse->cachedJsonResponse(
261  [
262  "endpoint" => $request->getRequestUri(),
263  "identifier" => $info['identifier'],
264  ],
265  $code
266  );
267  }
268  catch (MetastoreException $e) {
269  return $this->getResponseFromException($e, $e->httpCode());
270  }
271  catch (\Exception $e) {
272  return $this->getResponseFromException($e, 400);
273  }
274  }
275 
289  public function patch($schema_id, $identifier, Request $request) {
290 
291  try {
292  $data = $request->getContent();
293 
294  if (empty($data)) {
295  throw new MissingPayloadException("Empty body");
296  }
297  $obj = json_decode($data);
298  if (!$obj) {
299  throw new InvalidJsonException("Invalid JSON");
300  }
301  $this->checkIdentifier($data, $identifier);
302 
303  $this->service->patch($schema_id, $identifier, $data);
304  return $this->apiResponse->cachedJsonResponse((object) [
305  "endpoint" => $request->getRequestUri(),
306  "identifier" => $identifier,
307  ]);
308  }
309  catch (MetastoreException $e) {
310  return $this->getResponseFromException($e, $e->httpCode());
311  }
312  catch (\Exception $e) {
313  return $this->getResponseFromException($e, 400);
314  }
315  }
316 
328  public function delete($schema_id, $identifier) {
329  try {
330  $this->service->delete($schema_id, $identifier);
331  return $this->apiResponse->cachedJsonResponse((object) ["message" => "Dataset {$identifier} has been deleted."]);
332  }
333  catch (\Exception $e) {
334  return $this->getResponseFromException($e);
335  }
336  }
337 
344  public function getCatalog() : JsonResponse {
345  try {
346  return $this->apiResponse->cachedJsonResponse($this->service->getCatalog());
347  }
348  catch (\Exception $e) {
349  return $this->getResponseFromException($e);
350  }
351  }
352 
364  public function getDocs($identifier, Request $request) : JsonResponse {
365  try {
366  return $this->apiResponse->cachedJsonResponse(
367  $this->docs->getDatasetSpecific($identifier),
368  200,
369  ['dataset' => [$identifier]],
370  $request->query
371  );
372  }
373  catch (\Exception $e) {
374  return $this->getResponseFromException($e);
375  }
376  }
377 
381  private function checkIdentifier(string $data, $identifier = NULL) {
382  $obj = json_decode($data);
383  if (isset($identifier) && isset($obj->identifier) && $obj->identifier != $identifier) {
384  throw new CannotChangeUuidException("Identifier cannot be modified");
385  }
386  }
387 
388 }
Drupal\metastore\Controller\MetastoreController\__construct
__construct(MetastoreApiResponse $apiResponse, Service $service, DatasetApiDocs $docs)
Definition: MetastoreController.php:63
Drupal\metastore\Controller\MetastoreController\getSchemas
getSchemas()
Definition: MetastoreController.php:72
Drupal\metastore\Controller\MetastoreController\put
put($schema_id, string $identifier, Request $request)
Definition: MetastoreController.php:253
Drupal\metastore\Controller\MetastoreController\post
post(string $schema_id, Request $request)
Definition: MetastoreController.php:192
Drupal\metastore\Service
Definition: Service.php:21
Drupal\metastore\Service
Definition: Uuid5.php:5
Drupal\metastore\Controller\MetastoreController\patch
patch($schema_id, $identifier, Request $request)
Definition: MetastoreController.php:289
Drupal\metastore\Service\removeReferences
static removeReferences(RootedJsonData $object, $prefix="%")
Definition: Service.php:512
Drupal\metastore\MetastoreApiResponse
Definition: MetastoreApiResponse.php:13
Drupal\metastore\Controller\MetastoreController\getDocs
getDocs($identifier, Request $request)
Definition: MetastoreController.php:364
Drupal\metastore\Exception\CannotChangeUuidException
Definition: CannotChangeUuidException.php:10
Drupal\common\JsonResponseTrait
trait JsonResponseTrait
Definition: JsonResponseTrait.php:15
Drupal\metastore\Controller\MetastoreController\publish
publish(string $schema_id, string $identifier, Request $request)
Definition: MetastoreController.php:224
Drupal\metastore\Controller\MetastoreController
Definition: MetastoreController.php:23
Drupal\metastore\Exception\MetastoreException\httpCode
httpCode()
Drupal\metastore\Exception\MissingPayloadException
Definition: MissingPayloadException.php:10
Drupal\metastore\Controller\MetastoreController\getResources
getResources(string $schema_id, string $identifier)
Definition: MetastoreController.php:172
Drupal\metastore\Controller
Definition: MetastoreController.php:3
Drupal\metastore\Controller\MetastoreController\getCatalog
getCatalog()
Definition: MetastoreController.php:344
Drupal\common\getResponseFromException
getResponseFromException(\Exception $e, int $code=400)
Definition: JsonResponseTrait.php:37
Drupal\metastore\Exception\InvalidJsonException
Definition: InvalidJsonException.php:10
Drupal\metastore\Controller\MetastoreController\create
static create(ContainerInterface $container)
Definition: MetastoreController.php:52
Drupal\metastore\Controller\MetastoreController\getSchema
getSchema(string $identifier)
Definition: MetastoreController.php:79
Drupal\metastore\Controller\MetastoreController\getAll
getAll(string $schema_id, Request $request)
Definition: MetastoreController.php:99
Drupal\metastore\DatasetApiDocs
Definition: DatasetApiDocs.php:10
Drupal\metastore\Exception\MetastoreException
Definition: MetastoreException.php:10