Open Data Catalog v2.0.0
Service.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\dkan_datastore;
4 
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Queue\QueueFactory;
12 use Dkan\Datastore\Importer;
13 
17 class Service implements ContainerInjectionInterface {
18 
19  private $resourceServiceFactory;
20  private $importServiceFactory;
21  private $queue;
22  private $jobStoreFactory;
23 
29  public static function create(ContainerInterface $container) {
30  return new Service(
31  $container->get('dkan_datastore.service.factory.resource'),
32  $container->get('dkan_datastore.service.factory.import'),
33  $container->get('queue'),
34  $container->get('dkan_datastore.job_store_factory')
35  );
36  }
37 
41  public function __construct(Resource $resourceServiceFactory, Import $importServiceFactory, QueueFactory $queue, JobStoreFactory $jobStoreFactory) {
42  $this->queue = $queue->get('dkan_datastore_import');
43  $this->resourceServiceFactory = $resourceServiceFactory;
44  $this->importServiceFactory = $importServiceFactory;
45  $this->jobStoreFactory = $jobStoreFactory;
46  }
47 
56  public function import(string $uuid, bool $deferred = FALSE): array {
57 
58  $resourceService = $this->resourceServiceFactory->getInstance($uuid);
59 
60  // If we passed $deferred, immediately add to the queue for later.
61  if (!empty($deferred)) {
62  // This creates a filefetcher job.
63  $resourceService->get(TRUE, FALSE);
64  $queueId = $this->queueImport($uuid);
65  return [
66  'message' => "Resource {$uuid} has been queued to be imported.",
67  'queue_id' => $queueId,
68  ];
69  }
70 
71  /* @var $resource \Dkan\Datastore\Resource */
72  $resource = $resourceService->get(TRUE);
73  if (!$resource) {
74  $name = substr(strrchr(get_class($resourceService), "\\"), 1);
75  return [$name => $resourceService->getResult()];
76  }
77 
78  $importService = $this->importServiceFactory->getInstance($resource->getId(), ['resource' => $resource]);
79  $importService->import();
80 
81  $rname = substr(strrchr(get_class($resourceService), "\\"), 1);
82  $iname = substr(strrchr(get_class($importService), "\\"), 1);
83 
84  return [
85  $rname => $resourceService->getResult(),
86  $iname => $importService->getResult(),
87  ];
88  }
89 
97  public function drop($uuid) {
98  $storage = $this->getStorage($uuid);
99  if ($storage) {
100  $storage->destroy();
101  }
102 
103  /* @var $resourceService \Drupal\dkan_datastore\Service\Resource */
104  $resourceService = $this->resourceServiceFactory->getInstance($uuid);
105  $resourceService->remove();
106 
107  /* @var $resource \Dkan\Datastore\Resource */
108  $resource = $resourceService->get();
109  $this->jobStoreFactory->getInstance(Importer::class)->remove($resource->getId());
110 
111  }
112 
122  private function queueImport($uuid) {
123  // Attempt to fetch the file in a queue so as to not block user.
124  $queueId = $this->queue->createItem(['uuid' => $uuid]);
125 
126  if ($queueId === FALSE) {
127  throw new \RuntimeException("Failed to create file fetcher queue for {$uuid}");
128  }
129 
130  return $queueId;
131  }
132 
139  public function list() {
140  return ImporterList::getList($this->jobStoreFactory, $this->resourceServiceFactory, $this->importServiceFactory);
141  }
142 
149  public function getStorage(string $uuid) {
150  $resourceService = $this->resourceServiceFactory->getInstance($uuid);
151 
152  /* @var $resource \Dkan\Datastore\Resource */
153  $resource = $resourceService->get();
154  if ($resource) {
155  $importService = $this->importServiceFactory->getInstance($resource->getId(),
156  ['resource' => $resource]);
157  return $importService->getStorage();
158  }
159  return NULL;
160  }
161 
162 }
Drupal\dkan_datastore\Service\drop
drop($uuid)
Definition: Service.php:97
Drupal\dkan_datastore\Service\Factory\Resource
Definition: Resource.php:16
Drupal\dkan_datastore
Definition: DataNodeLifeCycle.php:3
Drupal\dkan_datastore\Service\__construct
__construct(Resource $resourceServiceFactory, Import $importServiceFactory, QueueFactory $queue, JobStoreFactory $jobStoreFactory)
Definition: Service.php:41
Drupal\dkan_datastore\Storage\JobStoreFactory\getInstance
getInstance(string $identifier, array $config=[])
Definition: JobStoreFactory.php:27
Drupal\dkan_datastore\Service\Factory\Import
Definition: Import.php:15
Drupal\dkan_datastore\Service
Definition: Service.php:17
Drupal\dkan_datastore\Service\ImporterList\ImporterList\getList
static getList(JobStoreFactory $jobStoreFactory, Resource $resrouceServiceFactory, Import $importServiceFactory)
Definition: ImporterList.php:73
Drupal\dkan_datastore\Service\getStorage
getStorage(string $uuid)
Definition: Service.php:149
Drupal\dkan_datastore\Service\ImporterList\ImporterList
Definition: ImporterList.php:13
Drupal\dkan_datastore\Storage\JobStoreFactory
Definition: JobStoreFactory.php:11
Drupal\dkan_datastore\Service\list
list()
Definition: Service.php:139
Drupal\dkan_datastore\Service\create
static create(ContainerInterface $container)
Definition: Service.php:29