<commit_before><?php

namespace App\Service\User;

use Aura\Session\Segment;
use Aura\Session\Session;
use App\Service\Base\BaseService;
use Cake\Database\Connection;

/**
 * User Session Handler
 */
class UserSession extends BaseService
{

    /**
     * Session
     *
     * @var Session
     */
    protected $session;

    /**
     * Session segment
     *
     * @var Segment
     */
    protected $segment;

    /**
     * Database
     *
     * @var Connection
     */
    protected $db;

    /**
     * @var Token
     */
    protected $token;

    /**
     * @var string
     */
    protected $secret = '';

    /**
     * UserSession constructor.
     *
     * @param Session $session Storage
     * @param Connection $db Database
     * @param string $secret Secret session key
     */
    public function __construct(Session $session, Connection $db, $secret = '')
    {
        $this->session = $session;
        $this->segment = $this->session->getSegment('app');
        $this->db = $db;
        $this->secret = $secret;
        $this->token = $this->createToken($secret);
    }

    /**
     * Get user Id.
     *
     * @return int
     */
    public function getId()
    {
        return (int)$this->get('user.id');
    }

    /**
     * Return token object.
     *
     * @return Token
     */
    public function getToken()
    {
        return $this->token;
    }

    /**
     * Create token object.
     *
     * @param $secret
     * @return Token
     */
    protected function createToken($secret)
    {
        return new Token($this->session->getId() . $secret);
    }

    /**
     * Change user session locale
     *
     * @param string $locale
     * @param string $domain
     * @return bool Status
     */
    public function setLocale($locale = 'en_US', $domain = 'messages')
    {
        $this->set('user.locale', $locale);
        $this->set('user.domain', $domain);

        set_locale($locale, $domain);
        return true;
    }

    /**
     * Get locale
     *
     * @param string $default
     * @return  string Locale
     */
    public function getLocale($default = 'en_US')
    {
        $result = $this->get('user.locale');
        if (empty($result)) {
            $result = $default;
        }
        return $result;
    }

    /**
     * Login user with username and password
     *
     * @param string $username Username
     * @param string $password Password
     * @return bool Status
     */
    public function login($username, $password)
    {
        // Check username and password
        $auth = new UserAuthentication($this->db, $this->token, $username, $password);
        $authResult = $auth->authenticate();

        if (!$authResult->isValid()) {
            return false;
        }

        $user = $authResult->getIdentity();

        // Login ok
        // Create new session id<commit_msg>Clear session data<commit_after><?php

namespace App\Service\User;

use Aura\Session\Segment;
use Aura\Session\Session;
use App\Service\Base\BaseService;
use Cake\Database\Connection;

/**
 * User Session Handler
 */
class UserSession extends BaseService
{

    /**
     * Session
     *
     * @var Session
     */
    protected $session;

    /**
     * Session segment
     *
     * @var Segment
     */
    protected $segment;

    /**
     * Database
     *
     * @var Connection
     */
    protected $db;

    /**
     * @var Token
     */
    protected $token;

    /**
     * @var string
     */
    protected $secret = '';

    /**
     * UserSession constructor.
     *
     * @param Session $session Storage
     * @param Connection $db Database
     * @param string $secret Secret session key
     */
    public function __construct(Session $session, Connection $db, $secret = '')
    {
        $this->session = $session;
        $this->segment = $this->session->getSegment('app');
        $this->db = $db;
        $this->secret = $secret;
        $this->token = $this->createToken($secret);
    }

    /**
     * Get user Id.
     *
     * @return int
     */
    public function getId()
    {
        return (int)$this->get('user.id');
    }

    /**
     * Return token object.
     *
     * @return Token
     */
    public function getToken()
    {
        return $this->token;
    }

    /**
     * Create token object.
     *
     * @param $secret
     * @return Token
     */
    protected function createToken($secret)
    {
        return new Token($this->session->getId() . $secret);
    }

    /**
     * Change user session locale
     *
     * @param string $locale
     * @param string $domain
     * @return bool Status
     */
    public function setLocale($locale = 'en_US', $domain = 'messages')
    {
        $this->set('user.locale', $locale);
        $this->set('user.domain', $domain);

        set_locale($locale, $domain);
        return true;
    }

    /**
     * Get locale
     *
     * @param string $default
     * @return  string Locale
     */
    public function getLocale($default = 'en_US')
    {
        $result = $this->get('user.locale');
        if (empty($result)) {
            $result = $default;
        }
        return $result;
    }

    /**
     * Login user with username and password
     *
     * @param string $username Username
     * @param string $password Password
     * @return bool Status
     */
    public function login($username, $password)
    {
        // Check username and password
        $auth = new UserAuthentication($this->db, $this->token, $username, $password);
        $authResult = $auth->authenticate();

        if (!$authResult->isValid()) {
            return false;
        }

        $user = $authResult->getIdentity();

        // Clear session data
        $this->segment->clear();
        $this->segment->clearFlash();
        $this->segment->clearFlashNow();

        // Create new session id
{'fraction_non_alphanumeric': 0.14002732240437157, 'fraction_numerical': 0.0, 'mean_word_length': 1.8077660594439118, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '3288999', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1673, 'n_words': 407}