فهرست منبع

publish mercure update on access update

Olivier Massot 6 ماه پیش
والد
کامیت
ffd739b6c7

+ 5 - 1
src/Entity/Access/Access.php

@@ -63,6 +63,8 @@ use App\Entity\Traits\CreatedOnAndByTrait;
 use App\Filter\ApiPlatform\Person\FullNameFilter;
 use App\Filter\ApiPlatform\Utils\InFilter;
 use App\Repository\Access\AccessRepository;
+use App\State\Processor\Access\AccessProcessor;
+use App\State\Processor\Organization\ParametersProcessor;
 use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
@@ -80,7 +82,9 @@ use Symfony\Component\Serializer\Annotation\Groups;
  *     @see ~/config/api_platform/Access/access.yaml
  *     @see \App\Doctrine\Access\CurrentAccessExtension
  */
-#[ApiResource]
+#[ApiResource(
+    processor: AccessProcessor::class
+)]
 #[Auditable]
 #[ORM\Entity(repositoryClass: AccessRepository::class)]
 #[ApiFilter(filterClass: BooleanFilter::class, properties: ['person.isPhysical'])]

+ 3 - 3
src/Service/MercureHub.php

@@ -13,10 +13,10 @@ use Symfony\Component\Serializer\SerializerInterface;
 /**
  * Sends private and encrypted mercure updates to the target users.
  *
- * Updates inform of modifications on entities : updates, creations, deletions.
+ * Updates inform of modifications on entities: updates, creations, deletions.
  *
  * The update topic is the id of the recipient user.
- * The content is a json containing the iri of the entity, the operation type, and the current data of this entity
+ * The content is a JSON containing the iri of the entity, the operation type, and the current data of this entity
  */
 class MercureHub
 {
@@ -42,7 +42,7 @@ class MercureHub
     }
 
     /**
-     * Send an update to the.
+     * Send an update to the client.
      */
     public function publish(int $accessId, mixed $entity, string $operationType = self::UPDATE): void
     {

+ 49 - 0
src/Service/OnChange/Access/OnAccessChange.php

@@ -0,0 +1,49 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service\OnChange\Access;
+
+use App\Entity\Access\Access;
+use App\Service\Access\AccessProfileCreator;
+use App\Service\MercureHub;
+use App\Service\OnChange\OnChangeContext;
+use App\Service\OnChange\OnChangeDefault;
+use Symfony\Bundle\SecurityBundle\Security;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
+
+/**
+ * Classe OnAccessChange qui comporte toutes les opérations automatiques se produisant lors de l'évolution d'un Access.
+ */
+class OnAccessChange extends OnChangeDefault
+{
+    public function __construct(
+        private Security $security,
+        private AccessProfileCreator $accessProfileCreator,
+        private MercureHub $mercureHub,
+    ) {
+    }
+
+    public function onChange(mixed $access, OnChangeContext $context): void
+    {
+        $this->publishNewProfile($access);
+    }
+
+    /**
+     * Publie via mercure le nouveau profil de l'access
+     *
+     * @param Access $access
+     * @return void
+     * @throws \Exception
+     */
+    public function publishNewProfile(Access $access): void {
+        $token = $this->security->getToken();
+
+        /** @var ?Access $originalAccess */
+        $originalAccess = $token instanceof SwitchUserToken ? $token->getOriginalToken()->getUser() : null;
+
+        $profile = $this->accessProfileCreator->getAccessProfile($access, $originalAccess);
+
+        $this->mercureHub->publishUpdate($access->getId(), $profile);
+    }
+}

+ 22 - 0
src/State/Processor/Access/AccessProcessor.php

@@ -0,0 +1,22 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\State\Processor\Access;
+
+use App\Service\OnChange\Access\OnAccessChange;
+use App\State\Processor\EntityProcessor;
+use JetBrains\PhpStorm\Pure;
+
+/**
+ * Classe AccessProcessor qui est un custom dataPersister gérant la resource Access.
+ */
+class AccessProcessor extends EntityProcessor
+{
+    #[Pure]
+    public function __construct(
+        OnAccessChange $onChange,
+    ) {
+        parent::__construct($onChange);
+    }
+}