<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

/**
 * Class TB_Shared_Ent_Core_PasswordReset
 *
 * @property integer $id
 * @property integer $account_id
 * @property string $email
 * @property string $token
 * @property string $state
 * @property timestamp $last_update
 */
class TB_Shared_Ent_Core_PasswordReset extends Francis_Db_Row {

    const STATE_PENDING = 'pending';
    const STATE_DELIVERED = 'delivered';

    /**
     * @return array
     */
    public static function getByState( $state ) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE state = :state";
        return self::findMany( $sql, array( ':state' => $state ) );
    }

    /**
     * @return array
     */
    public static function getByAccountId( $accountId) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE account_id = :account_id";
        return self::findOne( $sql, array( ':account_id' => $accountId) );
    }


    /**
     * @param string $token
     * @return NULL|TB_Shared_Ent_Core_PasswordReset
     */
    public static function getByToken( $token ) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE token = :token";
        return self::findOne( $sql, array( ':token' => $token ) );
    }

    /**
     * @param string $token
     * @return NULL|TB_Shared_Ent_Core_PasswordReset
     */
    public static function getForSending() {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE state = :state";
        return self::findMany( $sql, array( ':state' => TB_Shared_Ent_Core_PasswordReset::STATE_PENDING ) );
    }

    /**
     * Return table name
     *
     * @return null|string
     */
    public static function getTable() {
        return 'password_reset';
    }

    /**
     * Return db handler
     *
     * @return Francis_Db_MySql|null
     */
    public static function getDbh() {
        return TB_Shared_Db_Core::get();
    }

}