* '/path/to/preferences' * ), * $user // Instance of \Icinga\User * ); * * $preferences = new Preferences($store->load()); * $preferences->aPreference = 'value'; * $store->save($preferences); * */ abstract class PreferencesStore { /** * Store config * * @var ConfigObject */ protected $config; /** * Given user * * @var User */ protected $user; /** * Create a new store * * @param ConfigObject $config The config for this adapter * @param User $user The user to which these preferences belong */ public function __construct(ConfigObject $config, User $user) { $this->config = $config; $this->user = $user; $this->init(); } /** * Getter for the store config * * @return ConfigObject */ public function getStoreConfig() { return $this->config; } /** * Getter for the user * * @return User */ public function getUser() { return $this->user; } /** * Initialize the store */ abstract protected function init(); /** * Load preferences from source * * @return array */ abstract public function load(); /** * Save the given preferences * * @param Preferences $preferences The preferences to save */ abstract public function save(Preferences $preferences); /** * Create preferences storage adapter from config * * @param ConfigObject $config The config for the adapter * @param User $user The user to which these preferences belong * * @return self * * @throws ConfigurationError When the configuration defines an invalid storage type */ public static function create(ConfigObject $config, User $user) { $resourceConfig = ResourceFactory::getResourceConfig($config->resource); if ($resourceConfig->db === 'mysql') { $resourceConfig->charset = 'utf8mb4'; } $config->connection = ResourceFactory::createResource($resourceConfig); return new DbStore($config, $user); } }