Commit c511c5d4 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Écriture d'un find et findAll dédiés pour éviter les problèmes de...

Écriture d'un find et findAll dédiés pour éviter les problèmes de multiplications de réquête provoquer par getPrivilegesRoles()
parent ecaec9b8
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -2,7 +2,10 @@
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="UnicaenPrivilege\Entity\Db\Privilege" table="UNICAEN_PRIVILEGE_PRIVILEGE">
    <entity name="UnicaenPrivilege\Entity\Db\Privilege"
            table="UNICAEN_PRIVILEGE_PRIVILEGE"
            repository-class="UnicaenPrivilege\Entity\Db\Repository\PrivilegeRepository"
    >

        <indexes>
            <index name="ix_unicaen_privilege_categorie" columns="CATEGORIE_ID"/>
+38 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenPrivilege\Entity\Db\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use UnicaenApp\Service\EntityManagerAwareTrait;

class PrivilegeRepository extends EntityRepository
{
    use EntityManagerAwareTrait;

    public function createQueryBuilder($alias, $indexBy = null): QueryBuilder
    {
        $qb = parent::createQueryBuilder($alias, $indexBy);
        $qb
            ->leftjoin($alias . '.roles', 'role')->addSelect('role')
            ->leftJoin($alias . '.categorie', 'categorie')->addSelect('categorie');
        return $qb;
    }

    public function find($id, $lockMode = null, $lockVersion = null)
    {
        $qb = $this->createQueryBuilder('privilege')
            ->andWhere('privilege.id = :id')->setParameter('id', $id)
        ;
        $result = $qb->getQuery()->getOneOrNullResult();
        return $result;
    }

    public function findAll()
    {
        $qb = $this->createQueryBuilder('privilege')
        ;
        $result = $qb->getQuery()->getResult();
        return $result;
    }
}
 No newline at end of file
+59 −52
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use BjyAuthorize\Provider\Resource\ProviderInterface as ResourceProviderInterfac
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\ORMException;
use UnicaenApp\Controller\Plugin\Exception\InvalidArgumentException;
use UnicaenPrivilege\Entity\Db\PrivilegeCategorieInterface;
use UnicaenPrivilege\Entity\Db\PrivilegeInterface;
use UnicaenPrivilege\Exception\RuntimeException;
@@ -26,24 +27,75 @@ class PrivilegeService extends CommonService implements PrivilegeProviderInterfa


    /**
     * @param string $privilegeEntityClass
     * @param string $entityClass
     * @return void
     */
    public function setEntityClass($privilegeEntityClass)
    public function setEntityClass(string $entityClass) : void
    {
        if (! class_exists($privilegeEntityClass) || ! in_array(PrivilegeInterface::class, class_implements($privilegeEntityClass))) {
        if (! class_exists($entityClass) || ! in_array(PrivilegeInterface::class, class_implements($entityClass))) {
            throw new InvalidArgumentException("L'entité associée aux privilèges doit implémenter " . PrivilegeInterface::class);
        }

        $this->entityClass = $privilegeEntityClass;
        $this->entityClass = $entityClass;
    }

    /** ENTITY MANAGMENT **********************************************************************************************/

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function create(PrivilegeInterface $privilege) : PrivilegeInterface
    {
        try {
            $this->getEntityManager()->persist($privilege);
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            throw new RuntimeException("Un problème est survenu lors de la création du privilège.", null, $e);
        }

        return $privilege;
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function update(PrivilegeInterface $privilege) : PrivilegeInterface
    {
        try {
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            throw new RuntimeException("Un problème est survenu lors de la mise à jour du privilège \"" . $privilege->getLibelle() . "\"", null, $e);
        }

        return $privilege;
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function delete(PrivilegeInterface $privilege) : PrivilegeInterface
    {
        try {
            $this->getEntityManager()->remove($privilege);
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            throw new RuntimeException("Un problème est survenu lors de la suppression du privilège \"" . $privilege->getLibelle() . "\"", null, $e);
        }

        return $privilege;
    }

    /** REQUETAGE *****************************************************************************************************/

    /**
     * @param string $code
     * @param int|PrivilegeCategorieInterface $categorie
     * @return PrivilegeInterface|null
     */
    public function findByCode(string $code, $categorie)
    public function findByCode(string $code, $categorie) : ?PrivilegeInterface
    {
        return $this->getRepo()->findOneBy(['code' => $code, 'categorie' => $categorie]);
    }
@@ -66,7 +118,7 @@ class PrivilegeService extends CommonService implements PrivilegeProviderInterfa
     * @param int|PrivilegeCategorieInterface $categorie
     * @return PrivilegeInterface|null
     */
    public function findByLibelle(string $libelle, $categorie)
    public function findByLibelle(string $libelle, $categorie) : ?PrivilegeInterface
    {
        return $this->getRepo()->findOneBy(['libelle' => $libelle, 'categorie' => $categorie]);
    }
@@ -76,7 +128,7 @@ class PrivilegeService extends CommonService implements PrivilegeProviderInterfa
     * @param array $orderBy
     * @return array
     */
    public function findByCategorie(PrivilegeCategorieInterface $categorie, array $orderBy = ['ordre' => 'ASC'])
    public function findByCategorie(PrivilegeCategorieInterface $categorie, array $orderBy = ['ordre' => 'ASC']) : array
    {
        return $this->getRepo()->findBy(['categorie' => $categorie], $orderBy);
    }
@@ -102,52 +154,7 @@ class PrivilegeService extends CommonService implements PrivilegeProviderInterfa
        return $privilegesByCategorie;
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function create(PrivilegeInterface $privilege)
    {
        try {
            $this->getEntityManager()->persist($privilege);
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            throw new RuntimeException("Un problème est survenu lors de la création du privilège.", null, $e);
        }

        return $privilege;
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function update(PrivilegeInterface $privilege)
    {
        try {
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            throw new RuntimeException("Un problème est survenu lors de la mise à jour du privilège \"" . $privilege->getLibelle() . "\"", null, $e);
        }

        return $privilege;
    }

    /**
     * @param PrivilegeInterface $privilege
     * @return PrivilegeInterface
     */
    public function delete(PrivilegeInterface $privilege)
    {
        try {
            $this->getEntityManager()->remove($privilege);
            $this->getEntityManager()->flush($privilege);
        } catch(ORMException $e) {
            throw new RuntimeException("Un problème est survenu lors de la suppression du privilège \"" . $privilege->getLibelle() . "\"", null, $e);
        }

        return $privilege;
    }

    /**
     * @param RoleInterface $role