src/Manager/CustomerManager.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Entity\AbstractOperation;
  4. use App\Entity\AbstractUser;
  5. use App\Entity\CappingOperation;
  6. use App\Entity\Customer;
  7. use App\Entity\Operation;
  8. use App\Mailer\SecurityMailer;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\ORM\EntityRepository;
  11. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  12. /**
  13.  * Class CustomerManager.
  14.  */
  15. final class CustomerManager
  16. {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $manager;
  21.     /**
  22.      * @var EntityRepository
  23.      */
  24.     public $repository;
  25.     /**
  26.      * @var string
  27.      */
  28.     private $pwdRequestTTL;
  29.     /**
  30.      * @var TokenGeneratorInterface
  31.      */
  32.     private $tokenGenerator;
  33.     /**
  34.      * @var SecurityMailer
  35.      */
  36.     private $mailer;
  37.     /**
  38.      * CustomerManager constructor.
  39.      *
  40.      * @param string                  $pwdRequestTTL
  41.      * @param EntityManagerInterface  $manager
  42.      * @param TokenGeneratorInterface $tokenGenerator
  43.      * @param SecurityMailer          $mailer
  44.      */
  45.     public function __construct($pwdRequestTTLEntityManagerInterface $managerTokenGeneratorInterface $tokenGeneratorSecurityMailer $mailer)
  46.     {
  47.         $this->manager $manager;
  48.         $this->pwdRequestTTL $pwdRequestTTL;
  49.         $this->tokenGenerator $tokenGenerator;
  50.         $this->mailer $mailer;
  51.         $this->repository $this->manager->getRepository(Customer::class);
  52.     }
  53.     /**
  54.      * @param string $email
  55.      *
  56.      * @return Customer|object|null
  57.      */
  58.     public function findOneByEmail($email)
  59.     {
  60.         return $this->repository->findOneBy(['email' => $email]);
  61.     }
  62.     /**
  63.      * @param AbstractUser $user
  64.      *
  65.      * @throws \Twig_Error_Loader
  66.      * @throws \Twig_Error_Runtime
  67.      * @throws \Twig_Error_Syntax
  68.      */
  69.     public function requestPasswordReset(AbstractUser $user)
  70.     {
  71.         if (!$user->isPasswordRequestNonExpired($this->pwdRequestTTL) || null === $user->getConfirmationToken()) {
  72.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  73.             $user->setPasswordRequestedAt(new \DateTime());
  74.             $this->manager->flush();
  75.         }
  76.         $this->mailer->sendClientResetPwdUrl($user);
  77.     }
  78.     /**
  79.      * Get user if token is valid.
  80.      *
  81.      * @param string $token
  82.      *
  83.      * @return Customer|null
  84.      */
  85.     public function findUserByValidToken($token)
  86.     {
  87.         $user $this->repository->findOneBy(['confirmationToken' => $token]);
  88.         if (!$user || !$user->isPasswordRequestNonExpired($this->pwdRequestTTL)) {
  89.             return null;
  90.         }
  91.         return $user;
  92.     }
  93.     /**
  94.      * Reset user password.
  95.      *
  96.      * @param AbstractUser $user
  97.      * @param string       $newPassword
  98.      */
  99.     public function resetPassword(AbstractUser $user$newPassword)
  100.     {
  101.         $user
  102.             ->setPlainPassword($newPassword)
  103.             ->setConfirmationToken(null);
  104.         $this->manager->flush();
  105.     }
  106.     /**
  107.      * Flush.
  108.      */
  109.     public function flush()
  110.     {
  111.         $this->manager->flush();
  112.     }
  113.     /**
  114.      * @param $customerId
  115.      * @param $operationId
  116.      * @return array
  117.      *
  118.      * Function is injected in twig directly
  119.      */
  120.     public function getPartMaxByCustomerAndOperation($customerId$operationId): array
  121.     {
  122.         $operation $this->manager->getRepository(AbstractOperation::class)->findOneBy([
  123.             'id' => $operationId,
  124.         ]);
  125.         if ($operation instanceof Operation) {
  126.             $eligibility =  $this->manager->getRepository(Operation::class)->getEligibilityByOperation($operationId);
  127.         }
  128.         elseif ($operation instanceof CappingOperation) {
  129.             $eligibility =  $this->manager->getRepository(CappingOperation::class)->getEligibilityByOperation($operationId);
  130.         }
  131.         return [
  132.             'part_max' => $this->manager->getRepository(Customer::class)->getPartMaxByCustomerAndOperation($customerId$operationId),
  133.             'eligibility' => $eligibility
  134.         ];
  135.     }
  136. }