| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Features\UserSettings\Factories; |
| 6 |
|
| 7 |
use Metricool\Features\UserSettings\Storage\AbstractStorage; |
| 8 |
|
| 9 |
class StorageFactory |
| 10 |
{ |
| 11 |
private const STORAGE_NAMESPACE = '\\Metricool\\Features\\UserSettings\\Storage\\'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Creates a storage from the user_settings configuration |
| 15 |
* @see config/user_settings.php |
| 16 |
*/ |
| 17 |
public static function createFromConfig(string $name, array $options): AbstractStorage |
| 18 |
{ |
| 19 |
if (!isset($options['class'])) { |
| 20 |
throw new \InvalidArgumentException('Class for "' . esc_html($name) . '" not mapped, please add it to the config'); |
| 21 |
} |
| 22 |
|
| 23 |
$storageClass = self::STORAGE_NAMESPACE . ucfirst($options['class']); |
| 24 |
|
| 25 |
if (!class_exists($storageClass)) { |
| 26 |
throw new \InvalidArgumentException('Storage "' . esc_html($storageClass) . '" not found'); |
| 27 |
} |
| 28 |
|
| 29 |
return new $storageClass($name, $options); |
| 30 |
} |
| 31 |
} |
| 32 |
|