<commit_before><?php

namespace App\Repositories;

use App\Entities\ApiToken;
use App\Entities\User;
use Carbon\Carbon;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;
use Illuminate\Support\Collection;
use Laravel\Spark\Contracts\Repositories\TokenRepository;
use Ramsey\Uuid\Uuid;
use Laravel\Spark\JWT;
use Laravel\Spark\Token;


class ApiTokenRepository extends EntityRepository implements TokenRepository
{
    /**
     * {@inheritdoc}
     */
    public function validToken($token)
    {
        $queryBuilder = $this->createQueryBuilder('at');
        $criteria = Criteria::create()->andWhere(
            Criteria::expr()->eq('token', $token)
        )->andWhere(
            Criteria::expr()->orX(
                Criteria::expr()->isNull('expires_at'),
                Criteria::expr()->gte('expires_at', Carbon::now())
            )
        )->setMaxResults(1);

        $queryBuilder->addCriteria($criteria);

        $result = $queryBuilder->getQuery()->getResult();
        return $result[0] ?? null;
    }

    /**
     * @param User $user
     * @param string $name
     * @param array $data<commit_msg>Refactor so that the User entity is eager loaded<commit_after><?php

namespace App\Repositories;

use App\Entities\ApiToken;
use App\Entities\User;
use Carbon\Carbon;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Illuminate\Support\Collection;
use Laravel\Spark\Contracts\Repositories\TokenRepository;
use Ramsey\Uuid\Uuid;
use Laravel\Spark\JWT;
use Laravel\Spark\Token;


class ApiTokenRepository extends EntityRepository implements TokenRepository
{
    /**
     * {@inheritdoc}
     */
    public function validToken($token)
    {
        $queryBuilder = $this->createQueryBuilder('at')
            ->addSelect('u')
            ->leftJoin('at.user', 'u');

        $criteria = Criteria::create()->andWhere(
            Criteria::expr()->eq('token', $token)
        )->andWhere(
            Criteria::expr()->orX(
                Criteria::expr()->isNull('expires_at'),
                Criteria::expr()->gte('expires_at', Carbon::now())
            )
        )->setMaxResults(1);

        return $queryBuilder->addCriteria($criteria)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * @param User $user
     * @param string $name
     * @param array $data
{'fraction_non_alphanumeric': 0.14173228346456693, 'fraction_numerical': 0.0012432656444260257, 'mean_word_length': 2.8810289389067525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '5549506', 'n_tokens_mistral': 711, 'n_tokens_neox': 700, 'n_words': 130}