Open Data Catalog v2.0.0
ResourceLocalizer.php
Go to the documentation of this file.
1 <?php
2 
4 
10 use Contracts\FactoryInterface;
11 use Drupal\Core\File\FileSystemInterface;
15 use FileFetcher\FileFetcher;
16 use Procrastinator\Result;
17 use Drupal\common\EventDispatcherTrait;
18 
25  use LoggerTrait;
26  use EventDispatcherTrait;
27 
28  const LOCAL_FILE_PERSPECTIVE = 'local_file';
29  const LOCAL_URL_PERSPECTIVE = 'local_url';
30 
31  private $fileMapper;
32  private $fileFetcherFactory;
33  private $drupalFiles;
34  private $jobStoreFactory;
35 
39  public function __construct(ResourceMapper $fileMapper, FactoryInterface $fileFetcherFactory, DrupalFiles $drupalFiles, JobStoreFactory $jobStoreFactory) {
40  $this->fileMapper = $fileMapper;
41  $this->fileFetcherFactory = $fileFetcherFactory;
42  $this->drupalFiles = $drupalFiles;
43  $this->jobStoreFactory = $jobStoreFactory;
44  }
45 
49  public function localize($identifier, $version = NULL) {
50  $resource = $this->getResourceSource($identifier, $version);
51  if ($resource) {
52  $ff = $this->getFileFetcher($resource);
53  return $ff->run();
54  }
55  }
56 
60  public function get($identifier, $version = NULL, $perpective = self::LOCAL_FILE_PERSPECTIVE): ?Resource {
62  $resource = $this->getResourceSource($identifier, $version);
63 
64  if (!$resource) {
65  return NULL;
66  }
67 
68  $ff = $this->getFileFetcher($resource);
69 
70  if ($ff->getResult()->getStatus() != Result::DONE) {
71  return NULL;
72  }
73 
74  $this->registerNewPerspectives($resource, $ff);
75 
76  return $this->getFileMapper()->get($resource->getIdentifier(), $perpective, $resource->getVersion());
77  }
78 
82  private function registerNewPerspectives(Resource $resource, FileFetcher $fileFetcher) {
83 
84  $localFilePath = $fileFetcher->getStateProperty('destination');
85  $dir = "file://" . $this->drupalFiles->getPublicFilesDirectory();
86  $localFileDrupalUri = str_replace($dir, "public://", $localFilePath);
87  $localUrl = $this->drupalFiles->fileCreateUrl($localFileDrupalUri);
88  $localUrl = Referencer::hostify($localUrl);
89 
90  $new = $resource->createNewPerspective(self::LOCAL_FILE_PERSPECTIVE, $localFilePath);
91 
92  try {
93  $this->getFileMapper()->registerNewPerspective($new);
94  }
95  catch (AlreadyRegistered $e) {
96  }
97 
98  $localUrlPerspective = $resource->createNewPerspective(self::LOCAL_URL_PERSPECTIVE, $localUrl);
99 
100  try {
101  $this->getFileMapper()->registerNewPerspective($localUrlPerspective);
102  }
103  catch (AlreadyRegistered $e) {
104  }
105  }
106 
110  public function getResult($identifier, $version = NULL) {
111  $ff = $this->getFileFetcher($this->getResourceSource($identifier, $version));
112  return $ff->getResult();
113  }
114 
118  public function remove($identifier, $version = NULL) {
120  $resource = $this->get($identifier, $version);
121  $resource2 = $this->get($identifier, $version, self::LOCAL_URL_PERSPECTIVE);
122  if ($resource2) {
123  $this->removeLocalUrl($resource2);
124  }
125  if ($resource) {
126  $uuid = "{$resource->getIdentifier()}_{$resource->getVersion()}";
127  if (file_exists($resource->getFilePath())) {
128  \Drupal::service('file_system')->deleteRecursive("public://resources/{$uuid}");
129  }
130  $this->removeJob($uuid);
131  $this->fileMapper->remove($resource);
132  }
133  }
134 
138  private function removeLocalUrl(Resource $resource) {
139  return $this->fileMapper->remove($resource);
140  }
141 
145  private function removeJob($uuid) {
146  if ($uuid) {
147  $this->getJobStoreFactory()->getInstance(FileFetcher::class)->remove($uuid);
148  }
149  }
150 
154  private function getResourceSource($identifier, $version = NULL): ?Resource {
155  return $this->getFileMapper()->get($identifier, Resource::DEFAULT_SOURCE_PERSPECTIVE, $version);
156  }
157 
161  public function getFileFetcher(Resource $resource): FileFetcher {
162  $uuid = "{$resource->getIdentifier()}_{$resource->getVersion()}";
163  $directory = "public://resources/{$uuid}";
164  $this->getFilesystem()->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
165  $config = [
166  'filePath' => UrlHostTokenResolver::resolveFilePath($resource->getFilePath()),
167  'temporaryDirectory' => $directory,
168  ];
169  return $this->fileFetcherFactory->getInstance($uuid, $config);
170  }
171 
175  private function getFileMapper(): ResourceMapper {
176  return $this->fileMapper;
177  }
178 
182  private function getJobStoreFactory() {
183  return $this->jobStoreFactory;
184  }
185 
192  public function getFileSystem(): FileSystemInterface {
193  return $this->drupalFiles->getFileSystem();
194  }
195 
196 }
Drupal\datastore\Service\ResourceLocalizer\LOCAL_URL_PERSPECTIVE
const LOCAL_URL_PERSPECTIVE
Definition: ResourceLocalizer.php:29
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\metastore\Exception\AlreadyRegistered
Definition: AlreadyRegistered.php:10
Drupal\datastore\Service\ResourceLocalizer\getResult
getResult($identifier, $version=NULL)
Definition: ResourceLocalizer.php:110
Drupal\common\Resource\getFilePath
getFilePath()
Definition: Resource.php:160
Drupal\common\Resource
Definition: Resource.php:29
Drupal\datastore\Service\ResourceLocalizer
Definition: ResourceLocalizer.php:24
Drupal\datastore\Service\ResourceLocalizer\localize
localize($identifier, $version=NULL)
Definition: ResourceLocalizer.php:49
Drupal\common\Resource\DEFAULT_SOURCE_PERSPECTIVE
const DEFAULT_SOURCE_PERSPECTIVE
Definition: Resource.php:32
Drupal\common\Storage\JobStoreFactory
Definition: JobStoreFactory.php:11
Drupal\datastore\Service
Definition: DatastoreQuery.php:3
Drupal\datastore\Service\ResourceLocalizer\LOCAL_FILE_PERSPECTIVE
const LOCAL_FILE_PERSPECTIVE
Definition: ResourceLocalizer.php:28
Drupal\metastore\ResourceMapper
Definition: ResourceMapper.php:15
Drupal\datastore\Service\ResourceLocalizer\__construct
__construct(ResourceMapper $fileMapper, FactoryInterface $fileFetcherFactory, DrupalFiles $drupalFiles, JobStoreFactory $jobStoreFactory)
Definition: ResourceLocalizer.php:39
Drupal\metastore\Reference\Referencer
Definition: Referencer.php:20
Drupal\common\UrlHostTokenResolver
Definition: UrlHostTokenResolver.php:10
Drupal\datastore\Service\ResourceLocalizer\getFileFetcher
getFileFetcher(Resource $resource)
Definition: ResourceLocalizer.php:161
Drupal\common\Util\DrupalFiles
Definition: DrupalFiles.php:19
Drupal\datastore\Service\ResourceLocalizer\getFileSystem
getFileSystem()
Definition: ResourceLocalizer.php:192