<?php
declare(strict_types = 1);
namespace App\Domain\Repository;
use App\Domain\Entity\User;
use App\Domain\Repository\Interfaces\UserRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface, UserRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* @param string $username
*
* @return User|null
*
* @throws NonUniqueResultException
*/
public function getUserByUsername(string $username): ?User
{
return $this->createQueryBuilder('user')
->where('user.username = :username')
->setParameter('username', $username)
->setCacheable(true)
->getQuery()
->getOneOrNullResult();
}
/**
* @param string $mail
*
* @return User|null
*
* @throws NonUniqueResultException
*/
public function getUserByEmail($mail): ?User
{
return $this->createQueryBuilder('user')
->where('user.email = :email')
->setParameter('email', $mail)
->setCacheable(true)
->getQuery()
->getOneOrNullResult();
}
/**
* @param string $username
* @param string $mail
*
* @return mixed
*
* @throws NonUniqueResultException
*/
public function getUserByUsernameAndEmail(string $username, string $mail)
{
return $this->createQueryBuilder('user')
->where('user.username = :username AND user.email = :email')
->setParameter('username', $username)
->setParameter('email', $mail)
->getQuery()
->getOneOrNullResult();
}
/**
* @return int|mixed|string
*/
public function getAllUser()
{
return $this->createQueryBuilder('u')
->orderBy('u.createdAt', 'DESC')
->getQuery()
->getResult()
;
}
/**
* @param $role
* @return mixed
*/
public function getAllUserByRoles($role)
{
return $this->createQueryBuilder('user')
->where('user.role LIKE :role')
->setParameter('role', '%"'.$role.'"%')
->orderBy('user.createdAt', 'DESC')
->getQuery()
->getResult()
;
}
/**
* @param string $token
*
* @return mixed
*
* @throws NonUniqueResultException
*/
public function getUserByToken(string $token)
{
return $this->createQueryBuilder('user')
->where('user.token = :token')
->setParameter('token', $token)
->getQuery()
->getOneOrNullResult();
}
/**
* @param string $id
*
* @return mixed
*
* @throws NonUniqueResultException
*/
public function getUserById(string $id): ?User
{
return$this->createQueryBuilder('u')
->where('u.id = :id')
->setParameter('id', $id)
->setCacheable(true)
->getQuery()
->getOneOrNullResult()
;
}
/**
* @param string $username
*
* @return mixed
*
* @throws NonUniqueResultException
*/
public function loadUserByUsername(string $username)
{
return $this->createQueryBuilder('user')
->where('user.username = :username')
->setParameter('username', $username)
->setCacheable(true)
->getQuery()
->getOneOrNullResult();
}
/**
* @param string $id
* @param bool $value
* @return \Doctrine\ORM\Query
*/
public function disableUserById(string $id, bool $value)
{
return $this->createQueryBuilder('user')
->update('App:User', 'u')
->set('u.status', '?1')
->where('u.id = ?2')
->setParameter(1, $value)
->setParameter(2, $id)
->getQuery()
->execute();
}
/**
* @param $user
*
* @throws ORMException
*
* @throws OptimisticLockException
*/
public function save($user)
{
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function flush()
{
$this->getEntityManager()->flush();
}
/**
* @return void
*
* @throws ORMException
* @throws OptimisticLockException
*/
public function update(): void
{
$this->getEntityManager()->flush();
}
/**
* @param $user
* @return void
*
* @throws ORMException
* @throws OptimisticLockException
*/
public function updateUser($user): void
{
$this->getEntityManager()->flush();
}
/**
* @param string $id
*
* @throws NonUniqueResultException
* @throws ORMException
* @throws OptimisticLockException
*/
public function deleteUser(string $id)
{
$user = $this->getUserById($id);
$this->getEntityManager()->remove($user);
$this->getEntityManager()->flush();
}
}