<?php

/**
 * Description of Core
 *
 * @author Arkadiusz Rychlik <a.rychlik at eppe.com.pl>
 */
class Rtn_RB_Auth_Adapter_Core implements Zend_Auth_Adapter_Interface {

	/**
	 * @return Rtn_RB_Auth_Adapter
	 */
	final static function factory() {
		return new static();
	}

	protected $username;
	protected $password;

	/**
	 * @var RedBean_OODBBean
	 */
	protected $bean;

	public function setUsername($_username) {
		$this->username = $_username;
		return $this;
	}

	public function setPassword($_password) {
		$this->password = $_password;
		return $this;
	}

	public function authenticate() {
		$this->bean = R::findOne('user', 'name = :name AND pass = :pass AND active = 1', array(
				':name' => $this->username,
				':pass' => R::getCell('SELECT PASSWORD("'.$this->password.'")')
		));

		if (!empty($this->bean)){
			return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $this->bean);
		}
		else{
			return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $this->bean);
		}
	}
	
	public function authenticateWithFacebook($fbuserid) {
		$this->bean = R::findOne('user', 'fbuserid = :fbuserid AND active = 1', array(
				':fbuserid' => $fbuserid,
		));

		return !empty($this->bean);		
	}

	/**
	 * @return RedBean_OODBBean
	 */
	public function getBean() {
		return $this->bean;
	}

}

