Commit 00008505 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

CRUD Privilege : utilisation d'une transaction explicite, sinon empêche une...

CRUD Privilege : utilisation d'une transaction explicite, sinon empêche une appli appelante d'en utiliser.
parent 06132fae
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace UnicaenPrivilege\Controller;

use Exception;
use UnicaenPrivilege\Form\Privilege\CategorieFiltreFormAwareTrait;
use UnicaenPrivilege\Form\Privilege\PrivilegeFormAwareTrait;
use UnicaenPrivilege\Service\Privilege\PrivilegeCategorieServiceAwareTrait;
@@ -105,6 +106,8 @@ class PrivilegeController extends AbstractActionController
    {
        $privilege = $this->privilegeService->getRequestedPrivilege($this);
        $role = $this->roleService->getRequestedRole($this);

        // inversion + déclenchement d'evt par le PrivilegeService (pour vidage du cache)
        $this->privilegeService->toggle($role,$privilege);

        $viewModel = new ViewModel();
+34 −14
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ namespace UnicaenPrivilege\Service\Privilege;

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\ORMException;
use Exception;
use Laminas\EventManager\EventManagerAwareInterface;
use Laminas\EventManager\EventManagerAwareTrait;
use Laminas\Mvc\Controller\AbstractActionController;
@@ -43,15 +43,21 @@ class PrivilegeService extends CommonService implements EventManagerAwareInterfa
    /** ENTITY MANAGMENT **********************************************************************************************/

    /**
     * Création d'un nouveau Privilege en bdd.
     *
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function create(PrivilegeInterface $privilege) : PrivilegeInterface
    {
        $this->entityManager->beginTransaction();

        try {
            $this->getEntityManager()->persist($privilege);
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            $this->entityManager->persist($privilege);
            $this->entityManager->flush();
            $this->entityManager->commit();
        } catch(Exception $e) {
            $this->entityManager->rollback();
            throw new RuntimeException("Un problème est survenu lors de la création du privilège.", null, $e);
        }

@@ -59,14 +65,22 @@ class PrivilegeService extends CommonService implements EventManagerAwareInterfa
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     * Enregistrement des modifs d'un Privilege en bdd.
     *
     * Ouvre une transaction, enregistre les modifications en cours sur le Privilege puis fait un commit.
     * **Un événement informant de l'opération est déclenché à l'issue du commit (evt écouté pour le vidage du cache).**
     *
     * @param \UnicaenPrivilege\Entity\Db\PrivilegeInterface|null $privilege Eventuel seul privilège concerné (@deprecated)
     */
    public function update(PrivilegeInterface $privilege) : PrivilegeInterface
    public function update(?PrivilegeInterface $privilege = null) : ?PrivilegeInterface
    {
        $this->entityManager->beginTransaction();

        try {
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            $this->entityManager->flush($privilege);
            $this->entityManager->commit();
        } catch(Exception $e) {
            $this->entityManager->rollback();
            throw new RuntimeException("Un problème est survenu lors de la mise à jour du privilège \"" . $privilege->getLibelle() . "\"", null, $e);
        }

@@ -76,15 +90,21 @@ class PrivilegeService extends CommonService implements EventManagerAwareInterfa
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     * Suppression d'un Privilege en bdd.
     *
     * Ouvre une transaction, supprime le Privilege puis fait un commit.
     * **Un événement informant de l'opération est déclenché à l'issue du commit (evt écouté pour le vidage du cache).**
     */
    public function delete(PrivilegeInterface $privilege) : PrivilegeInterface
    {
        $this->entityManager->beginTransaction();

        try {
            $this->getEntityManager()->remove($privilege);
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            $this->entityManager->remove($privilege);
            $this->entityManager->flush();
            $this->entityManager->commit();
        } catch(Exception $e) {
            $this->entityManager->rollback();
            throw new RuntimeException("Un problème est survenu lors de la suppression du privilège \"" . $privilege->getLibelle() . "\"", null, $e);
        }