<?php

namespace Alchemy\Phrasea\Model\Repositories;

use Alchemy\Phrasea\Model\Entities\ApiAccount;
use Alchemy\Phrasea\Model\Entities\User;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr;

/**
 * ApiOauthCodeRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ApiOauthCodeRepository extends EntityRepository
{
    public function findByAccount(ApiAccount $account)
    {
        $qb = $this->createQueryBuilder('c');
        $qb->where($qb->expr()->eq('c.account', ':account'));
        $qb->setParameter(':account', $account);

        return $qb->getQuery()->getResult();
    }

    public function findByUserAccount(User $user, $limit = 50)
    {
        $qb = $this->createQueryBuilder('c');
        $qb->innerJoin('c.account', 'acc', Expr\Join::WITH, $qb->expr()->eq('acc.user', ':user'));
        $qb->setMaxResults($limit)
            ->orderBy('c.created', 'DESC');
        $qb->setParameter(':user', $user);

        return $qb->getQuery()->getResult();
    }
}
