<?php

namespace Alchemy\Phrasea\Model\Repositories;

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

/**
 * ApiOauthTokenRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ApiOauthTokenRepository extends EntityRepository
{
    /**
     * @param ApiAccount $account
     * @return ApiOauthToken|null
     */
    public function findDeveloperToken(ApiAccount $account)
    {
        $qb = $this->createQueryBuilder('tok');
        $qb->innerJoin('tok.account', 'acc', Expr\Join::WITH, $qb->expr()->eq('acc.id', ':acc_id'));
        $qb->innerJoin('acc.application', 'app', Expr\Join::WITH, $qb->expr()->orx(
            $qb->expr()->eq('app.creator', 'acc.user'),
            $qb->expr()->isNull('app.creator')
        ));
        $qb->where($qb->expr()->isNull('tok.expires'));
        $qb->orderBy('tok.created', 'DESC');
        $qb->setParameter(':acc_id', $account->getId());

        /*
         * @note until we add expiration token, there is no way to distinguish a developer issued token from
         * a connection process issued token.
         */
        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * @param ApiAccount $account
     * @return ApiOauthToken[]
     */
    public function findOauthTokens(ApiAccount $account)
    {
        $qb = $this->createQueryBuilder('tok');
        $qb->where($qb->expr()->eq('tok.account', ':acc'));
        $qb->setParameter(':acc', $account);

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