From 8cd71b7521254225aa72b612299fb02bead20428 Mon Sep 17 00:00:00 2001 From: afornerot Date: Fri, 3 Jan 2025 16:37:50 +0100 Subject: [PATCH] ko tt les 5 appels --- config/packages/cache.yaml | 18 +------ config/services.yaml | 1 + src/Controller/DemandeLogementController.php | 23 ++++++--- src/Controller/ProfilEtudiantController.php | 25 ++++++++-- src/Service/CacheCounterService.php | 51 ++++++++++++++++++++ 5 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 src/Service/CacheCounterService.php diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml index 6899b72..90963ea 100644 --- a/config/packages/cache.yaml +++ b/config/packages/cache.yaml @@ -1,19 +1,3 @@ framework: cache: - # Unique name of your app: used to compute stable namespaces for cache keys. - #prefix_seed: your_vendor_name/app_name - - # The "app" cache stores to the filesystem by default. - # The data in this cache should persist between deploys. - # Other options include: - - # Redis - #app: cache.adapter.redis - #default_redis_provider: redis://localhost - - # APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues) - #app: cache.adapter.apcu - - # Namespaced pools use the above "app" backend by default - #pools: - #my.dedicated.cache: null + app: cache.adapter.filesystem diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..c884bff 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -10,6 +10,7 @@ services: _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. + public: true # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name diff --git a/src/Controller/DemandeLogementController.php b/src/Controller/DemandeLogementController.php index d46276b..95429d4 100644 --- a/src/Controller/DemandeLogementController.php +++ b/src/Controller/DemandeLogementController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Dto\DemandeLogementRequestVO; use App\Dto\ErreurReponseVO; +use App\Service\CacheCounterService; use Nelmio\ApiDocBundle\Annotation\Model; use OpenApi\Attributes as OA; use Symfony\Component\HttpFoundation\JsonResponse; @@ -12,6 +13,13 @@ use Symfony\Component\Routing\Annotation\Route; class DemandeLogementController { + private CacheCounterService $cacheCounterService; + + public function __construct(CacheCounterService $cacheCounterService) + { + $this->cacheCounterService = $cacheCounterService; + } + #[Route('/api/v1/etudiants/{typeid}/{id}/demande-logement', methods: ['POST'])] #[OA\Tag(name: 'Service de demande de logement d\'un étudiant')] #[OA\Post( @@ -83,14 +91,15 @@ class DemandeLogementController return new JsonResponse(['error' => 'Invalid request'], 400); } - if ($typid="id-vpe" && $id % 2 == 0) { - $error=new ErreurReponseVO; - $error->setStatus(400); - $error->setError('IdPVE paire Mock force KO'); - $error->setPath($request->getUri()); - return new JsonResponse($error); + // Incrémenter le compteur d'appel pour ce idPve + $count = $this->cacheCounterService->incrementCounter('logement',$id); + + // Générer une erreur tout les 5 appels associés à cet idE + if ($count % 5 == 0) { + $this->cacheCounterService->resetCounter('logement',$id); + return new JsonResponse(['status' => 'KO', 'message' => '5eme appel pour cet ID = KO'], 404); } - return new JsonResponse(['status' => 'OK', 'message' => 'IdPVE impaire Mock force OK'], 201); + return new JsonResponse(['status' => 'OK', 'message' => 'OK nb appel = '.($count%5)], 201); } } \ No newline at end of file diff --git a/src/Controller/ProfilEtudiantController.php b/src/Controller/ProfilEtudiantController.php index d8d9248..0e964b5 100644 --- a/src/Controller/ProfilEtudiantController.php +++ b/src/Controller/ProfilEtudiantController.php @@ -2,17 +2,26 @@ namespace App\Controller; -use App\Dto\DemandeLogementRequestVO; use App\Dto\ErreurReponseVO; use App\Dto\ProfilEtudiantRequestVO; +use App\Service\CacheCounterService; use Nelmio\ApiDocBundle\Annotation\Model; use OpenApi\Attributes as OA; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; class ProfilEtudiantController { + private CacheCounterService $cacheCounterService; + + public function __construct(CacheCounterService $cacheCounterService) + { + $this->cacheCounterService = $cacheCounterService; + } + #[Route('/api/v1/notifications/mse/profil-etudiant', name: 'notifier_modification_profil_etudiant', methods: ['POST'])] #[OA\Tag(name: 'Service de notifications')] #[OA\Post( @@ -74,11 +83,17 @@ class ProfilEtudiantController return new JsonResponse(['error' => 'idPVE doit être un entier'], 400); } - if ($data['idPVE'] % 2 == 0) { - return new JsonResponse(['status' => 'KO', 'message' => 'IdPVE paire Mock force KO'], 404); - } + // Incrémenter le compteur d'appel pour ce idPve + $id=$data['idPVE']; + $count = $this->cacheCounterService->incrementCounter('profil',$id); - return new JsonResponse(['status' => 'OK', 'message' => 'IdPVE impaire Mock force OK'], 201); + // Générer une erreur tout les 5 appels associés à cet idPVE + if ($count % 5 == 0) { + $this->cacheCounterService->resetCounter('profil',$id); + return new JsonResponse(['status' => 'KO', 'message' => '5eme appel pour cet ID = KO'], 404); + } + + return new JsonResponse(['status' => 'OK', 'message' => 'OK nb appel = '.($count%5)], 201); } diff --git a/src/Service/CacheCounterService.php b/src/Service/CacheCounterService.php new file mode 100644 index 0000000..8873011 --- /dev/null +++ b/src/Service/CacheCounterService.php @@ -0,0 +1,51 @@ +cache = $cache; + } + + /** + * Gère l'incrémentation du compteur dans le cache pour un ID donné + * + * @param string $id L'identifiant unique pour lequel on suit le nombre d'occurrences + * @return int Le nombre d'occurrences de cet ID dans le cache + */ + public function incrementCounter(string $type, string $id): int + { + $cacheKey = 'id_count_'.$type.'_'.$id; + + // Récupérer ou initialiser le compteur à 0 si l'ID n'existe pas dans le cache + $count = $this->cache->get($cacheKey, function (ItemInterface $item) { + $item->expiresAfter(3600); // Expire après 1 heure + return 0; + }); + + // Incrémenter le compteur + $count++; + + // Mettre à jour le cache avec la nouvelle valeur + // Supprimer l'ancienne valeur du cache et réécrire la nouvelle + $this->cache->delete($cacheKey); // Supprime l'ancienne valeur + $this->cache->get($cacheKey, function (ItemInterface $item) use ($count) { + $item->expiresAfter(3600); // Expire après 1 heure + return $count; + }); + + return $count; + } + + public function resetCounter(string $type, string $id): void{ + $cacheKey = 'id_count_'.$type.'_'.$id; + $this->cache->delete($cacheKey); + } +}