<?php
/**
 * 
 * PasswordReset Table Service Layer
 * 
 * @author neilcorke <neil@corkeweb.com>
 *
 */
class Falk_PasswordResetService
{
    /**
     * password_reset Zend Table
     *
     * @var passwordResetTable
     */
    protected $passwordReset;
    
    /**
     * The PDO Database adapter
     */
    protected $db;
  	
 	/**
 	 * Instantiate the PasswordReset Table model
 	 */
  	function __construct()
    {    
    	// Get the default db adapter:
    	$this->db = Zend_Db_Table::getDefaultAdapter();
    	Zend_Db_Table_Abstract::setDefaultAdapter($this->db);
    	
      	$this->passwordReset = new Customer_Model_PasswordResetTable();                                
    }
    
    
    /**
     * Insert new reset record
     * 
     * @param int    $customerId  The customer ID
     * @param string $key         The reset key
     * 
     * @access public
     */
    public function newPasswordReset($customerId, $key)
    {
    	// Caluclate expiration:
    	$date = new Zend_Date();
    	
    	$expiration = $date->add('24', Zend_Date::HOUR)
    				 		->get(Zend_Date::TIMESTAMP);
    	
    	// Build the parameters array:
    	$params = array('reset_id' => $key,
    					'customer_id' => $customerId,
    					'expiration' => $expiration);

        // Execute the query:                		
      	$this->passwordReset->insert($params); 
    }
    
    
    /**
     * Get the Password Reset Data by key 
     * 
     * @param string  $key  The reset key
     * 
     * @return mixed array  The table row
     * 
     * @access public
     */
    public function getPasswordResetData($key)
    {
    	$row = $this->passwordReset->find($key);
     	
     	return $row;
    }
    
    
    /**
     * Delete any rows matching the customer ID
     * 
     * @param int  $customerId  The customer ID
     * 
     * @access public
     */
  	public function clearPasswordReset($customerId)
    {    	
    	$where = $this->passwordReset->getAdapter()->quoteInto('customer_id = ?', $customerId);
    	
    	$this->passwordReset->delete($where);
    }
}