Skip to content
Permalink
Browse files

refactor: Removed not needed trait and refactor use of `getenv` funct…

…ion.
  • Loading branch information...
tarlepp committed Jun 30, 2019
1 parent 639a946 commit 6b48266e37cfa26100a7a2e412a0e4f374c883be
Showing with 65 additions and 73 deletions.
  1. +0 −66 src/Repository/Traits/LoadUserByUserNameTrait.php
  2. +65 −7 src/Repository/UserRepository.php

This file was deleted.

@@ -9,7 +9,9 @@
namespace App\Repository;
use App\Entity\User as Entity;
use App\Repository\Traits\LoadUserByUserNameTrait;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use function array_key_exists;
/**
* Class UserRepository
@@ -30,9 +32,6 @@
*/
class UserRepository extends BaseRepository
{
// Traits
use LoadUserByUserNameTrait;
/**
* @var string
*/
@@ -45,6 +44,25 @@ class UserRepository extends BaseRepository
*/
protected static $searchColumns = ['username', 'firstName', 'lastName', 'email'];
/**
* @var string
*/
private $environment = 'dev';
/**
* @required
*
* @param string $environment
*
* @return UserRepository
*/
public function setEnvironment(string $environment): self
{
$this->environment = $environment;
return $this;
}
/**
* Method to check if specified username is available or not.
*
@@ -53,7 +71,7 @@ class UserRepository extends BaseRepository
*
* @return bool
*
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws NonUniqueResultException
*/
public function isUsernameAvailable(string $username, ?string $id = null): bool
{
@@ -68,21 +86,61 @@ public function isUsernameAvailable(string $username, ?string $id = null): bool
*
* @return bool
*
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws NonUniqueResultException
*/
public function isEmailAvailable(string $email, ?string $id = null): bool
{
return $this->isUnique('email', $email, $id);
}
/**
* Loads the user for the given username.
*
* This method must throw UsernameNotFoundException if the user is not found.
*
* Method is override for performance reasons see link below.
*
* @link http://symfony2-document.readthedocs.org/en/latest/cookbook/security/entity_provider.html
* #managing-roles-in-the-database
*
* @psalm-suppress ImplementedReturnTypeMismatch
*
* @param string $username The username
*
* @return Entity|null
*
* @throws ORMException
*/
public function loadUserByUsername($username): ?Entity
{
static $cache = [];
if (!array_key_exists($username, $cache) || $this->environment === 'test') {
// Build query
$query = $this
->createQueryBuilder('u')
->select('u, g, r')
->leftJoin('u.userGroups', 'g')
->leftJoin('g.role', 'r')
->where('u.id = :username OR u.username = :username OR u.email = :email')
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery();
$cache[$username] = $query->getOneOrNullResult() ?? false;
}
return $cache[$username] instanceof Entity ? $cache[$username] : null;
}
/**
* @param string $column Column to check
* @param string $value Value of specified column
* @param string|null $id User id to ignore
*
* @return bool
*
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws NonUniqueResultException
*/
private function isUnique(string $column, string $value, ?string $id = null): bool
{

0 comments on commit 6b48266

Please sign in to comment.
You can’t perform that action at this time.