Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| helper | |
0.00% |
0 / 38 |
|
0.00% |
0 / 11 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| get_provider_options | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_current_provider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_current_definition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_current_adapter | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_new_adapter | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| delete_storage_options | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| set_storage_provider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_storage_definition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| copy_file_to_new_adapter | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| update_storage_config | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 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\config\config; |
| 17 | use phpbb\di\service_collection; |
| 18 | |
| 19 | class helper |
| 20 | { |
| 21 | /** @var config */ |
| 22 | protected $config; |
| 23 | |
| 24 | /** @var adapter_factory */ |
| 25 | protected $adapter_factory; |
| 26 | |
| 27 | /** @var state_helper */ |
| 28 | protected $state_helper; |
| 29 | |
| 30 | /** @var service_collection */ |
| 31 | protected $provider_collection; |
| 32 | |
| 33 | /** @var service_collection */ |
| 34 | protected $adapter_collection; |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * @param config $config |
| 40 | * @param adapter_factory $adapter_factory |
| 41 | * @param state_helper $state_helper |
| 42 | * @param service_collection $provider_collection |
| 43 | * @param service_collection $adapter_collection |
| 44 | */ |
| 45 | public function __construct(config $config, adapter_factory $adapter_factory, state_helper $state_helper, service_collection $provider_collection, service_collection $adapter_collection) |
| 46 | { |
| 47 | $this->config = $config; |
| 48 | $this->adapter_factory = $adapter_factory; |
| 49 | $this->state_helper = $state_helper; |
| 50 | $this->provider_collection = $provider_collection; |
| 51 | $this->adapter_collection = $adapter_collection; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get adapter definitions from a provider |
| 56 | * |
| 57 | * @param string $provider_class Provider class |
| 58 | * |
| 59 | * @return array Adapter definitions |
| 60 | */ |
| 61 | public function get_provider_options(string $provider_class) : array |
| 62 | { |
| 63 | return $this->provider_collection->get_by_class($provider_class)->get_options(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the current provider from config |
| 68 | * |
| 69 | * @param string $storage_name Storage name |
| 70 | * |
| 71 | * @return string The current provider |
| 72 | */ |
| 73 | public function get_current_provider(string $storage_name) : string |
| 74 | { |
| 75 | return (string) $this->config['storage\\' . $storage_name . '\\provider']; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the current value of the definition of a storage from config |
| 80 | * |
| 81 | * @param string $storage_name Storage name |
| 82 | * @param string $definition Definition |
| 83 | * |
| 84 | * @return string Definition value |
| 85 | */ |
| 86 | public function get_current_definition(string $storage_name, string $definition) : string |
| 87 | { |
| 88 | return (string) $this->config['storage\\' . $storage_name . '\\config\\' . $definition]; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get current storage adapter |
| 93 | * |
| 94 | * @param string $storage_name Storage adapter name |
| 95 | * |
| 96 | * @return object Storage adapter instance |
| 97 | */ |
| 98 | public function get_current_adapter(string $storage_name): object |
| 99 | { |
| 100 | static $adapters = []; |
| 101 | |
| 102 | if (!isset($adapters[$storage_name])) |
| 103 | { |
| 104 | $adapters[$storage_name] = $this->adapter_factory->get($storage_name); |
| 105 | } |
| 106 | |
| 107 | return $adapters[$storage_name]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get new storage adapter |
| 112 | * |
| 113 | * @param string $storage_name |
| 114 | * |
| 115 | * @return mixed Storage adapter instance |
| 116 | */ |
| 117 | public function get_new_adapter(string $storage_name): mixed |
| 118 | { |
| 119 | static $adapters = []; |
| 120 | |
| 121 | if (!isset($adapters[$storage_name])) |
| 122 | { |
| 123 | $provider_class = $this->state_helper->new_provider($storage_name); |
| 124 | $definitions = array_keys($this->get_provider_options($provider_class)); |
| 125 | |
| 126 | $options = []; |
| 127 | foreach ($definitions as $definition) |
| 128 | { |
| 129 | $options[$definition] = $this->state_helper->new_definition_value($storage_name, $definition); |
| 130 | } |
| 131 | |
| 132 | $adapters[$storage_name] = $this->adapter_factory->get_with_options($storage_name, $provider_class, $options); |
| 133 | } |
| 134 | |
| 135 | return $adapters[$storage_name]; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Delete configuration options for a given storage |
| 140 | * |
| 141 | * @param string $storage_name |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | public function delete_storage_options(string $storage_name): void |
| 146 | { |
| 147 | $provider = $this->get_current_provider($storage_name); |
| 148 | $options = $this->get_provider_options($provider); |
| 149 | |
| 150 | foreach (array_keys($options) as $definition) |
| 151 | { |
| 152 | $this->config->delete('storage\\' . $storage_name . '\\config\\' . $definition); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Set a provider in configuration for a given storage |
| 158 | * |
| 159 | * @param string $storage_name |
| 160 | * @param string $provider |
| 161 | * |
| 162 | * @return void |
| 163 | */ |
| 164 | public function set_storage_provider(string $storage_name, string $provider): void |
| 165 | { |
| 166 | $this->config->set('storage\\' . $storage_name . '\\provider', $provider); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Set storage options in configuration for a given storage |
| 171 | * |
| 172 | * @param string $storage_name |
| 173 | * @param string $definition |
| 174 | * @param string $value |
| 175 | * |
| 176 | * @return void |
| 177 | */ |
| 178 | public function set_storage_definition(string $storage_name, string $definition, string $value): void |
| 179 | { |
| 180 | $this->config->set('storage\\' . $storage_name . '\\config\\' . $definition, $value); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Copy a file from the current adapter to the new adapter |
| 185 | * |
| 186 | * @param $storage_name |
| 187 | * @param $file |
| 188 | * |
| 189 | * @return void |
| 190 | */ |
| 191 | public function copy_file_to_new_adapter($storage_name, $file): void |
| 192 | { |
| 193 | $current_adapter = $this->get_current_adapter($storage_name); |
| 194 | $new_adapter = $this->get_new_adapter($storage_name); |
| 195 | |
| 196 | $stream = $current_adapter->read($file); |
| 197 | $new_adapter->write($file, $stream); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Updates a storage with the info provided in the form (that is stored in the state at this point) |
| 203 | * |
| 204 | * @param string $storage_name Storage name |
| 205 | */ |
| 206 | public function update_storage_config(string $storage_name) : void |
| 207 | { |
| 208 | // Remove old storage config |
| 209 | $this->delete_storage_options($storage_name); |
| 210 | |
| 211 | // Update provider |
| 212 | $new_provider = $this->state_helper->new_provider($storage_name); |
| 213 | $this->set_storage_provider($storage_name, $new_provider); |
| 214 | |
| 215 | // Set new storage config |
| 216 | $new_options = $this->get_provider_options($new_provider); |
| 217 | |
| 218 | foreach (array_keys($new_options) as $definition) |
| 219 | { |
| 220 | $new_definition_value = $this->state_helper->new_definition_value($storage_name, $definition); |
| 221 | $this->set_storage_definition($storage_name, $definition, $new_definition_value); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | } |