Skip to content

Commit

Permalink
FRW-9013 Fixed performance degradation issue on customer registration…
Browse files Browse the repository at this point in the history
…. (#11221)

FRW-9013 Fixed performance degradation issue on customer registration.
  • Loading branch information
kraal-spryker authored Dec 2, 2024
1 parent b31057d commit 7b70625
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 17 deletions.
30 changes: 14 additions & 16 deletions src/Spryker/Zed/Customer/Business/Customer/EmailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@
namespace Spryker\Zed\Customer\Business\Customer;

use Spryker\Zed\Customer\Dependency\Service\CustomerToUtilValidateServiceInterface;
use Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface;
use Spryker\Zed\Customer\Persistence\CustomerRepositoryInterface;

class EmailValidator implements EmailValidatorInterface
{
/**
* @var int
*/
protected const COL_EMAIL_MAX_ALLOWED_LENGHT = 100;
protected const COL_EMAIL_MAX_ALLOWED_LENGTH = 100;

/**
* @var \Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface
* @var \Spryker\Zed\Customer\Dependency\Service\CustomerToUtilValidateServiceInterface
*/
protected $queryContainer;
protected $utilValidateService;

/**
* @var \Spryker\Zed\Customer\Dependency\Service\CustomerToUtilValidateServiceInterface
* @var \Spryker\Zed\Customer\Persistence\CustomerRepositoryInterface
*/
protected $utilValidateService;
protected CustomerRepositoryInterface $customerRepository;

/**
* @param \Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface $queryContainer
* @param \Spryker\Zed\Customer\Dependency\Service\CustomerToUtilValidateServiceInterface $utilValidateService
* @param \Spryker\Zed\Customer\Persistence\CustomerRepositoryInterface $customerRepository
*/
public function __construct(CustomerQueryContainerInterface $queryContainer, CustomerToUtilValidateServiceInterface $utilValidateService)
{
$this->queryContainer = $queryContainer;
public function __construct(
CustomerToUtilValidateServiceInterface $utilValidateService,
CustomerRepositoryInterface $customerRepository
) {
$this->utilValidateService = $utilValidateService;
$this->customerRepository = $customerRepository;
}

/**
Expand All @@ -55,11 +57,7 @@ public function isFormatValid($email)
*/
public function isEmailAvailableForCustomer($email, $idCustomer)
{
$customerEntity = $this->queryContainer
->queryCustomerByEmailApartFromIdCustomer($email, $idCustomer)
->findOne();

return ($customerEntity === null);
return $this->customerRepository->isEmailAvailableForCustomer($email, $idCustomer);
}

/**
Expand All @@ -69,6 +67,6 @@ public function isEmailAvailableForCustomer($email, $idCustomer)
*/
public function isEmailLengthValid(string $email): bool
{
return mb_strlen($email) <= static::COL_EMAIL_MAX_ALLOWED_LENGHT;
return mb_strlen($email) <= static::COL_EMAIL_MAX_ALLOWED_LENGTH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ public function createCustomerOrderHydrator()
public function createEmailValidator()
{
return new EmailValidator(
$this->getQueryContainer(),
$this->getUtilValidateService(),
$this->getRepository(),
);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Spryker/Zed/Customer/CustomerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class CustomerConfig extends AbstractBundleConfig
*/
public const PATTERN_LAST_NAME = '/^[^:\/<>]+$/';

/**
* @var bool
*/
protected const IS_CUSTOMER_EMAIL_VALIDATION_CASE_SENSITIVE = true;

/**
* @var int
*/
Expand Down Expand Up @@ -355,6 +360,19 @@ public function getCustomerInvalidSalutationErrorCode(): int
return static::ERROR_CODE_CUSTOMER_INVALID_SALUTATION;
}

/**
* Specification:
* - Returns whether customer email validation should be case sensitive.
*
* @api
*
* @return bool
*/
public function isCustomerEmailValidationCaseSensitive(): bool
{
return static::IS_CUSTOMER_EMAIL_VALIDATION_CASE_SENSITIVE;
}

/**
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function queryCustomerByEmail($email)
* @api
*
* @inheritDoc
*
* @deprecated Use {@link \Spryker\Zed\Customer\Persistence\CustomerRepository::isEmailAvailableForCustomer()} instead.
*/
public function queryCustomerByEmailApartFromIdCustomer($email, $exceptIdCustomer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function queryCustomerByEmail($email);
*
* @api
*
* @deprecated Use {@link \Spryker\Zed\Customer\Persistence\CustomerRepository::isEmailAvailableForCustomer()} instead.
*
* @param string $email
* @param int $exceptIdCustomer
*
Expand Down
15 changes: 15 additions & 0 deletions src/Spryker/Zed/Customer/Persistence/CustomerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ public function findCustomerByCriteria(CustomerCriteriaTransfer $customerCriteri
->mapCustomerEntityToCustomer($customerEntity->toArray());
}

/**
* @param string $email
* @param int|null $exceptIdCustomer
*
* @return bool
*/
public function isEmailAvailableForCustomer(string $email, ?int $exceptIdCustomer): bool
{
return $this->getFactory()
->createSpyCustomerQuery()
->filterByEmail($email, Criteria::EQUAL, $this->getFactory()->getConfig()->isCustomerEmailValidationCaseSensitive())
->filterByIdCustomer($exceptIdCustomer, Criteria::NOT_EQUAL)
->count() === 0;
}

/**
* @param \Generated\Shared\Transfer\AddressCriteriaFilterTransfer $addressCriteriaFilterTransfer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ public function getAddressesByCriteria(AddressCriteriaFilterTransfer $addressCri
* @return \Generated\Shared\Transfer\CustomerTransfer|null
*/
public function findCustomerByCriteria(CustomerCriteriaTransfer $customerCriteriaTransfer): ?CustomerTransfer;

/**
* @param string $email
* @param int|null $exceptIdCustomer
*
* @return bool
*/
public function isEmailAvailableForCustomer(string $email, ?int $exceptIdCustomer): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Spryker\Zed\Customer\Persistence\Propel;

use Orm\Zed\Customer\Persistence\Base\SpyCustomerQuery as BaseSpyCustomerQuery;
use Orm\Zed\Customer\Persistence\Map\SpyCustomerTableMap;
use Propel\Runtime\ActiveQuery\Criteria;

/**
Expand Down Expand Up @@ -38,4 +39,24 @@ public static function create($modelAlias = null, ?Criteria $criteria = null, $w

return $query;
}

/**
* @param list<string>|string|null $email
* @param string $comparison
* @param bool $ignoreCase
*
* @return self
*/
public function filterByEmail($email = null, $comparison = Criteria::EQUAL, bool $ignoreCase = true): self
{
$query = parent::filterByEmail($email, $comparison);

if ($ignoreCase === false) {
/** @var \Propel\Runtime\ActiveQuery\Criterion\BasicCriterion $criterion */
$criterion = $query->getCriterion(SpyCustomerTableMap::COL_EMAIL);
$criterion->setIgnoreCase(false);
}

return $query;
}
}

0 comments on commit 7b70625

Please sign in to comment.