Skip to content

Commit

Permalink
Merge pull request #7 from keboola/remove-unused-method
Browse files Browse the repository at this point in the history
fixes #5
  • Loading branch information
ondrejhlavacek authored Nov 13, 2017
2 parents 061008a + dd96e95 commit 2e3c2fc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
36 changes: 6 additions & 30 deletions src/Keboola/Temp/Temp.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class Temp
*/
protected $files = array();

/**
* @var Bool
*/
protected $preserveRunFolder = false;

/**
*
* If temp folder needs to be deterministic, you can use ID as the last part of folder name
Expand All @@ -50,14 +45,6 @@ public function initRunFolder()
}
}

/**
* @param bool $value
*/
public function setPreserveRunFolder($value)
{
$this->preserveRunFolder = $value;
}

/**
* Get path to temp directory
*
Expand Down Expand Up @@ -87,30 +74,28 @@ public function getTmpFolder()
* Create empty file in TMP directory
*
* @param string $suffix filename suffix
* @param bool $preserve
* @throws \Exception
* @return \SplFileInfo
*/
public function createTmpFile($suffix = null, $preserve = false)
public function createTmpFile($suffix = null)
{
$file = uniqid();

if ($suffix) {
$file .= '-' . $suffix;
}

return $this->createFile($file, $preserve);
return $this->createFile($file);
}

/**
* Creates named temporary file
*
* @param $fileName
* @param bool $preserve
* @return \SplFileInfo
* @throws \Exception
*/
public function createFile($fileName, $preserve = false)
public function createFile($fileName)
{
$this->initRunFolder();

Expand All @@ -124,8 +109,7 @@ public function createFile($fileName, $preserve = false)

$this->filesystem->touch($pathName);
$this->files[] = array(
'file' => $fileInfo,
'preserve' => $preserve
'file' => $fileInfo
);
$this->filesystem->chmod($pathName, 0600);

Expand All @@ -146,21 +130,13 @@ public function setId($id) {
*/
function __destruct()
{
$preserveRunFolder = $this->preserveRunFolder;

try {
foreach ($this->files as $file) {
if ($file['preserve']) {
$preserveRunFolder = true;
}
if (file_exists($file['file']) && is_file($file['file']) && !$file['preserve']) {
if (file_exists($file['file']) && is_file($file['file'])) {
$this->filesystem->remove($file['file']->getPathname());
}
}

if (!$preserveRunFolder && is_dir($this->getTmpPath())) {
$this->filesystem->remove($this->getTmpPath());
}
$this->filesystem->remove($this->getTmpPath());
} catch (\Exception $e) {
// Graceful destructor, does not throw any errors.
// Fixes issues when deleting files on a server that is just shutting down.
Expand Down
11 changes: 0 additions & 11 deletions tests/Keboola/Temp/TempTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,4 @@ public function testCleanupForeignFile()
unset($temp);
self::assertFileNotExists($dir);
}

public function testCleanupFilePreserve()
{
$temp = new Temp();
$file = $temp->createFile('file', true);

unset($temp);
self::assertFileExists($file->getPathname());
unlink($file->getPathname());
rmdir(dirname($file->getPathname()));
}
}

0 comments on commit 2e3c2fc

Please sign in to comment.