39 lines
974 B
PHP
39 lines
974 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Infrastructure\External\Api;
|
|
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Враппер бинарных фалов, при создании класса содает новый временный файл,
|
|
* при необходимости сохраняет файл под новым именем
|
|
*/
|
|
class BinaryStringFileResult
|
|
{
|
|
public string $tempFileName;
|
|
|
|
public function __construct(
|
|
string $content,
|
|
) {
|
|
$this->saveToTempFile($content);
|
|
}
|
|
|
|
private function saveToTempFile(string $content): void
|
|
{
|
|
$this->tempFileName = sprintf('%s/%s_%s', sys_get_temp_dir(), 'Document', time());
|
|
file_put_contents($this->tempFileName, $content);
|
|
}
|
|
|
|
public function remove(): bool
|
|
{
|
|
if (file_exists($this->tempFileName)) {
|
|
unlink($this->tempFileName);
|
|
return true;
|
|
}
|
|
|
|
throw new RuntimeException('Temp file not found');
|
|
}
|
|
}
|