Open Data Catalog v2.0.0
Resource.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use Procrastinator\Result;
7 use FileFetcher\FileFetcher;
8 use Dkan\Datastore\Resource as R;
9 use Drupal\Core\Entity\EntityRepository;
10 use Drupal\Core\File\FileSystem;
11 use Drupal\node\NodeInterface;
12 
16 class Resource {
17  const DEFAULT_TIMELIMIT = 50;
18 
19  private $uuid;
20  private $entityRepository;
21  private $fileSystem;
22  private $jobStoreFactory;
23 
27  public function __construct(
28  string $uuid,
29  EntityRepository $entityRepository,
30  FileSystem $fileSystem,
31  JobStoreFactory $jobStoreFactory
32  ) {
33  $this->uuid = $uuid;
34  $this->entityRepository = $entityRepository;
35  $this->fileSystem = $fileSystem;
36  $this->jobStoreFactory = $jobStoreFactory;
37  }
38 
50  public function get($useFileFetcher = FALSE, $runFileFetcher = TRUE): ?R {
51  $node = $this->entityRepository->loadEntityByUuid('node', $this->uuid);
52  if (!$node) {
53  return NULL;
54  }
55 
56  if ($useFileFetcher == TRUE) {
57  return $this->getResourceFromFileFetcher($node, $runFileFetcher);
58  }
59  else {
60  return new R($node->id(), $this->getResourceFilePathFromNode($node), $this->getMimeType($node));
61  }
62  }
63 
72  public function remove() {
73  $fileFetcher = $this->getFileFetcher();
74  $filePath = $fileFetcher->getStateProperty('destination');
75  if (file_exists($filePath)) {
76  unlink($filePath);
77  }
78  $this->jobStoreFactory->getInstance(FileFetcher::class)->remove($this->uuid);
79  }
80 
84  public function getResult(): Result {
85  $fileFetcher = $this->getFileFetcher();
86  return isset($fileFetcher) ? $fileFetcher->getResult() : new Result();
87  }
88 
94  protected function getFileFetcherInstance($filePath, $tmpDirectory) {
95  $fileFetcher = FileFetcher::get($this->uuid, $this->jobStoreFactory->getInstance(FileFetcher::class), [
96  'filePath' => $filePath,
97  'temporaryDirectory' => $tmpDirectory,
98  ]);
99  $fileFetcher->setTimeLimit(self::DEFAULT_TIMELIMIT);
100  return $fileFetcher;
101  }
102 
106  public function getFileFetcher(): FileFetcher {
107 
108  $node = $this->entityRepository->loadEntityByUuid('node', $this->uuid);
109 
110  if (!$node) {
111  throw new \Exception("No node found for uuid {$this->uuid}");
112  }
113 
114  $filePath = $this->getResourceFilePathFromNode($node);
115 
116  $tmpDirectory = $this->fileSystem->realpath("public://") . "/dkan-tmp";
117  $this->fileSystem->prepareDirectory($tmpDirectory, FileSystem::CREATE_DIRECTORY | FileSystem::MODIFY_PERMISSIONS);
118 
119  return $this->getFileFetcherInstance($filePath, $tmpDirectory);
120  }
121 
134  private function getResourceFilePathFromNode(NodeInterface $node): string {
135 
136  $meta = $node->get('field_json_metadata')->getString();
137 
138  if (!isset($meta)) {
139  throw new \Exception("Entity for {$node->uuid()} does not have required field `field_json_metadata`.");
140  }
141 
142  $metadata = json_decode($meta);
143 
144  if (!($metadata instanceof \stdClass)) {
145  throw new \Exception("Invalid metadata information or missing file information.");
146  }
147 
148  if (isset($metadata->data->downloadURL)) {
149  return $metadata->data->downloadURL;
150  }
151 
152  throw new \Exception("Invalid metadata information or missing file information.");
153  }
154 
158  private function getResourceFromFileFetcher($node, $runFileFetcher) {
159  $fileFetcher = $this->getFileFetcher($this->uuid);
160 
161  if ($runFileFetcher) {
162  $fileFetcher->run();
163  }
164 
165  if ($fileFetcher->getResult()->getStatus() != Result::DONE) {
166  return NULL;
167  }
168 
169  $json = $fileFetcher->getResult()->getData();
170  $fileData = json_decode($json);
171  return new R($node->id(), $fileData->destination, $this->getMimeType($node));
172  }
173 
183  private function getMimeType(NodeInterface $node) : ?string {
184  $metadata = json_decode($node->get('field_json_metadata')->getString());
185  return $metadata->data->mediaType ?? NULL;
186  }
187 
188 }
Drupal\dkan_datastore\Service\Resource\getResult
getResult()
Definition: Resource.php:84
Drupal\dkan_datastore\Service\Resource
Definition: Resource.php:16
Drupal\dkan_datastore\Service\Resource\getFileFetcherInstance
getFileFetcherInstance($filePath, $tmpDirectory)
Definition: Resource.php:94
Drupal\dkan_datastore\Service\Resource\DEFAULT_TIMELIMIT
const DEFAULT_TIMELIMIT
Definition: Resource.php:17
Drupal\dkan_datastore\Service\Resource\getFileFetcher
getFileFetcher()
Definition: Resource.php:106
Drupal\dkan_datastore\Storage\JobStoreFactory
Definition: JobStoreFactory.php:11
Drupal\dkan_datastore\Service\Resource\__construct
__construct(string $uuid, EntityRepository $entityRepository, FileSystem $fileSystem, JobStoreFactory $jobStoreFactory)
Definition: Resource.php:27
Drupal\dkan_datastore\Service