Exemplo n.º 1
0
 /**
  * Extract the zip archive to be imported
  *
  * @throws \RuntimeException When archive cannot be opened or extracted
  *                           or does not contain exactly one csv file
  */
 protected function extractZipArchive()
 {
     $archive = new \ZipArchive();
     $status = $archive->open($this->filePath);
     if (true !== $status) {
         throw new \RuntimeException(sprintf('Error "%d" occurred while opening the zip archive.', $status));
     } else {
         $targetDir = sprintf('%s/%s_%d_%s', pathinfo($this->filePath, PATHINFO_DIRNAME), pathinfo($this->filePath, PATHINFO_FILENAME), $this->stepExecution->getId(), md5(microtime() . $this->stepExecution->getId()));
         if ($archive->extractTo($targetDir) !== true) {
             throw new \RuntimeException('Error occurred while extracting the zip archive.');
         }
         $archive->close();
         $this->extractedPath = $targetDir;
         $csvFiles = glob($targetDir . '/*.[cC][sS][vV]');
         $csvCount = count($csvFiles);
         if (1 !== $csvCount) {
             throw new \RuntimeException(sprintf('Expecting the root directory of the archive to contain exactly 1 csv file, found %d', $csvCount));
         }
         $this->filePath = current($csvFiles);
     }
 }