Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
36 / 48 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TempFileWriter | |
75.00% |
36 / 48 |
|
50.00% |
3 / 6 |
24.64 | |
0.00% |
0 / 1 |
| __construct | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
5.26 | |||
| __destruct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeTempFiles | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| writeFile | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| createTempDirectory | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
5.02 | |||
| createRootCacheDirectory | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoMediaManager\model\sharedStimulus\service; |
| 22 | |
| 23 | use RuntimeException; |
| 24 | |
| 25 | class TempFileWriter |
| 26 | { |
| 27 | /** @var string */ |
| 28 | private $cacheDirectory; |
| 29 | |
| 30 | /** @var string[] */ |
| 31 | private $createdFiles = []; |
| 32 | |
| 33 | /** @var string[] */ |
| 34 | private $createdDirectories = []; |
| 35 | |
| 36 | public function __construct(string $cacheDirectory = null) |
| 37 | { |
| 38 | $this->cacheDirectory = $cacheDirectory; |
| 39 | |
| 40 | if (null === $this->cacheDirectory) { |
| 41 | $this->cacheDirectory = defined('GENERIS_CACHE_PATH') && !empty(GENERIS_CACHE_PATH) |
| 42 | ? GENERIS_CACHE_PATH |
| 43 | : sys_get_temp_dir(); |
| 44 | } |
| 45 | |
| 46 | $this->createRootCacheDirectory(); |
| 47 | $this->cacheDirectory = realpath($this->cacheDirectory); |
| 48 | } |
| 49 | |
| 50 | public function __destruct() |
| 51 | { |
| 52 | $this->removeTempFiles(); |
| 53 | } |
| 54 | |
| 55 | public function removeTempFiles(): void |
| 56 | { |
| 57 | foreach (array_reverse($this->createdFiles) as $path) { |
| 58 | if (file_exists($path)) { |
| 59 | unlink($path); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | foreach (array_reverse($this->createdDirectories) as $path) { |
| 64 | if (is_dir($path)) { |
| 65 | rmdir($path); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | $this->createdFiles = []; |
| 70 | $this->createdDirectories = []; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @throws RuntimeException |
| 75 | */ |
| 76 | public function writeFile( |
| 77 | string $namespace, |
| 78 | string $basename, |
| 79 | string $data |
| 80 | ): string { |
| 81 | $path = $this->createTempDirectory($namespace) |
| 82 | . DIRECTORY_SEPARATOR |
| 83 | . $basename; |
| 84 | |
| 85 | if (!@file_put_contents($path, $data)) { |
| 86 | throw new RuntimeException( |
| 87 | 'Error writing data to temp file: ' . |
| 88 | error_get_last()['message'] |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | $path = realpath($path); |
| 93 | $this->createdFiles[] = $path; |
| 94 | |
| 95 | return $path; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @throws RuntimeException |
| 100 | */ |
| 101 | private function createTempDirectory(string $namespace): string |
| 102 | { |
| 103 | $this->createRootCacheDirectory(); |
| 104 | |
| 105 | $cacheDir = $this->cacheDirectory . DIRECTORY_SEPARATOR . $namespace; |
| 106 | |
| 107 | if (!is_dir($cacheDir)) { |
| 108 | if (!mkdir($cacheDir, 0777, true)) { |
| 109 | throw new RuntimeException( |
| 110 | 'Cannot create subdirectory under temp directory' |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | $this->createdDirectories[] = realpath($cacheDir); |
| 115 | } |
| 116 | |
| 117 | $tmpDir = realpath($cacheDir) . DIRECTORY_SEPARATOR . uniqid(); |
| 118 | if (!mkdir($tmpDir)) { |
| 119 | throw new RuntimeException( |
| 120 | "Unable to create temp directory {$tmpDir}" |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $this->createdDirectories[] = $tmpDir; |
| 125 | |
| 126 | return $tmpDir; |
| 127 | } |
| 128 | |
| 129 | private function createRootCacheDirectory(): void |
| 130 | { |
| 131 | if (!is_dir($this->cacheDirectory)) { |
| 132 | if (!mkdir($this->cacheDirectory, 0777, true)) { |
| 133 | throw new RuntimeException( |
| 134 | 'Cache root directory does not exist and cannot be created' |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | $this->createdDirectories[] = realpath($this->cacheDirectory); |
| 139 | } |
| 140 | } |
| 141 | } |