Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| storage | |
0.00% |
0 / 25 |
|
0.00% |
0 / 11 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get_name | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_adapter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| read | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| write | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| delete | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| exists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| file_size | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| total_files | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| total_size | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| free_space | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\storage; |
| 15 | |
| 16 | use phpbb\storage\adapter\adapter_interface; |
| 17 | use phpbb\storage\exception\storage_exception; |
| 18 | |
| 19 | /** |
| 20 | * Experimental |
| 21 | */ |
| 22 | class storage |
| 23 | { |
| 24 | /** |
| 25 | * @var adapter_interface |
| 26 | */ |
| 27 | protected $adapter; |
| 28 | |
| 29 | /** |
| 30 | * @var adapter_factory |
| 31 | */ |
| 32 | protected $factory; |
| 33 | |
| 34 | /** |
| 35 | * @var file_tracker |
| 36 | */ |
| 37 | protected $file_tracker; |
| 38 | |
| 39 | /** |
| 40 | * @var string |
| 41 | */ |
| 42 | protected $storage_name; |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
| 47 | * @param adapter_factory $factory |
| 48 | * @param file_tracker $file_tracker |
| 49 | * @param string $storage_name |
| 50 | */ |
| 51 | public function __construct(adapter_factory $factory, file_tracker $file_tracker, string $storage_name) |
| 52 | { |
| 53 | $this->factory = $factory; |
| 54 | $this->file_tracker = $file_tracker; |
| 55 | $this->storage_name = $storage_name; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns storage name |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function get_name(): string |
| 64 | { |
| 65 | return $this->storage_name; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns an adapter instance |
| 70 | * |
| 71 | * @return adapter_interface |
| 72 | */ |
| 73 | protected function get_adapter(): mixed |
| 74 | { |
| 75 | if ($this->adapter === null) |
| 76 | { |
| 77 | $this->adapter = $this->factory->get($this->storage_name); |
| 78 | } |
| 79 | |
| 80 | return $this->adapter; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Reads a file as a stream |
| 85 | * |
| 86 | * @param string $path File to read |
| 87 | * |
| 88 | * @return resource Returns a file pointer |
| 89 | * @throws storage_exception When the file doesn't exist |
| 90 | * When unable to open file |
| 91 | * |
| 92 | */ |
| 93 | public function read(string $path) |
| 94 | { |
| 95 | if (!$this->exists($path)) |
| 96 | { |
| 97 | throw new storage_exception('STORAGE_FILE_NO_EXIST', $path); |
| 98 | } |
| 99 | |
| 100 | return $this->get_adapter()->read($path); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Writes a new file using a stream |
| 105 | * |
| 106 | * @param string $path The target file |
| 107 | * @param resource $resource The resource |
| 108 | * |
| 109 | * @throws storage_exception When the file exist |
| 110 | * When target file cannot be created |
| 111 | */ |
| 112 | public function write(string $path, $resource): void |
| 113 | { |
| 114 | if ($this->exists($path)) |
| 115 | { |
| 116 | throw new storage_exception('STORAGE_FILE_EXISTS', $path); |
| 117 | } |
| 118 | |
| 119 | if (!is_resource($resource)) |
| 120 | { |
| 121 | throw new storage_exception('STORAGE_INVALID_RESOURCE'); |
| 122 | } |
| 123 | |
| 124 | $size = $this->get_adapter()->write($path, $resource); |
| 125 | $this->file_tracker->track_file($this->storage_name, $path, $size); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Removes files or directories |
| 130 | * |
| 131 | * @param string $path file/directory to remove |
| 132 | * |
| 133 | * @throws storage_exception When removal fails |
| 134 | * When the file doesn't exist |
| 135 | */ |
| 136 | public function delete(string $path): void |
| 137 | { |
| 138 | if (!$this->exists($path)) |
| 139 | { |
| 140 | throw new storage_exception('STORAGE_FILE_NO_EXIST', $path); |
| 141 | } |
| 142 | |
| 143 | $this->get_adapter()->delete($path); |
| 144 | $this->file_tracker->untrack_file($this->get_name(), $path); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Checks the existence of files or directories |
| 149 | * |
| 150 | * @param string $path file/directory to check |
| 151 | * |
| 152 | * @return bool Returns true if the file/directory exist, false otherwise |
| 153 | */ |
| 154 | public function exists(string $path): bool |
| 155 | { |
| 156 | return $this->file_tracker->is_tracked($this->get_name(), $path); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get file size in bytes |
| 161 | * |
| 162 | * @param string $path The file |
| 163 | * |
| 164 | * @return int Size in bytes. |
| 165 | */ |
| 166 | public function file_size(string $path): int |
| 167 | { |
| 168 | return $this->file_tracker->file_size($this->get_name(), $path); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Return the number of files stored in this storage |
| 173 | * |
| 174 | * @return int Number of files. |
| 175 | */ |
| 176 | public function total_files(): int |
| 177 | { |
| 178 | return $this->file_tracker->total_files($this->get_name()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get total storage size |
| 183 | * |
| 184 | * @return float Size in bytes |
| 185 | */ |
| 186 | public function total_size(): float |
| 187 | { |
| 188 | return $this->file_tracker->total_size($this->get_name()); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get space available in bytes |
| 193 | * |
| 194 | * @return float Returns available space |
| 195 | * @throws storage_exception When unable to retrieve available storage space |
| 196 | * |
| 197 | */ |
| 198 | public function free_space() |
| 199 | { |
| 200 | return $this->get_adapter()->free_space(); |
| 201 | } |
| 202 | |
| 203 | } |