src/Serializer/Normalizer/CardiometabolicProfileExtendedNormalizer.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Serializer\Normalizer;
  3. use App\Entity\CardiovascularScore;
  4. use App\Entity\ScoreRisk;
  5. use App\Entity\Users;
  6. use App\Helper\ConvertUnitMeasures;
  7. use App\Helper\HealthHelper;
  8. use App\Traits\ProfilesTrait;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\DependencyInjection\Attribute\Autowire;
  11. use App\Entity\CardiometabolicProfiles;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  15. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  16. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class CardiometabolicProfileExtendedNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  19. {
  20.     use ProfilesTrait;
  21.     private const ALREADY_CALLED 'PROFILE_EXTENDED_NORMALIZER_ALREADY_CALLED';
  22.     private ?Users $user;
  23.     private ?float $unitMeasure;
  24.     public function __construct(
  25.         #[Autowire(serviceObjectNormalizer::class)]
  26.         private NormalizerInterface    $normalizer,
  27.         private EntityManagerInterface $em,
  28.         private TranslatorInterface    $translator,
  29.         private Security               $security
  30.     )
  31.     {
  32.         $this->user $security->getUser();
  33.         if ($this->user instanceof Users) {
  34.             $this->unitMeasure $this->user->getUnitMeasure();
  35.     } else {
  36.         $this->unitMeasure null;
  37.     }
  38.     }
  39.     public function normalize($objectstring $format null, array $context = []): array
  40.     {
  41.         $context[self::ALREADY_CALLED] = true;
  42.     $normalized $this->normalizer->normalize($object$format$context);
  43.     if (null === $this->unitMeasure) {
  44.             $this->unitMeasure $object->getUser()->getUnitMeasure();
  45.         }
  46.         if (in_array('profiles:extended'$context['groups'])) {
  47.             if ($object->getCvdScoreId() > 0) {
  48.                 $cvdScore $this->em->getRepository(CardiovascularScore::class)->find($object->getCvdScoreId());
  49.                 if ($cvdScore) {
  50.                     $scoreRisk $this->em->getRepository(ScoreRisk::class)->find($cvdScore->getScoreRiskId());
  51.                 }
  52.             }
  53.             $scoreRisk HealthHelper::getScoreRisk($object$scoreRisk ?? null$this->em);
  54.             $normalized['score_risk'] = $this->parseScoreRisk($scoreRisk$this->user->getLang());
  55.             $normalized['cvd_score'] = HealthHelper::parseScoreToArray(HealthHelper::getScore($object$cvdScore ?? null$scoreRisk));
  56.         }
  57.         if ($this->unitMeasure == 2) {
  58.             $normalized['weight'] = (float)ConvertUnitMeasures::kgToLbs($normalized['weight']);
  59.             $normalized['height'] = (float)ConvertUnitMeasures::cmToInches($normalized['height']);
  60.         }
  61.         return $normalized;
  62.     }
  63.     public function supportsNormalization($datastring $format null, array $context = []): bool
  64.     {
  65.         // avoid recursion: only call once per object
  66.         if (isset($context[self::ALREADY_CALLED])) {
  67.             return false;
  68.         }
  69.         return $data instanceof CardiometabolicProfiles;
  70.     }
  71.     public function hasCacheableSupportsMethod(): bool
  72.     {
  73.         return true;
  74.     }
  75. }