Commit 8e0d68f3 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Création d'options de config ; améliorations ; refactorisations.

parent c4ca763a
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ namespace Application;

use Application\Entity\Db\Source;
use Application\Navigation\NavigationFactoryFactory;
use Import\Filter\PrefixEtabColumnValueFilter;
use Import\Model\ImportObserv;
use Import\Model\ImportObservResult;
use Retraitement\Filter\Command\RetraitementShellCommandMines;
@@ -91,10 +90,6 @@ $config = [
                ],
            ],
        ],
        // Options concernant les inscriptions administratives (IA)
        'inscription_administrative' => [
            'run_reception_process_after_import_process' => false,
        ],
    ],
    'import' => [

+15 −0
Original line number Diff line number Diff line
@@ -46,6 +46,21 @@ return [
                ],
            ],
        ],

        /**
         * Options du module InscriptionAdministrative
         */
        'inscription_administrative' => [
            // Active ou non l'automaticité du traitement de l'IA (par le service {@see InscriptionAdministrativeReceptionProcess})
            // lorsque l'événement {@see InscriptionAdministrativeImportEvent} est reçu.
            'run_reception_process_after_import' => false,

            'reception' => [
                // Autorise ou non la mise à jour du doctorant (à partir des données "apprenant") *lorsque c'est possible*.
                // NB : même si l'autorisation n'est pas accordée, le Code Apprenant du doctorant sera tout de même mis à jour.
                'update_doctorant_if_possible' => true,
            ],
        ],
    ],

    'actualite' => [
+30 −42
Original line number Diff line number Diff line
@@ -2,14 +2,14 @@

namespace Application\Service;

use Application\Constants;
use Application\Entity\Db\Repository\DefaultEntityRepository;
use DateTime;
use Doctrine\ORM\EntityManager;
use Exception;
use InvalidArgumentException;
use RuntimeException;
use UnicaenApp\Service\EntityManagerAwareInterface;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenApp\Traits\MessageAwareTrait;
use Webmozart\Assert\Assert;

abstract class BaseService implements EntityManagerAwareInterface
{
@@ -52,54 +52,42 @@ abstract class BaseService implements EntityManagerAwareInterface
    }

    /**
     * Calcule le "changeset" d'une entité (propriétés dont la valeur a changé)
     * si on lui applique l'update spécifié.
     * Calcule le diff (en terme de valeur d'attributs) entre 2 entités.
     *
     * @param object $entity Entity concernée
     * @param array $fields Champs à prendre en compte, sinon ils sont tous pris en compte
     * @param callable $updateEntityCallable Callable de l'update à réaliser
     * @return array<string, array<mixed, mixed>>
     * @param object $entity1 1ere entité
     * @param object $entity2 2eme entité
     * @param array|null $fieldNames Champs à prendre en compte, sinon ils sont tous pris en compte
     *
     * @return array Ex : [ 'prenom1' => ['Paul', 'Paule'], 'nomUsuel' => ['Hochon', 'Hauchon'] ]
     */
    protected function computeDiffForUpdate(object $entity, callable $updateEntityCallable, array $fields = []): array
    protected function computeEntityDiff(object $entity1, object $entity2, ?array $fieldNames = null): array
    {
        $entityManager = $this->getEntityManager();
        $uow = $entityManager->getUnitOfWork();
        if ($uow->getEntityChangeSet($entity)) {
            throw new InvalidArgumentException("Opération impossible car l'entité est en cours de modification");
        }

        // application provisoire de l'update UNIQUEMENT pour pouvoir calculer le "changeset" Doctrine
        $updateEntityCallable($entity);
        $metadata = $this->entityManager->getClassMetadata($this->getRepository()->getClassName());

        // calcul du changeset
        $entityClass = get_class($entity);
        $metadata = $entityManager->getClassMetadata($entityClass);
        $uow->recomputeSingleEntityChangeSet($metadata, $entity);
        $changeset = $uow->getEntityChangeSet($entity);

        // refresh de l'entité pour annuler l'update
        try {
            $entityManager->refresh($entity);
        } catch (Exception $e) {
            throw new RuntimeException("Refresh impossible ! " . $e->getMessage(), null, $e);
        if ($fieldNames === null) {
            $fieldNames = $metadata->getFieldNames();
        }
        Assert::notEmpty($fieldNames);

        // mise en forme du résultat
        if (!$fields) {
            $fields = $metadata->getFieldNames();
        }
        $fieldsWithChanges = [];
        foreach ($fields as $field) {
            if (!array_key_exists($field, $changeset)) {
                continue;
        $stringify = function($item) {
            if ($item instanceof DateTime) {
                return $item->format(Constants::DATETIME_FORMAT);
            }
            $oldValue = $changeset[$field][0];
            $newValue = $changeset[$field][1];
            if ($oldValue <> $newValue) {
                $fieldsWithChanges[$field] = [$oldValue, $newValue];
            return (string)$item;
        };

        $diff = [];
        foreach ($fieldNames as $fieldName) {
            $individuFieldValue = $metadata->getFieldValue($entity1, $fieldName);
            $individuFromApprenantFieldValue = $metadata->getFieldValue($entity2, $fieldName);
            if ($individuFieldValue <> $individuFromApprenantFieldValue) {
                $diff[$fieldName] = [
                    $stringify($individuFieldValue),
                    $stringify($individuFromApprenantFieldValue)
                ];
            }
        }

        return $fieldsWithChanges;
        return $diff;
    }
}
 No newline at end of file
+13 −7
Original line number Diff line number Diff line
@@ -149,19 +149,25 @@ class DoctorantService extends BaseService
        $doctorant->setIne($iaa->getINE());
    }


    /**
     * Détermine si le doctorant spécifié possède des attributs dont la valeur pourrait être mise à jour
     * à partir de ceux de l'apprenant spécifié.
     */
    public function diffDoctorantFromApprenant(
        Doctorant $doctorant,
        InscriptionAdministrativeApprenant $apprenant): array
        InscriptionAdministrativeApprenant $apprenant,
        ?array $fieldNames = null): array
    {
        $callable = function() use ($doctorant, $apprenant) {
            // update provisoire du doctorant UNIQUEMENT pour pouvoir calculer le "changeset" Doctrine
            $this->updateDoctorantFromApprenant($doctorant, $apprenant);
        };
        $doctorantFromApprenant = new Doctorant();
        $this->updateDoctorantFromApprenant($doctorantFromApprenant, $apprenant);

        if ($fieldNames === null) {
            $fieldNames = [
                'ine',
            ];
        }

        return $this->computeDiffForUpdate($doctorant, $callable, ['ine']);
        return $this->computeEntityDiff($doctorant, $doctorantFromApprenant, $fieldNames);
    }
}
 No newline at end of file
+20 −19
Original line number Diff line number Diff line
@@ -163,14 +163,14 @@ class IndividuService extends BaseService
     */
    public function diffIndividuFromApprenant(
        Individu $individu,
        InscriptionAdministrativeApprenant $apprenant): array
        InscriptionAdministrativeApprenant $apprenant,
        ?array $fieldNames = null): array
    {
        $callable = function() use ($individu, $apprenant) {
            // update provisoire du doctorant UNIQUEMENT pour pouvoir calculer le "changeset" Doctrine
            $this->initIndividuFromApprenant($individu, $apprenant);
        };
        $individuFromApprenant = new Individu();
        $this->initIndividuFromApprenant($individuFromApprenant, $apprenant);

        $fields = [
        if ($fieldNames === null) {
            $fieldNames = [
                'civilite',
                'nomPatronymique',
                'nomUsuel',
@@ -180,8 +180,9 @@ class IndividuService extends BaseService
                'dateNaissance',
                'paysNationalite',
            ];
        }

        return $this->computeDiffForUpdate($individu, $callable, $fields);
        return $this->computeEntityDiff($individu, $individuFromApprenant, $fieldNames);
    }

    /**
Loading