<?php

class Atmail_Provider_Factory
{
	
	/* use this common interface factory class to create instances of a single provider */

	public static function factory( Array $configArray )
	{
		
		//Zend_Registry::get('log')->debug( "\n" . print_r($configArray, true) . "\n" . __METHOD__ . ' #' . __LINE__ . ": \$configArray \n");
		
		if( !array_key_exists('Provider', $configArray) )
			throw new Exception(__METHOD__ . ' #' . __LINE__ . ': Error: provider must be specified when instantiating provider with factory');
		if( !array_key_exists('Type', $configArray) )
			throw new Exception(__METHOD__ . ' #' . __LINE__ . ': Error: type must be specified when instantiating provider with factory');
		
		//TODO: before trying to instantiate, check if checking for class exists actually autoloads in atempt to auto find class definitions if unloaded (yet) files
		
		//some providers are so similar that they share the same class
		if( $configArray['Type'] == 'Email' && $configArray['Provider'] == 'Gmail' )
		{
		
			$configArray['Provider'] = 'IMAP';
		
		}
			
		$className = "Atmail_Provider_" . trim($configArray['Type']) . "_". trim($configArray['Provider']);
		
		try
		{
		
			$instance = new $className($configArray);
		
		}
		catch( Exception $e )
		{

			Zend_Registry::get('log')->debug( __METHOD__ . ' #' . __LINE__ . ": Failed instantiating provider class: " . $className . '. Ignoring as irrelevant');
			return null;
			
		}
		return $instance;
		
	}

}