<?php
namespace App\Repository;
use App\Entity\Agency;
use App\Entity\Estimate;
use App\Entity\User;
use DateTime;
use DateTimeInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use InvalidArgumentException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use function get_class;
/**
* @extends ServiceEntityRepository<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct (ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* @param User $entity
* @param bool $flush
*/
public function add (User $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @param Agency[] $ids
* @return User[]
*/
public function findByAgencies (array $ids): array
{
$qb = $this->createQueryBuilder('u');
$qb
->distinct()
->join('u.agencies', 'a')
->andWhere($qb->expr()->in('a.id', $ids))
->andWhere($qb->expr()->eq('u.active', true));
return $qb->getQuery()->getResult();
}
/**
* @param string | int $id
* @return User[]
*/
public function findByAgency ($id): array
{
$qb = $this->createQueryBuilder('u');
$qb
->distinct()
->join('u.agencies', 'a')
->andWhere($qb->expr()->eq('a.id', $id));
return $qb->getQuery()->getResult();
}
/**
* @param string $role
* @return User[]
*/
public function findByRole (string $role): array
{
if (!array_key_exists($role, User::getRolesList())) return [];
$qb = $this->createQueryBuilder('u');
$qb->andWhere($qb->expr()->like('u.roles', "'%" . $role . "%'"));
return $qb->getQuery()->getResult();
}
/**
* @param string | null $role
* @param array $agencies
* @return User[]
*/
public function findByRoleAndAgencies (string $role = null, array $agencies = []): array
{
$qb = $this->createQueryBuilder('u');
if ($role && array_key_exists($role, User::getRolesList())) $qb->andWhere($qb->expr()->like('u.roles', "'%" . $role . "%'"));
if (count($agencies)) {
$qb
->leftJoin("u.agencies", "a")
->andWhere($qb->expr()->in("a.id", $agencies));
}
$qb->addOrderBy("u.firstname");
$qb->addOrderBy("u.lastname");
return $qb->getQuery()->getResult();
}
/**
* @param string | int $id
* @return User[]
*/
public function findCommercialByAgency ($id): array
{
$qb = $this->createQueryBuilder('u');
$qb
->distinct()
->join('u.agencies', 'a')
->andWhere($qb->expr()->eq('a.id', $id));
return $qb->getQuery()->getResult();
}
/**
* @param array $ids
* @return User[]
*/
public function findTechnicienByAgencies (array $ids): array
{
// dd($ids);
$qb = $this->createQueryBuilder('u');
$qb
->distinct()
->andWhere($qb->expr()->eq('u.active', true))
->join('u.agencies', 'a')
->andWhere($qb->expr()->in('a.id', $ids))
->andWhere($qb->expr()->like("u.roles", $qb->expr()->literal('%"' . User::ROLE_TECHNICIEN . '"%')));
return $qb->getQuery()->getResult();
}
/**
* @param User $entity
* @param bool $flush
*/
public function remove (User $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword (PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->_em->persist($user);
$this->_em->flush();
}
}