src/Events/AddPersFertiOrgSpecCompositionSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\PersFertiOrgSpecComposition;
  5. use App\Exception\PersFertiOrgSpecError;
  6. use App\Repository\ParametrageRepository;
  7. use App\Repository\PersFertiOrgSpecCompositionRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Security;
  13. class AddPersFertiOrgSpecCompositionSubscriber implements EventSubscriberInterface
  14. {
  15.     private $security;
  16.     /** @var EntityManagerInterface */
  17.     private $manager;
  18.     private $persFertiOrgSpecCompositionRepository;
  19.     private $parametrageRepository;
  20.     public function __construct(Security $securityPersFertiOrgSpecCompositionRepository $persFertiOrgSpecCompositionRepository,ParametrageRepository $parametrageRepository)
  21.     {
  22.         $this->security $security;
  23.         $this->persFertiOrgSpecCompositionRepository $persFertiOrgSpecCompositionRepository;
  24.         $this->parametrageRepository $parametrageRepository;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::VIEW => ['setForPersFertiOrgSpecComposition',EventPriorities::PRE_VALIDATE]
  30.         ];
  31.     }
  32.     public function setForPersFertiOrgSpecComposition(ViewEvent $event){
  33.         $result $event->getControllerResult();
  34.         $method $event->getRequest()->getMethod();
  35.         //Vérifier les contraintes de %
  36.         if($result instanceof PersFertiOrgSpecComposition && ($method == 'POST' || $method == 'PATCH')) {
  37.             $persFertiOrgSpecComposition=$this->persFertiOrgSpecCompositionRepository->findBy(array('persFertiOrgSpec' => $result->getPersFertiOrgSpec()));
  38.             $somme=0;
  39.             foreach ($persFertiOrgSpecComposition as $elt){
  40.                 if($elt->getId() != $event->getRequest()->attributes->get('id')) $somme+=$elt->getQPourc();
  41.             }
  42.             $restant 100 $somme;
  43.             //ajouter l'élément courant
  44.             $somme+= $result->getQPourc();
  45.             if($somme 100){
  46.                 throw new PersFertiOrgSpecError('La composition totale de votre effluent ne peut pas dépasser 100%. Il reste '.floor($restant).'%.');
  47.             }
  48.             $messageInverse $this->parametrageRepository->findOneBy(array('code' => 'FERTI_ORG_INVERSION_MESSAGE'));
  49.             if($result->getNNh4Pourc() > $result->getNPourc()){
  50.                 throw new PersFertiOrgSpecError($messageInverse->getValeur());
  51.             }
  52. /*
  53.             $ecart = $this->parametrageRepository->findOneBy(array('code' => 'FERTI_ORG_ECART_POURC'));
  54.             $messageEcart = $this->parametrageRepository->findOneBy(array('code' => 'FERTI_ORG_ECART_MESSAGE'));
  55.             $ecartNPourcBas=number_format($result->getRegFertiOrgType()->getNPourc() - ($result->getRegFertiOrgType()->getNPourc()*$ecart->getValeur()/100),2);
  56.             $ecartNPourcHaut=number_format($result->getRegFertiOrgType()->getNPourc() + ($result->getRegFertiOrgType()->getNPourc()*$ecart->getValeur()/100),2);
  57.             $ecartNNh4cBas=number_format($result->getRegFertiOrgType()->getNNh4Pourc() - ($result->getRegFertiOrgType()->getNNh4Pourc()*$ecart->getValeur()/100),2);
  58.             $ecarNNh4Haut=number_format($result->getRegFertiOrgType()->getNNh4Pourc() + ($result->getRegFertiOrgType()->getNNh4Pourc()*$ecart->getValeur()/100),2);
  59.             $messageNPourc = '. Votre valeur Teneur N total doit etre située entre '.($ecartNPourcBas*10).' et '.($ecartNPourcHaut*10);
  60.             $messageNNh4 = '. Votre valeur Teneur N-NH4 total doit etre située entre '.($ecartNNh4cBas*10).' et '.($ecarNNh4Haut*10);
  61.             if($result->getNPourc() < $ecartNPourcBas || $result->getNPourc() > $ecartNPourcHaut){
  62.                 throw new PersFertiOrgSpecError($messageEcart->getValeur().$messageNPourc);
  63.             }
  64.             if($result->getNNh4Pourc() < $ecartNNh4cBas || $result->getNNh4Pourc() > $ecarNNh4Haut){
  65.                 throw new PersFertiOrgSpecError($messageEcart->getValeur().$messageNNh4);
  66.             }
  67. */
  68.         }
  69.     }
  70. }