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

/**
 * Class TB_Shared_Ent_Core_Session
 *
 * @property integer $id
 * @property integer $account_id
 * @property integer $profile_id
 * @property string $token
 * @property integer $last_update
 */
class TB_Shared_Ent_Core_Session extends Francis_Db_Row {

    public static function getByAccountId( $accountId ) {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE account_id = :account_id ORDER BY last_update DESC LIMIT 1';
        return self::findOne( $sql, array( ':account_id' => $accountId) );
    }

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

    /**
     * Deletes entry by given account- and profile id
     *
     * @param int $accountId
     * @param int $profileId
     */
    public static function deleteByAccountId( $accountId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE account_id = :account_id';
        return self::execute( $sql, array( ':account_id' => $accountId ) );
    }

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

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