← Back
Editing: UserService.php
<?php namespace App\Service; use App\Contract\PersistableUserInterface; use App\Entity\Solution; use App\Entity\User; use App\Repository\UserRepository; use App\Service\Shared\RandomColorGenerator; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Address; use Symfony\Component\Security\Core\Security; class UserService { public function __construct( private readonly EntityManagerInterface $em, private readonly UserRepository $userRepository, private readonly MailerInterface $mailer, private readonly SolutionService $solutionService, private readonly AgentService $agentService, private readonly Security $security ) { } public function createUser(User $user): void { if ($this->checkUserExist($user)) { throw new \Exception(sprintf('Address %s already exists in database', $user->getEmail())); } $this->em->persist($user); } /** * This method check if the email is associated to an user. */ public function checkUserExist(User $user): bool { $emailExist = $this->userRepository->findOneBy(['email' => $user->getEmail()]); if ($emailExist) { return true; } return false; } /** * @throws TransportExceptionInterface */ public function sendNewPassword(User $user, string $rawPassword): void { $email = (new TemplatedEmail()) ->from($this->solutionService->getCurrent()->getEmail()) ->to(new Address($user->getEmail())) ->subject('Réinitialisation de votre mot de passe') // path of the Twig template to render ->htmlTemplate('emails/new_password.html.twig') // pass variables (name => value) to the template ->context( [ 'user' => $user, 'rawPassword' => $rawPassword, ] ); $this->mailer->send($email); } public function randomPassword(): string { return bin2hex(openssl_random_pseudo_bytes(4)); } /** * @throws \Exception */ public function statsByAgent(User $agent): array { $this->agentService->setAgent($agent); $controlesDone = count($this->agentService->getControles()); if (in_array('ROLE_AGENT', $agent->getArrayRoles())) { $agentFrequency = $agent->getAgentFrequencyBySolution($this->solutionService->getCurrent()); return [ 'remainingControles' => $this->agentService->getRemainingControls(), 'controlesDone' => $controlesDone, 'totalControles' => $agentFrequency->getFrequencyValue(), 'pourcentage' => ($controlesDone / $agentFrequency->getFrequencyValue()) * 100, ]; } return [ 'remainingControles' => 0, 'controlesDone' => $controlesDone, 'totalControles' => 0, 'pourcentage' => 100, ]; } /** * @return array{controlsDone: int, totalControles: int} * * @throws \Exception */ public function statsByManager( PersistableUserInterface $manager, bool $forReport = false, ?Solution $solution = null ): array { $controlesDone = 0; $totalControles = 0; if (!$forReport) { $agents = $this->getAgentsForCurrentSolution($manager); } else { $agents = $this->getAgentsForSolution($solution, $manager); } /** @var User $agent */ foreach ($agents as $agent) { $stats = $this->statsByAgent($agent); $totalControles += $stats['totalControles']; $controlesDone += $stats['controlesDone']; } return ['controlsDone' => $controlesDone, 'totalControles' => $totalControles]; } /** * @return User[] */ public function getAgentsForCurrentSolution(?PersistableUserInterface $manager = null): array { return $this->getAgentsForSolution($this->solutionService->getCurrent(), $manager); } /** * @return User[] */ public function getAgentsForSolution(Solution $solution, ?PersistableUserInterface $manager = null): array { $agents = []; if (!$manager) { /** @var PersistableUserInterface $manager */ $manager = $this->security->getUser(); } /* Manager can do controls */ $agents[] = $manager; /** @var User $agent */ foreach ($manager->getAgents() as $agent) { foreach ($agent->getSolutions() as $agentSolution) { if ($solution->getId() === $agentSolution->getId()) { $agents[] = $agent; } } } return $agents; } /** * @throws TransportExceptionInterface */ public function sendNewAccountEmail(PersistableUserInterface $user, ?Solution $solution = null): void { if (!$solution && $this->solutionService) { $solution = $this->solutionService->getCurrent(); $emailFrom = $solution->getEmail(); } else { $emailFrom = 'contact@arithmetic.fr'; } $email = (new TemplatedEmail()) ->from($emailFrom) ->to(new Address($user->getEmail())) ->subject('Création de compte') // path of the Twig template to render ->htmlTemplate('emails/creation_compte.html.twig') // pass variables (name => value) to the template ->context( [ 'user' => $user, ] ); $this->mailer->send($email); } /** * @throws \Exception */ public function setColorsToNewManagers(): void { /** @var User[] $managers */ $managers = $this->userRepository->getManagersWithoutColor(); $countManagersWithoutColor = count($managers); $colors = RandomColorGenerator::many($countManagersWithoutColor); foreach ($managers as $key => $manager) { $manager->setColor($colors[$key]); $this->em->persist($manager); } $this->em->flush(); } }
Save File
Cancel