<commit_before><?php

namespace Rhubarb\Scaffolds\Authentication;

use Rhubarb\Stem\Exceptions\RecordNotFoundException;
use Rhubarb\Stem\Filters\AndGroup;
use Rhubarb\Stem\Filters\Equals;
use Rhubarb\Stem\Models\Model;
use Rhubarb\Stem\Repositories\MySql\Schema\Columns\MySqlEnumColumn;
use Rhubarb\Stem\Schema\Columns\AutoIncrementColumn;
use Rhubarb\Stem\Schema\Columns\BooleanColumn;
use Rhubarb\Stem\Schema\Columns\DateTimeColumn;
use Rhubarb\Stem\Schema\Columns\ForeignKeyColumn;
use Rhubarb\Stem\Schema\Columns\IntegerColumn;
use Rhubarb\Stem\Schema\Columns\LongStringColumn;
use Rhubarb\Stem\Schema\Columns\StringColumn;
use Rhubarb\Stem\Schema\ModelSchema;

class UserLog extends Model
{
    const USER_LOG_LOGIN_SUCCESSFUL = 'Login successful';
    const USER_LOG_LOGIN_FAILED = 'Login failed';
    const USER_LOG_LOGIN_EXPIRED = 'Login expired';
    const USER_LOG_LOGIN_LOCKED = 'Login lLocked out';
    const USER_LOG_LOGIN_DISABLED = 'Login disabled';
    const USER_LOG_PASSWORD_CHANGED = '<PASSWORD>';

    protected function createSchema()
    {
        $modelSchema = new ModelSchema('tblAuthenticationUserLog');
        $modelSchema->addColumn(
            new AutoIncrementColumn('UserLogID'),
            new ForeignKeyColumn('UserID'),
            new StringColumn('EnteredUsername', 200),
            new MySqlEnumColumn("LogType", '', [
                '',
                self::USER_LOG_LOGIN_SUCCESSFUL,
                self::USER_LOG_LOGIN_LOCKED,
                self::USER_LOG_LOGIN_FAILED,
                self::USER_LOG_LOGIN_EXPIRED,
                self::USER_LOG_LOGIN_DISABLED,
                self::USER_LOG_PASSWORD_CHANGED
            ]),
            new LongStringColumn('Message'),
            new LongStringColumn('Data'),
            new DateTimeColumn('DateCreated')

        );

        return $modelSchema;
    }

    protected function beforeSave()
    {
        if ($this->isNewRecord()) {
            $this->DateCreated = 'now';
        }

        parent::beforeSave();
    }

    public static function getLastSuccessfulLoginAttempt($username)
    {
        try {
            return self::findFirst(new AndGroup(
                [
                    new Equals("EnteredUsername", $username),
                    new Equals("LogType", self::USER_LOG_LOGIN_SUCCESSFUL)
                ]
            ));
        } catch (RecordNotFoundException $exception) {
        }

        return null;
    }
}<commit_msg>Real world testing<commit_after><?php

namespace Rhubarb\Scaffolds\Authentication;

use Rhubarb\Stem\Exceptions\RecordNotFoundException;
use Rhubarb\Stem\Filters\AndGroup;
use Rhubarb\Stem\Filters\Equals;
use Rhubarb\Stem\Models\Model;
use Rhubarb\Stem\Repositories\MySql\Schema\Columns\MySqlEnumColumn;
use Rhubarb\Stem\Schema\Columns\AutoIncrementColumn;
use Rhubarb\Stem\Schema\Columns\BooleanColumn;
use Rhubarb\Stem\Schema\Columns\DateTimeColumn;
use Rhubarb\Stem\Schema\Columns\ForeignKeyColumn;
use Rhubarb\Stem\Schema\Columns\IntegerColumn;
use Rhubarb\Stem\Schema\Columns\LongStringColumn;
use Rhubarb\Stem\Schema\Columns\StringColumn;
use Rhubarb\Stem\Schema\ModelSchema;

class UserLog extends Model
{
    const USER_LOG_LOGIN_SUCCESSFUL = 'Login successful';
    const USER_LOG_LOGIN_FAILED = 'Login failed';
    const USER_LOG_LOGIN_EXPIRED = 'Login expired';
    const USER_LOG_LOGIN_LOCKED = 'Login lLocked out';
    const USER_LOG_LOGIN_DISABLED = 'Login disabled';
    const USER_LOG_PASSWORD_CHANGED = '<PASSWORD>';

    protected function createSchema()
    {
        $modelSchema = new ModelSchema('tblAuthenticationUserLog');
        $modelSchema->addColumn(
            new AutoIncrementColumn('UserLogID'),
            new ForeignKeyColumn('UserID'),
            new StringColumn('EnteredUsername', 200),
            new MySqlEnumColumn("LogType", '', [
                '',
                self::USER_LOG_LOGIN_SUCCESSFUL,
                self::USER_LOG_LOGIN_LOCKED,
                self::USER_LOG_LOGIN_FAILED,
                self::USER_LOG_LOGIN_EXPIRED,
                self::USER_LOG_LOGIN_DISABLED,
                self::USER_LOG_PASSWORD_CHANGED
            ]),
            new LongStringColumn('Message'),
            new LongStringColumn('Data'),
            new DateTimeColumn('DateCreated')

        );

        return $modelSchema;
    }

    protected function beforeSave()
    {
        if ($this->isNewRecord()) {
            $this->DateCreated = 'now';
        }

        parent::beforeSave();
    }

    public static function getLastSuccessfulLoginAttempt($username)
    {
        try {
            return self::findLast(new AndGroup(
                [
                    new Equals("EnteredUsername", $username),
                    new Equals("LogType", self::USER_LOG_LOGIN_SUCCESSFUL)
                ]
            ));
        } catch (RecordNotFoundException $exception) {
        }

        return null;
    }
}
{'fraction_non_alphanumeric': 0.09723352318958503, 'fraction_numerical': 0.0012205044751830757, 'mean_word_length': 2.6557620817843866, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6563249', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1443, 'n_words': 230}