<?php
namespace App\Manager;
use App\Entity\AbstractOperation;
use App\Entity\AbstractUser;
use App\Entity\CappingOperation;
use App\Entity\Customer;
use App\Entity\Operation;
use App\Mailer\SecurityMailer;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
/**
* Class CustomerManager.
*/
final class CustomerManager
{
/**
* @var EntityManagerInterface
*/
private $manager;
/**
* @var EntityRepository
*/
public $repository;
/**
* @var string
*/
private $pwdRequestTTL;
/**
* @var TokenGeneratorInterface
*/
private $tokenGenerator;
/**
* @var SecurityMailer
*/
private $mailer;
/**
* CustomerManager constructor.
*
* @param string $pwdRequestTTL
* @param EntityManagerInterface $manager
* @param TokenGeneratorInterface $tokenGenerator
* @param SecurityMailer $mailer
*/
public function __construct($pwdRequestTTL, EntityManagerInterface $manager, TokenGeneratorInterface $tokenGenerator, SecurityMailer $mailer)
{
$this->manager = $manager;
$this->pwdRequestTTL = $pwdRequestTTL;
$this->tokenGenerator = $tokenGenerator;
$this->mailer = $mailer;
$this->repository = $this->manager->getRepository(Customer::class);
}
/**
* @param string $email
*
* @return Customer|object|null
*/
public function findOneByEmail($email)
{
return $this->repository->findOneBy(['email' => $email]);
}
/**
* @param AbstractUser $user
*
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
*/
public function requestPasswordReset(AbstractUser $user)
{
if (!$user->isPasswordRequestNonExpired($this->pwdRequestTTL) || null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
$user->setPasswordRequestedAt(new \DateTime());
$this->manager->flush();
}
$this->mailer->sendClientResetPwdUrl($user);
}
/**
* Get user if token is valid.
*
* @param string $token
*
* @return Customer|null
*/
public function findUserByValidToken($token)
{
$user = $this->repository->findOneBy(['confirmationToken' => $token]);
if (!$user || !$user->isPasswordRequestNonExpired($this->pwdRequestTTL)) {
return null;
}
return $user;
}
/**
* Reset user password.
*
* @param AbstractUser $user
* @param string $newPassword
*/
public function resetPassword(AbstractUser $user, $newPassword)
{
$user
->setPlainPassword($newPassword)
->setConfirmationToken(null);
$this->manager->flush();
}
/**
* Flush.
*/
public function flush()
{
$this->manager->flush();
}
/**
* @param $customerId
* @param $operationId
* @return array
*
* Function is injected in twig directly
*/
public function getPartMaxByCustomerAndOperation($customerId, $operationId): array
{
$operation = $this->manager->getRepository(AbstractOperation::class)->findOneBy([
'id' => $operationId,
]);
if ($operation instanceof Operation) {
$eligibility = $this->manager->getRepository(Operation::class)->getEligibilityByOperation($operationId);
}
elseif ($operation instanceof CappingOperation) {
$eligibility = $this->manager->getRepository(CappingOperation::class)->getEligibilityByOperation($operationId);
}
return [
'part_max' => $this->manager->getRepository(Customer::class)->getPartMaxByCustomerAndOperation($customerId, $operationId),
'eligibility' => $eligibility
];
}
}