Open Data Catalog v2.0.0
ImporterListItem.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Dkan\Datastore\Importer;
6 use FileFetcher\FileFetcher;
7 use Procrastinator\Job\Job;
8 
13 
19  public $fileFetcher;
20 
26  public $importer;
27 
38 
45 
52 
58  public $fileName;
59 
70 
80 
81 
88 
97  public function __construct(FileFetcher $fileFetcher, $importer = NULL) {
98  $this->fileFetcher = $fileFetcher;
99  $this->importer = $importer;
100  }
101 
110  public static function getItem(FileFetcher $fileFetcher, $importer = NULL) {
112  $item->buildItem();
113  return $item;
114  }
115 
119  private function buildItem() {
120  $this->fileName = $this->getFileName();
121  $this->fileFetcherStatus = $this->fileFetcher->getResult()->getStatus();
122  $this->fileFetcherBytes = $this->getBytesProcessed($this->fileFetcher);
123  $this->fileFetcherPercentDone = $this->getPercentDone($this->fileFetcher);
124  $this->importerStatus = 'waiting';
125  $this->importerBytes = 0;
126  $this->importerPercentDone = 0;
127 
128  if (isset($this->importer)) {
129  $this->importerStatus = $this->importer->getResult()->getStatus();
130  $this->importerBytes = $this->getBytesProcessed($this->importer);
131  $this->importerPercentDone = $this->getPercentDone($this->importer);
132  }
133  }
134 
138  private function getFileName(): string {
139  $fileLocation = $this->fileFetcher->getStateProperty('source');
140  $locationParts = explode('/', $fileLocation);
141  return end($locationParts);
142  }
143 
153  private function getPercentDone(Job $job): float {
154  $bytes = $this->getBytesProcessed($job);
155  $filesize = $this->getFileSize();
156  return ($filesize > 0) ? round($bytes / $filesize * 100) : 0;
157  }
158 
165  private function getFileSize(): int {
166  return $this->fileFetcher->getStateProperty('total_bytes');
167  }
168 
178  private function getBytesProcessed(Job $job): int {
179  $className = get_class($job);
180  switch ($className) {
181  // For Importer, avoid going above total size due to chunk multiplication.
182  case Importer::class:
183  $chunksSize = $job->getStateProperty('chunksProcessed') * Importer::BYTES_PER_CHUNK;
184  $fileSize = $this->getFileSize();
185  return ($chunksSize > $fileSize) ? $fileSize : $chunksSize;
186 
187  case FileFetcher::class:
188  return $job->getStateProperty('total_bytes_copied');
189 
190  default:
191  return 0;
192  }
193  }
194 
195 }
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\__construct
__construct(FileFetcher $fileFetcher, $importer=NULL)
Definition: ImporterListItem.php:97
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$fileName
$fileName
Definition: ImporterListItem.php:58
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$importerStatus
$importerStatus
Definition: ImporterListItem.php:69
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$fileFetcher
$fileFetcher
Definition: ImporterListItem.php:19
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$importer
$importer
Definition: ImporterListItem.php:26
Drupal\dkan_datastore\Service\ImporterList
Definition: ImporterList.php:3
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem
Definition: ImporterListItem.php:12
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$importerBytes
$importerBytes
Definition: ImporterListItem.php:79
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$fileFetcherBytes
$fileFetcherBytes
Definition: ImporterListItem.php:51
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$fileFetcherStatus
$fileFetcherStatus
Definition: ImporterListItem.php:37
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\getItem
static getItem(FileFetcher $fileFetcher, $importer=NULL)
Definition: ImporterListItem.php:110
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$fileFetcherPercentDone
$fileFetcherPercentDone
Definition: ImporterListItem.php:44
Drupal\dkan_datastore\Service\ImporterList\ImporterListItem\$importerPercentDone
$importerPercentDone
Definition: ImporterListItem.php:87