<?php

/*
 * This file is part of Phraseanet
 *
 * (c) 2005-2014 Alchemy
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Alchemy\Phrasea\Model\Repositories;

use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Entities\UsrAuthProvider;
use Doctrine\ORM\EntityRepository;

/**
 * UsrAuthProvider
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class UsrAuthProviderRepository extends EntityRepository
{
    /**
     * @param User $user
     * @return UsrAuthProvider[]
     */
    public function findByUser(User $user)
    {
        $dql = 'SELECT u
                FROM Phraseanet:UsrAuthProvider u
                WHERE u.user = :usrId';

        $params = ['usrId' => $user->getId()];

        $query = $this->_em->createQuery($dql);
        $query->setParameters($params);

        return $query->getResult();
    }

    /**
     * @param $providerId
     * @param $distantId
     * @return UsrAuthProvider|null
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findWithProviderAndId($providerId, $distantId)
    {
        $dql = 'SELECT u
                FROM Phraseanet:UsrAuthProvider u
                WHERE u.provider = :providerId AND u.distant_id = :distantId';

        $params = ['providerId' => $providerId, 'distantId' => $distantId];

        $query = $this->_em->createQuery($dql);
        $query->setParameters($params);

        return $query->getOneOrNullResult();
    }
}
