<commit_before><?php

namespace PerFi\PerFiBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use PerFi\Domain\Account\Account;
use PerFi\Domain\Account\AccountId;
use PerFi\Domain\Account\AccountRepository as AccountRepositoryInterface;
use PerFi\Domain\Account\AccountType;
use PerFi\PerFiBundle\Entity\Account as DtoAccount;

/**
 * AccountRepository
 */
class AccountRepository extends EntityRepository
    implements AccountRepositoryInterface
{
    /**
     * {@inheritdoc}
     */
    public function add(Account $account)
    {
        $entity = new DtoAccount();
        $entity->setAccountId((string) $account->id());
        $entity->setTitle($account->title());
        $entity->setType((string) $account->type());

        $em = $this->getEntityManager();
        $em->persist($entity);
        $em->flush();
    }

    /**
     * Get an account by it's ID
     *
     * @param AccountId $accountId
     * @return Account
     */
    public function get(AccountId $accountId) : Account
    {
        $dto = $this->findOneBy(['accountId' => (string) $accountId]);

        return $this->dtoToEntity($dto);
    }

    /**
     * {@inheritdoc}
     */
    public function getAll() : array
    {
        $queryBuilder = $this->createQueryBuilder('a');

        $dtos = $queryBuilder
            ->select('a')
            ->getQuery()
            ->getResult();

        return $this->dtosToEntities($dtos);
    }

    /**
     * Get all accounts by an AccountType
     *
     * @param AccountType
     * @return array
     */
    public function getAllByType(AccountType $type) : array
    {
        $queryBuilder = $this->createQueryBuilder('a');

        $dtos = $queryBuilder
            ->select('a')
            ->where('a.type = :type')
            ->setParameter('type', (string) $type)
            ->getQuery()
            ->getResult();

        return $this->dtosToEntities($dtos);
    }

    private function dtosToEntities($dtos)
    {
        $accounts = [];

        foreach ($dtos as $dto) {
            $accounts[] = $this->dtoToEntity($dto);
        }

        return $accounts;
    }

    private function dtoToEntity($dto)
    {
        $account = Account::withId(
            AccountId::fromString($dto->getAccountId()),
            AccountType::fromString($dto->getType()),
            $dto->getTitle()
        );

        return $account;
    }
}
<commit_msg>Use query builder to get accounts<commit_after><?php

namespace PerFi\PerFiBundle\Repository;

use Doctrine\ORM\EntityRepository;
use PerFi\Domain\Account\Account;
use PerFi\Domain\Account\AccountId;
use PerFi\Domain\Account\AccountRepository as AccountRepositoryInterface;
use PerFi\Domain\Account\AccountType;
use PerFi\PerFiBundle\Entity\Account as DtoAccount;
use PerFi\PerFiBundle\Factory\AccountFactory;

/**
 * AccountRepository
 */
class AccountRepository extends EntityRepository
    implements AccountRepositoryInterface
{
    /**
     * {@inheritdoc}
     */
    public function add(Account $account)
    {
        $entity = new DtoAccount();
        $entity->setAccountId((string) $account->id());
        $entity->setTitle($account->title());
        $entity->setType((string) $account->type());

        $em = $this->getEntityManager();
        $em->persist($entity);
        $em->flush();
    }

    /**
     * Get an account by it's ID
     *
     * @param AccountId $accountId
     * @return Account
     */
    public function get(AccountId $accountId) : Account
    {
        $dto = $this->findOneBy(['accountId' => (string) $accountId]);

        return $this->dtoToEntity($dto);
    }

    /**
     * {@inheritdoc}
     */
    public function getAll() : array
    {
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();

        $statement = $qb->select(
            'a.account_id AS id', 'a.title', 'a.type'
        )
        ->from('account', 'a')
        ->execute();

        return $this->mapToEntities($statement);
    }

    /**
     * Get all accounts by an AccountType
     *
     * @param AccountType
     * @return array
     */
    public function getAllByType(AccountType $type) : array
    {
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();

        $statement = $qb->select(
            'a.account_id AS id', 'a.title', 'a.type'
        )
        ->from('account', 'a')
        ->where('a.type = :type')
        ->setParameter('type', $type)
        ->execute();

        return $this->mapToEntities($statement);
    }

    private function mapToEntities($statement)
    {
        $accounts = [];

        while ($row = $statement->fetch()) {
            $accounts[] = $this->mapToEntity($row);
        }

        return $accounts;
    }

    private function mapToEntity(array $row) : Account
    {
        return AccountFactory::fromArray($row);
    }
}

{'fraction_non_alphanumeric': 0.1473186768029587, 'fraction_numerical': 0.0, 'mean_word_length': 2.3503097040605643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '1885155', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1423, 'n_words': 274}