src/Offer/Serializer/Normalizer/OfferNormalizer.php line 87

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2021 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2021 TECLA Consulting Group oü
  16.  */
  17. namespace App\Offer\Serializer\Normalizer;
  18. use App\Contact\Security\ContactContextInterface;
  19. use App\Core\Serializer\Normalizer\AttributeAwareNormalizerTrait;
  20. use App\Core\Util\DecimalNumberEncoderInterface;
  21. use App\Offer\Model\OfferInterface;
  22. use App\Offer\Util\OfferHistoryGeneratorInterface;
  23. use App\Offer\Util\OfferNumberEncoderInterface;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  26. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  27. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  28. final class OfferNormalizer implements ContextAwareNormalizerInterfaceCacheableSupportsMethodInterface
  29. {
  30.     use AttributeAwareNormalizerTrait;
  31.     private NormalizerInterface $normalizer;
  32.     private UrlGeneratorInterface $urlGenerator;
  33.     private ContactContextInterface $context;
  34.     private DecimalNumberEncoderInterface $decimalEncoder;
  35.     private OfferNumberEncoderInterface $numberEncoder;
  36.     private OfferHistoryGeneratorInterface $historyGenerator;
  37.     public function __construct(
  38.         NormalizerInterface $normalizer,
  39.         UrlGeneratorInterface $urlGenerator,
  40.         ContactContextInterface $context,
  41.         DecimalNumberEncoderInterface $decimalEncoder,
  42.         OfferNumberEncoderInterface $numberEncoder,
  43.         OfferHistoryGeneratorInterface $historyGenerator
  44.     ) {
  45.         $this->normalizer $normalizer;
  46.         $this->urlGenerator $urlGenerator;
  47.         $this->context $context;
  48.         $this->decimalEncoder $decimalEncoder;
  49.         $this->numberEncoder $numberEncoder;
  50.         $this->historyGenerator $historyGenerator;
  51.     }
  52.     public function normalize($objectstring $format null, array $context = []): float|int|bool|\ArrayObject|array|string|null
  53.     {
  54.         $data $this->normalizer->normalize($object$format$context);
  55.         if (true === $this->shouldSerializeAttribute($dataOfferInterface::ATTR_NUMBER$context)) {
  56.             $this->normalizeNumber($object$data);
  57.         }
  58.         if (true === $this->shouldSerializeAttribute($dataOfferInterface::ATTR_GROSS_TOTAL$context)) {
  59.             $this->normalizeGrossTotal($object$data);
  60.         }
  61.         if (true === $this->shouldSerializeAttribute($dataOfferInterface::ATTR_HISTORY$context)) {
  62.             $this->normalizeHistory($object$data);
  63.         }
  64.         if (true === $this->shouldSerializeAttribute($dataOfferInterface::ATTR__LINKS$context)) {
  65.             $this->normalize_Links($object$data);
  66.         }
  67.         return $data;
  68.     }
  69.     public function supportsNormalization($datastring $format null, array $context = []): bool
  70.     {
  71.         return $data instanceof OfferInterface;
  72.     }
  73.     public function hasCacheableSupportsMethod(): bool
  74.     {
  75.         return __CLASS__ === static::class;
  76.     }
  77.     private function normalizeNumber(OfferInterface $offer, array &$data): void
  78.     {
  79.         $data[OfferInterface::ATTR_NUMBER] = $this->numberEncoder->encode($offer->getNumber());
  80.     }
  81.     private function normalizeGrossTotal(OfferInterface $offer, array &$data): void
  82.     {
  83.         $data[OfferInterface::ATTR_GROSS_TOTAL] = $this->decimalEncoder->decode($offer->getItemsGrossTotal());
  84.     }
  85.     private function normalizeHistory(OfferInterface $offer, array &$data): void
  86.     {
  87.         $history $this->historyGenerator->generate($offer);
  88.         $data[OfferInterface::ATTR_HISTORY] = $this->normalizer->normalize($history);
  89.     }
  90.     private function normalize_Links(OfferInterface $offer, array &$data): void
  91.     {
  92.         if ($offer->getSupplier() === $this->context->getCurrent()) {
  93.             $httpViewUrl $this->urlGenerator->generate('app_offer_http_offer_outgoing_view', [
  94.                 'id' => $offer->getId(),
  95.             ]);
  96.             $httpExportUrl $this->urlGenerator->generate('app_offer_http_offer_outgoing_export', [
  97.                 'id' => $offer->getId(),
  98.             ]);
  99.         } else {
  100.             $httpViewUrl $this->urlGenerator->generate('app_offer_http_offer_incoming_view', [
  101.                 'id' => $offer->getId(),
  102.             ]);
  103.             $httpExportUrl $this->urlGenerator->generate('app_offer_http_offer_incoming_export', [
  104.                 'id' => $offer->getId(),
  105.             ]);
  106.         }
  107.         $data[OfferInterface::ATTR__LINKS] = [
  108.             'http' => [
  109.                 'view' => $httpViewUrl,
  110.                 'export' => $httpExportUrl,
  111.             ],
  112.         ];
  113.     }
  114. }