* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\User\Preferences; use Icinga\User; use Icinga\Exception\ProgrammingError; use \Zend_Config; use \Zend_Db; /** * Create preference stores from zend config */ final class StoreFactory { /** * Prefix for classes containing namespace */ const CLASS_PREFIX = 'Icinga\\User\\Preferences\\'; /** * Suffix for class */ const CLASS_SUFFIX = 'Store'; /** * Create storage adapter from zend configuration * * @param Zend_Config $config * @param User $user * @return FlushObserverInterface * @throws ProgrammingError */ public static function create(Zend_Config $config, User $user) { $class = self::CLASS_PREFIX. ucfirst($config->get('type')). self::CLASS_SUFFIX; if (class_exists($class)) { $store = new $class(); if (!$store instanceof FlushObserverInterface) { throw new ProgrammingError('Not instance of FlushObserverInterface: '. $class); } $items = $config->toArray(); unset($items['type']); // TODO(mh): Encapsulate into a db adapter factory (#4503) if (isset($items['dbname']) && isset($items['dbuser']) && isset($items['dbpassword']) && isset($items['dbhost']) && isset($items['dbtype']) ) { $zendDbType = 'PDO_'. strtoupper($items['dbtype']); $zendDbOptions = array( 'host' => $items['dbhost'], 'username' => $items['dbuser'], 'password' => $items['dbpassword'], 'dbname' => $items['dbname'] ); if (isset($items['port'])) { $zendDbOptions['port'] = $items['port']; } $dbAdapter = Zend_Db::factory($zendDbType, $zendDbOptions); $items['dbAdapter'] = $dbAdapter; } foreach ($items as $key => $value) { $setter = 'set'. ucfirst($key); if (is_callable(array($store, $setter))) { $store->$setter($value); } } $store->setUser($user); return $store; } throw new ProgrammingError('Could not instantiate class: '. $class); } }