<?php

/**
 * Provides a class factory for all the different quota modes
 *
 * @package Atmail
 * @subpackage Quota
 * @copyright Copyright (C) 2011 Atmail. All rights reserved.
 * @author Atmail
 */

class Atmail_Quota_Factory
{
	public static function getInstance($configuration = array())
	{
		// create a new instance of the quota class based on the configuration given

		// sanity check input variables
		$configuration[Atmail_Quota::TYPE] = array_key_exists(Atmail_Quota::TYPE, $configuration) ? $configuration[Atmail_Quota::TYPE] : Atmail_Quota::TYPE_DB;
		if(!array_key_exists(Atmail_Quota::ACCOUNT, $configuration))
		{
			$username = Zend_Auth::getInstance()->getStorage()->read();
			if($username != null && !is_string($username) && array_key_exists('user', $username))
			{
				$username = $username['user'];
			}
			if($username != '')
			{
				$configuration[Atmail_Quota::ACCOUNT] = $username;
			}
		}
		unset($userData);
		Atmail_Quota::log('getInstance with configuration = ' . print_r($configuration, true));

		// check valid type
		if(in_array($configuration[Atmail_Quota::TYPE], Atmail_Quota::$validTypes) )
		{
			// valid type found, instance class with configuration
			$className = 'Atmail_Quota_' . $configuration[Atmail_Quota::TYPE];
			Atmail_Quota::log('using class ' . $className);
			return new $className($configuration);
		}

		return false;
	}
}
