<?php
namespace Roothirsch\CoreBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Roothirsch\CoreBundle\Entity\User;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
class UserRepository extends ServiceEntityRepository
{
public function __construct(\Doctrine\Persistence\ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* {@inheritdoc}
*/
public function findOneByApiToken($apiToken)
{
$apiUser = $this->findOneBy(['apiToken' => $apiToken]);
if (!$apiUser) {
throw new AuthenticationCredentialsNotFoundException();
}
return $apiUser;
}
/**
* @param string $email
*
* @return null|User
*/
public function findUserByEmail($email)
{
return $this->findOneBy(['email' => $email]);
}
/**
* @param string $securityToken
*
* @return null|User
*/
public function findUserBySecurityToken($securityToken)
{
return $this->findOneBy(['securityToken' => $securityToken]);
}
/**
* @param string $registrationToken
*
* @return null|User
*/
public function findUserByRegistrationToken($registrationToken)
{
return $this->findOneBy(['registrationToken' => $registrationToken]);
}
/**
* Finds coworkers for a user.
*
* @param User $user
*
* @return array|mixed
*/
public function findCoworkersForUser(User $user)
{
$company = $user->getCompany();
if (!is_object($company)) {
return [];
}
$dql = 'SELECT u FROM AppBundle:User AS u JOIN u.company c WHERE c.id = :company AND u.id != :user ORDER BY u.lastName';
$em = $this->getEntityManager();
$query = $em->createQuery($dql);
$query->setParameters(
[
'company' => $company->getId(),
'user' => $user->getId(),
]
);
return $query->getResult();
}
}