* @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 \Zend_Config; use \Icinga\User; use \Icinga\Exception\ProgrammingError; use \Icinga\Application\DbAdapterFactory; /** * 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( 'Preferences StoreFactory could not create provider (class ' . $class . '). ' . 'Class is not instance of FlushObserverInterface.' ); } $items = $config->toArray(); if ($items['type'] == 'db') { $items['dbAdapter'] = DbAdapterFactory::getDbAdapter($items['resource']); } unset($items['type']); 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( 'Preferences StoreFactory could not create provider (class ' . $class . '). Class not found.' ); } }