<commit_before><?php
namespace Sum\Oauth2\Server\Storage\Pdo\Mysql;

use League\OAuth2\Server\Storage\SessionInterface;
use Phalcon\Db\Adapter\Pdo;
use Phalcon\Db;

class Session implements SessionInterface
{

    protected $db;
    protected $tables = array(
        'oauth_sessions'                => 'oauth_sessions',
        'oauth_session_authcodes'       => 'oauth_session_authcodes',
        'oauth_session_redirects'       => 'oauth_session_redirects',
        'oauth_session_access_tokens'   => 'oauth_session_access_tokens',
        'oauth_session_refresh_tokens'  => 'oauth_session_refresh_tokens',
        'oauth_session_token_scopes'    => 'oauth_session_token_scopes',
        'oauth_session_authcode_scopes' => 'oauth_session_authcode_scopes'
    );


    /**
     * @param $db \Phalcon\Db\Adapter\Pdo
     */
    public function __construct($db)
    {
        $this->db = $db;
    }

    /**
     * Create a new session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_sessions (client_id, owner_type,  owner_id)
     *  VALUE (:clientId, :ownerType, :ownerId)
     * </code>
     *
     * @param  string $clientId The client ID
     * @param  string $ownerType The type of the session owner (e.g. "user")
     * @param  string $ownerId The ID of the session owner (e.g. "123")
     * @return int               The session ID
     */
    public function createSession($clientId, $ownerType, $ownerId)
    {
        return $this->db->insert(
            $this->tables['oauth_sessions'],
            [$clientId, $ownerType, $ownerId],
            ['client_id', 'owner_type', 'owner_id']
        );
    }

    /**
     * Delete a session
     *
     * Example SQL query:
     *
     * <code>
     * DELETE FROM oauth_sessions WHERE client_id = :clientId AND owner_type = :type AND owner_id = :typeId
     * </code>
     *
     * @param  string $clientId The client ID
     * @param  string $ownerType The type of the session owner (e.g. "user")
     * @param  string $ownerId The ID of the session owner (e.g. "123")
     * @return void
     */
    public function deleteSession($clientId, $ownerType, $ownerId)
    {
        $this->db->delete(
            $this->tables['oauth_sessions'],
            'client_id = :clientId AND owner_type = :type AND :owner_id = :typeId',
            ['type' => $ownerType, 'typeId' => $ownerId, 'clientId' => $clientId]
        );
    }

    /**
     * Associate a redirect URI with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_redirects (session_id, redirect_uri) VALUE (:sessionId, :redirectUri)
     * </code>
     *
     * @param  int $sessionId The session ID
     * @param  string $redirectUri The redirect URI
     * @return void
     */
    public function associateRedirectUri($sessionId, $redirectUri)
    {
        $this->db->insert(
            $this->tables['oauth_session_redirects'],
            [$sessionId, $redirectUri],
            ['session_id', 'redirect_uri']
        );
    }

    /**
     * Associate an access token with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_access_tokens (session_id, access_token, access_token_expires)
     *  VALUE (:sessionId, :accessToken, :accessTokenExpire)
     * </code>
     *
     * @param  int $sessionId The session ID
     * @param  string $accessToken The access token
     * @param  int $expireTime Unix timestamp of the access token expiry time
     * @return int                 The access token ID
     */
    public function associateAccessToken($sessionId, $accessToken, $expireTime)
    {
        $this->db->insert(
            $this->tables['oauth_session_access_tokens'],
            [$sessionId, $accessToken, $expireTime],
            ['session_id', 'access_token', 'access_token_expires']
        );
    }

    /**
     * Associate a refresh token with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_refresh_tokens (session_access_token_id, refresh_token, refresh_token_expires,
     *  client_id) VALUE (:accessTokenId, :refreshToken, :expireTime, :clientId)
     * </code>
     *
     * @param  int $accessTokenId The access token ID
     * @param  string $refreshToken The refresh token
     * @param  int $expireTime Unix timestamp of the refresh token expiry time
     * @param  string $clientId The client ID
     * @return void
     */
    public function associateRefreshToken($accessTokenId, $refreshToken, $expireTime, $clientId)
    {
        $this->db->insert(
            $this->tables['oauth_session_refresh_tokens'],
            [$accessTokenId, $refreshToken, $expireTime, $clientId],
            ['session_access_token_id', 'refresh_token', 'refresh_token_expires', 'client_id']
        );
    }

    /**
     * Assocate an authorization code with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_authcodes (session_id, auth_code, auth_code_expires)
     *  VALUE (:sessionId, :authCode, :authCodeExpires)
     * </code>
     *
     * @param  int $sessionId The session ID
     * @param  string $authCode The authorization code
     * @param  int $expireTime Unix timestamp of the access token expiry time
     * @return int                The auth code ID
     */
    public function associateAuthCode($sessionId, $authCode, $expireTime)
    {
        $this->db->insert(
            $this->tables['oauth_session_authcodes'],
            [$sessionId, $authCode, $expireTime],
            ['session_id', 'auth_code', 'auth_code_expires']
        );
    }

    /**
     * Remove an associated authorization token from a session
     *
     * Example SQL query:
     *
     * <code>
     * DELETE FROM oauth_session_authcodes WHERE session_id = :sessionId
     * </code>
     *
     * @param  int $sessionId The session ID
     * @return void
     */
    public function removeAuthCode($sessionId)
    {
        $this->db->delete(
            $this->tables['oauth_session_authcodes'],
            'session_id = :sessionId',
            ['sessionId' => $sessionId]
        );
    }

    /**
     * Validate an authorization code
     *
     * Example SQL query:
     *
     * <code><commit_msg>fix return variables<commit_after><?php
namespace Sum\Oauth2\Server\Storage\Pdo\Mysql;

use League\OAuth2\Server\Storage\SessionInterface;
use Phalcon\Db\Adapter\Pdo;
use Phalcon\Db;

class Session implements SessionInterface
{

    protected $db;
    protected $tables = array(
        'oauth_sessions'                => 'oauth_sessions',
        'oauth_session_authcodes'       => 'oauth_session_authcodes',
        'oauth_session_redirects'       => 'oauth_session_redirects',
        'oauth_session_access_tokens'   => 'oauth_session_access_tokens',
        'oauth_session_refresh_tokens'  => 'oauth_session_refresh_tokens',
        'oauth_session_token_scopes'    => 'oauth_session_token_scopes',
        'oauth_session_authcode_scopes' => 'oauth_session_authcode_scopes'
    );


    /**
     * @param $db \Phalcon\Db\Adapter\Pdo
     */
    public function __construct($db)
    {
        $this->db = $db;
    }

    /**
     * Create a new session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_sessions (client_id, owner_type,  owner_id)
     *  VALUE (:clientId, :ownerType, :ownerId)
     * </code>
     *
     * @param  string $clientId The client ID
     * @param  string $ownerType The type of the session owner (e.g. "user")
     * @param  string $ownerId The ID of the session owner (e.g. "123")
     * @return int               The session ID
     */
    public function createSession($clientId, $ownerType, $ownerId)
    {
        $this->db->insert(
            $this->tables['oauth_sessions'],
            [$clientId, $ownerType, $ownerId],
            ['client_id', 'owner_type', 'owner_id']
        );
        return $this->db->lastInsertId();
    }

    /**
     * Delete a session
     *
     * Example SQL query:
     *
     * <code>
     * DELETE FROM oauth_sessions WHERE client_id = :clientId AND owner_type = :type AND owner_id = :typeId
     * </code>
     *
     * @param  string $clientId The client ID
     * @param  string $ownerType The type of the session owner (e.g. "user")
     * @param  string $ownerId The ID of the session owner (e.g. "123")
     * @return void
     */
    public function deleteSession($clientId, $ownerType, $ownerId)
    {
        $this->db->delete(
            $this->tables['oauth_sessions'],
            'client_id = :clientId AND owner_type = :type AND :owner_id = :typeId',
            ['type' => $ownerType, 'typeId' => $ownerId, 'clientId' => $clientId]
        );
    }

    /**
     * Associate a redirect URI with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_redirects (session_id, redirect_uri) VALUE (:sessionId, :redirectUri)
     * </code>
     *
     * @param  int $sessionId The session ID
     * @param  string $redirectUri The redirect URI
     * @return void
     */
    public function associateRedirectUri($sessionId, $redirectUri)
    {
        $this->db->insert(
            $this->tables['oauth_session_redirects'],
            [$sessionId, $redirectUri],
            ['session_id', 'redirect_uri']
        );
    }

    /**
     * Associate an access token with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_access_tokens (session_id, access_token, access_token_expires)
     *  VALUE (:sessionId, :accessToken, :accessTokenExpire)
     * </code>
     *
     * @param  int $sessionId The session ID
     * @param  string $accessToken The access token
     * @param  int $expireTime Unix timestamp of the access token expiry time
     * @return int                 The access token ID
     */
    public function associateAccessToken($sessionId, $accessToken, $expireTime)
    {
        $this->db->insert(
            $this->tables['oauth_session_access_tokens'],
            [$sessionId, $accessToken, $expireTime],
            ['session_id', 'access_token', 'access_token_expires']
        );
        return $this->db->lastInsertId();
    }

    /**
     * Associate a refresh token with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_refresh_tokens (session_access_token_id, refresh_token, refresh_token_expires,
     *  client_id) VALUE (:accessTokenId, :refreshToken, :expireTime, :clientId)
     * </code>
     *
     * @param  int $accessTokenId The access token ID
     * @param  string $refreshToken The refresh token
     * @param  int $expireTime Unix timestamp of the refresh token expiry time
     * @param  string $clientId The client ID
     * @return void
     */
    public function associateRefreshToken($accessTokenId, $refreshToken, $expireTime, $clientId)
    {
        $this->db->insert(
            $this->tables['oauth_session_refresh_tokens'],
            [$accessTokenId, $refreshToken, $expireTime, $clientId],
            ['session_access_token_id', 'refresh_token', 'refresh_token_expires', 'client_id']
        );
    }

    /**
     * Assocate an authorization code with a session
     *
     * Example SQL query:
     *
     * <code>
     * INSERT INTO oauth_session_authcodes (session_id, auth_code, auth_code_expires)
     *  VALUE (:sessionId, :authCode, :authCodeExpires)
     * </code>
     *
     * @param  int $sessionId The session ID
     * @param  string $authCode The authorization code
     * @param  int $expireTime Unix timestamp of the access token expiry time
     * @return int                The auth code ID
     */
    public function associateAuthCode($sessionId, $authCode, $expireTime)
    {
        $this->db->insert(
            $this->tables['oauth_session_authcodes'],
            [$sessionId, $authCode, $expireTime],
            ['session_id', 'auth_code', 'auth_code_expires']
        );
        return $this->db->lastInsertId();
    }

    /**
     * Remove an associated authorization token from a session
     *
     * Example SQL query:
     *
     * <code>
     * DELETE FROM oauth_session_authcodes WHERE session_id = :sessionId
     * </code>
     *
     * @param  int $sessionId The session ID
     * @return void
     */
    public function removeAuthCode($sessionId)
    {
        $this->db->delete(
            $this->tables['oauth_session_authcodes'],
            'session_id = :sessionId',
            ['sessionId' => $sessionId]
        );
    }

    /**
     * Validate an authorization code
     *
     * Example SQL query:
     *
     * <code>
{'fraction_non_alphanumeric': 0.11393805309734513, 'fraction_numerical': 0.0012642225031605564, 'mean_word_length': 2.3378164556962027, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3943165', 'n_tokens_mistral': 3941, 'n_tokens_neox': 3828, 'n_words': 936}