| 1 | <?php |
||
| 13 | class OutputFile |
||
| 14 | { |
||
| 15 | /** @var resource<file>|null */ |
||
| 16 | protected $fp; |
||
| 17 | |||
| 18 | /** @var string */ |
||
| 19 | protected $fileName; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | protected $createdDir; |
||
| 23 | |||
| 24 | /** @var bool */ |
||
| 25 | private $success = false; |
||
| 26 | |||
| 27 | 8 | public function __construct($fileName) |
|
| 28 | { |
||
| 29 | 8 | $this->fileName = $fileName; |
|
| 30 | 8 | if (is_dir($fileName)) { |
|
| 31 | 1 | throw new Downloader\TransportException( |
|
| 32 | 1 | "The file could not be written to $fileName. Directory exists." |
|
| 33 | ); |
||
| 34 | } |
||
| 35 | |||
| 36 | 7 | $this->createDir($fileName); |
|
| 37 | |||
| 38 | 6 | $this->fp = fopen($fileName, 'wb'); |
|
| 39 | 6 | if (!$this->fp) { |
|
| 40 | 1 | throw new Downloader\TransportException( |
|
| 41 | 1 | "The file could not be written to $fileName." |
|
| 42 | ); |
||
| 43 | } |
||
| 44 | 5 | } |
|
| 45 | |||
| 46 | 5 | public function __destruct() |
|
| 47 | { |
||
| 48 | 5 | if ($this->fp) { |
|
| 49 | 5 | fclose($this->fp); |
|
| 50 | } |
||
| 51 | |||
| 52 | 5 | if (!$this->success) { |
|
| 53 | 4 | unlink($this->fileName); |
|
| 54 | 4 | if ($this->createdDir) { |
|
| 55 | 4 | rmdir($this->createdDir); |
|
| 56 | } |
||
| 57 | } |
||
| 58 | 5 | } |
|
| 59 | |||
| 60 | 5 | public function getPointer() |
|
| 64 | |||
| 65 | 1 | public function setSuccess() |
|
| 69 | |||
| 70 | 7 | protected function createDir($fileName) |
|
| 82 | } |
||
| 83 |