Open Data Catalog v2.0.0
Import.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use CsvParser\Parser\Csv;
6 use Dkan\Datastore\Importer;
9 use Procrastinator\Result;
10 use Dkan\Datastore\Resource;
12 
16 class Import {
17  const DEFAULT_TIMELIMIT = 50;
18 
19  private $resource;
20  private $jobStoreFactory;
21  private $databaseTableFactory;
22 
26  public function __construct(Resource $resource, JobStoreFactory $jobStoreFactory, DatabaseTableFactory $databaseTableFactory) {
27  $this->resource = $resource;
28  $this->jobStoreFactory = $jobStoreFactory;
29  $this->databaseTableFactory = $databaseTableFactory;
30  }
31 
35  public function import() {
36  $importer = $this->getImporter();
37  $importer->run();
38  }
39 
43  public function getResult(): Result {
44  $importer = $this->getImporter();
45  return $importer->getResult();
46  }
47 
57  public function getImporter(): Importer {
58  $delimiter = ",";
59  if ($this->resource->getMimeType() == 'text/tab-separated-values') {
60  $delimiter = "\t";
61  }
62 
63  $importer = Importer::get($this->resource->getId(),
64  $this->jobStoreFactory->getInstance(Importer::class),
65  [
66  "storage" => $this->getStorage(),
67  "parser" => $this->getNonRecordingParser($delimiter),
68  "resource" => $this->resource,
69  ]
70  );
71 
72  $importer->setTimeLimit(self::DEFAULT_TIMELIMIT);
73 
74  return $importer;
75  }
76 
90  private function getNonRecordingParser(string $delimiter) : Csv {
91  $parser = Csv::getParser($delimiter);
92  $parser->machine->stopRecording();
93  return $parser;
94  }
95 
102  public function getStorage(): DatabaseTable {
103  return $this->databaseTableFactory->getInstance($this->resource->getId(), ['resource' => $this->resource]);
104  }
105 
106 }
Drupal\dkan_datastore\Service\Import\getStorage
getStorage()
Definition: Import.php:102
Drupal\dkan_datastore\Storage\DatabaseTable
Definition: DatabaseTable.php:16
Drupal\dkan_datastore\Service\Resource
Definition: Resource.php:16
Drupal\dkan_datastore\Service\Import\getImporter
getImporter()
Definition: Import.php:57
Drupal\dkan_datastore\Storage\DatabaseTableFactory
Definition: DatabaseTableFactory.php:11
Drupal\dkan_datastore\Service\Import\getResult
getResult()
Definition: Import.php:43
Drupal\dkan_datastore\Service\Import\DEFAULT_TIMELIMIT
const DEFAULT_TIMELIMIT
Definition: Import.php:17
Drupal\dkan_datastore\Service\Import
Definition: Import.php:16
Drupal\dkan_datastore\Storage\JobStoreFactory
Definition: JobStoreFactory.php:11
Drupal\dkan_datastore\Service\Import\__construct
__construct(Resource $resource, JobStoreFactory $jobStoreFactory, DatabaseTableFactory $databaseTableFactory)
Definition: Import.php:26
Drupal\dkan_datastore\Service