[UTIL] Fix App\Util\TemporaryFile, adding default options and preventing warning on not enough permission

This commit is contained in:
Hugo Sales 2021-05-05 12:44:07 +00:00
parent 01a659e5b3
commit fc9b934bbc
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -37,7 +37,7 @@ class TemporaryFile extends \SplFileInfo
/** /**
* @param array $options - ['prefix' => ?string, 'suffix' => ?string, 'mode' => ?string, 'directory' => ?string] * @param array $options - ['prefix' => ?string, 'suffix' => ?string, 'mode' => ?string, 'directory' => ?string]
*/ */
public function __construct(array $options) public function __construct(array $options = [])
{ {
$attempts = 16; $attempts = 16;
for ($count = 0; $count < $attempts; ++$count) { for ($count = 0; $count < $attempts; ++$count) {
@ -49,8 +49,10 @@ class TemporaryFile extends \SplFileInfo
} }
} }
if ($count == $attempts) { if ($count == $attempts) {
// @codeCoverageIgnoreStart
$this->cleanup(); $this->cleanup();
throw new TemporaryFileException('Could not open file: ' . $filename); throw new TemporaryFileException('Could not open file: ' . $filename);
// @codeCoverageIgnoreEnd
} }
parent::__construct($filename); parent::__construct($filename);
@ -67,7 +69,9 @@ class TemporaryFile extends \SplFileInfo
if (!is_null($this->resource)) { if (!is_null($this->resource)) {
return fwrite($this->resource, $data); return fwrite($this->resource, $data);
} else { } else {
// @codeCoverageIgnoreStart
return null; return null;
// @codeCoverageIgnoreEnd
} }
} }
@ -134,7 +138,7 @@ class TemporaryFile extends \SplFileInfo
// Memorise if the file was there and see if there is access // Memorise if the file was there and see if there is access
$exists = file_exists($destpath); $exists = file_exists($destpath);
if (!touch($destpath)) { if (!@touch($destpath)) {
throw new TemporaryFileException( throw new TemporaryFileException(
'Insufficient permissions for destination: "' . $destpath . '"' 'Insufficient permissions for destination: "' . $destpath . '"'
); );
@ -143,7 +147,9 @@ class TemporaryFile extends \SplFileInfo
unlink($destpath); unlink($destpath);
} }
if (!$this->close()) { if (!$this->close()) {
// @codeCoverageIgnoreStart
throw new TemporaryFileException('Could not close the resource'); throw new TemporaryFileException('Could not close the resource');
// @codeCoverageIgnoreEnd
} }
rename($temppath, $destpath); rename($temppath, $destpath);