← Back
Editing: AgentService.php
<?php namespace App\Service; use App\Entity\AgentFrequency; use App\Entity\Periode; use App\Entity\Solution; use App\Entity\User; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Security\Core\User\UserInterface; class AgentService { private ?User $agent = null; private EntityManagerInterface $entityManager; private array $controles = []; private Utils $utils; private SolutionService $solutionService; private ?AgentFrequency $agentFrequency = null; public function __construct( EntityManagerInterface $entityManager, Utils $utils, SolutionService $solutionService ) { $this->entityManager = $entityManager; $this->utils = $utils; $this->solutionService = $solutionService; } /** * @throws \Exception */ public function getTotalControles(): int { return $this->getControles()->count(); } /** * @return float|int * * @throws \Exception */ public function getPercentDone(): float { $pourcentage = 0; $frequency = $this->agent->getAgentFrequencyBySolution($this->solutionService->getCurrent()); if (!$frequency instanceof AgentFrequency) { $frequency = $this->createDefaultFrequency($this->agent); } if (0 !== count($this->getControles())) { $pourcentage = round( (count($this->getControles()) / $frequency->getFrequencyValue()) * 100, 1 ); } return $pourcentage; } /** * @throws \Exception */ public function getColorCode(): string { if ($this->getPercentDone() >= 60) { return $this->solutionService->getCurrent()->getGraphColor(); } elseif ($this->getPercentDone() >= 20) { return '#dae500'; } return '#d3506b'; } /** * @throws \Exception */ public function getColor(): string { if ($this->getPercentDone() >= 60) { return 'blue'; } elseif ($this->getPercentDone() >= 20) { return 'green'; } return 'pink'; } /** * @return AgentFrequency|false */ public function createDefaultFrequency(User $agent) { if (in_array('ROLE_AGENT', $agent->getArrayRoles())) { $period = $this->entityManager->getRepository(Periode::class)->findOneBy(['nom' => 'MOIS']); $frequency = new AgentFrequency(); $frequency->setSolution($this->solutionService->getCurrent()); $frequency->setAgent($agent); $frequency->setFrequencyValue(3); $frequency->setFrequencyPeriod($period); $this->entityManager->persist($frequency); $this->entityManager->flush(); return $frequency; } return false; } /** * @return array (\DateTime|int|mixed|null)[] * * @throws \Exception */ public function getGoal(User $user): array { $this->setAgent($user); $agentFrequency = $user->getAgentFrequencyBySolution($this->solutionService->getCurrent()); $endDate = null; if (null !== $agentFrequency) { $range = $this->utils->getControleRangeDate( $agentFrequency->getFrequencyPeriod()->getNom() ); $endDate = $range['endDate']; } if ($agentFrequency?->getFrequencyValue()) { $nb = $agentFrequency->getFrequencyValue(); } else { $nb = 0; } return ['controle_a_effectuer' => $this->getRemainingControls(), 'objectif' => $nb, 'endDate' => $endDate]; } /** * @throws \Exception */ public function getRemainingControls(): ?int { if (in_array('ROLE_AGENT', $this->agent->getArrayRoles())) { $agentFrequency = $this->agent->getAgentFrequencyBySolution($this->solutionService->getCurrent()); return $agentFrequency ? $agentFrequency->getFrequencyValue() - count($this->getControles($this->agent)) : 0; } return 0; } /** * @throws \Exception */ public function getControles(?User $agent = null) { if (!$agent) { $agent = $this->agent; } if (!isset($this->controles[$agent->getId()])) { $range = [ 'startDate' => new \DateTime(), 'endDate' => new \DateTime(), ]; if (in_array('ROLE_AGENT', $agent->getArrayRoles())) { $agentFrequency = $agent->getAgentFrequencyBySolution($this->solutionService->getCurrent()); if (null === $agentFrequency) { $agentFrequency = $this->createDefaultFrequency($agent); } $range = $this->utils->getControleRangeDate( $agentFrequency->getFrequencyPeriod()->getNom() ); } $result = $this->getControlesByRangeDate($agent, $range['startDate'], $range['endDate']); $this->controles[$agent->getId()] = $result; } return $this->controles[$agent->getId()]; } public function getControlesByRangeDate( User $agent, \DateTimeInterface $startDate, \DateTimeInterface $endDate, ?Solution $solution = null ) { $criteria = Criteria::create() ->andWhere(Criteria::expr()->gte('dateRealisation', $startDate)) ->orWhere(Criteria::expr()->isNull('dateRealisation')) ->andWhere(Criteria::expr()->lte('dateRealisation', $endDate)) ->orWhere(Criteria::expr()->isNull('dateRealisation')) ->andWhere(Criteria::expr()->eq('valide', true)); if (null !== $solution) { $criteria->andWhere(Criteria::expr()->eq('solution', $solution)); } return $agent->getControles()->matching($criteria); } public function getAgent(): User { return $this->agent; } /** * @param User $agent */ public function setAgent(UserInterface $agent): AgentService { $this->agent = $agent; return $this; } }
Save File
Cancel