Jelajahi Sumber

listener pour mettre les informations des parametres à jour

Vincent GUFFON 3 tahun lalu
induk
melakukan
af113967e1

+ 17 - 0
src/EventListener/Helper.php

@@ -0,0 +1,17 @@
+<?php
+declare(strict_types=1);
+
+namespace App\EventListener;
+
+use Doctrine\ORM\EntityManagerInterface;
+
+
+/**
+ * Classe ... qui ...
+ */
+trait Helper
+{
+    private function hasChangeField(EntityManagerInterface $entityManager, $entity, string $field){
+        return key_exists($field, $entityManager->getUnitOfWork()->getEntityChangeSet($entity));
+    }
+}

+ 68 - 0
src/EventListener/Organization/OrganizationChangedSubscriber.php

@@ -0,0 +1,68 @@
+<?php
+declare(strict_types=1);
+
+namespace App\EventListener\Organization;
+
+use App\Entity\Billing\BillingSetting;
+use App\Entity\Organization\Organization;
+use App\Entity\Organization\Parameters;
+use App\Enum\Organization\LegalEnum;
+use App\EventListener\Helper;
+use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
+use Doctrine\ORM\EntityManagerInterface;
+use Doctrine\ORM\Event\OnFlushEventArgs;
+use Doctrine\ORM\Events;
+
+/**
+ * Classe subscriber qui doit intervenir quand on flush une entité Organization
+ */
+class OrganizationChangedSubscriber implements EventSubscriberInterface
+{
+    use Helper;
+
+    /**
+     * On souscrit à l'événement OnFlush
+     * @return array
+     */
+    public function getSubscribedEvents(): array
+    {
+        return [
+            Events::onFlush
+        ];
+    }
+
+    /**
+     * onFlush Event
+     * @param OnFlushEventArgs $onFlushEventArgs
+     */
+    public function onFlush(OnFlushEventArgs $onFlushEventArgs){
+        $entityManager = $onFlushEventArgs->getEntityManager();
+        $uow = $entityManager->getUnitOfWork();
+        foreach ($uow->getScheduledEntityUpdates() as $entityUpdate){
+            if($entityUpdate instanceof Organization){
+                //Si un update du field legalStatus est modifié
+                if($this->hasChangeField($entityManager, $entityUpdate, 'legalStatus'))
+                    $this->handleLegalStatusChanged($entityUpdate, $entityManager);
+            }
+        }
+    }
+
+    /**
+     * Changement qui doivent être fait si le statut légale d'une structure est changé.
+     * @param Organization $organization
+     * @param EntityManagerInterface $entityManager
+     */
+    public function handleLegalStatusChanged(Organization $organization, EntityManagerInterface $entityManager){
+        //Si le status légal n'est pas "Association Loi 1901"
+        if($organization->getLegalStatus() !== LegalEnum::ASSOCIATION_LAW_1901()->getValue()){
+            $organization->getParameters()->setShowAdherentList(false);
+            $entityManager->getUnitOfWork()->computeChangeSet($entityManager->getClassMetadata(Parameters::class), $organization->getParameters());
+        }
+
+        //Si le status légal est "Société commerciale"
+        if($organization->getLegalStatus() === LegalEnum::COMMERCIAL_SOCIETY()->getValue()){
+            $organization->getBillingSetting()->setApplyVat(true);
+            $entityManager->getUnitOfWork()->computeChangeSet($entityManager->getClassMetadata(BillingSetting::class), $organization->getBillingSetting());
+        }
+    }
+}