<?php
namespace App\Repository;
use App\Entity\OrderWarehouse;
use App\Entity\Service;
use App\Entity\User;
use App\Entity\Warehouse;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @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);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newEncodedPassword);
$this->_em->persist($user);
$this->_em->flush();
}
public function getQueryBuilderUserByOrderWarehouse(OrderWarehouse $orderWarehouse): QueryBuilder
{
return $this->createQueryBuilder('user')
->innerJoin('user.create_orders', 'ordersWarehouse')
->andWhere('ordersWarehouse = :orderWarehouse')
->setParameter('orderWarehouse', $orderWarehouse);
}
public function findUsersEmailByWarehouse(Warehouse $orderWarehouse): array
{
$qb = $this->createQueryBuilder('user')
->innerJoin('user.warehouses', 'ordersWarehouse')
->andWhere('ordersWarehouse = :orderWarehouse')
->setParameter('orderWarehouse', $orderWarehouse);
return $qb->getQuery()->execute();
}
public function findUsersByRole(string $role): array
{
$qb = $this->createQueryBuilder('user')
->where('user.roles LIKE :role')
->setParameter('role', '%'.$role.'%');
return $qb->getQuery()->execute();
}
public function findUsersEmailByService(?Service $service): array
{
$qb = $this->createQueryBuilder('user')
->innerJoin('user.services', 'services')
->andWhere('services IN (:service)')
->setParameter('service', $service);
return $qb->getQuery()->execute();
}
public function findUserByEmail(string $email): ?User
{
return $this->createQueryBuilder('user')
->where('user.email = :email')
->setParameter('email', $email)
->getQuery()
->getOneOrNullResult();
}
public function persist(User $user): void
{
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}
}