vendor/sulu/sulu/src/Sulu/Component/Rest/FlattenExceptionNormalizer.php line 45

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Rest;
  11. use Sulu\Component\Rest\Exception\ReferencingResourcesFoundExceptionInterface;
  12. use Sulu\Component\Rest\Exception\RemoveDependantResourcesFoundExceptionInterface;
  13. use Sulu\Component\Rest\Exception\TranslationErrorMessageExceptionInterface;
  14. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  15. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  16. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. /**
  19.  * @internal the following class is only for internal use don't use it in your project
  20.  */
  21. class FlattenExceptionNormalizer implements ContextAwareNormalizerInterface
  22. {
  23.     /**
  24.      * @var NormalizerInterface
  25.      */
  26.     private $decoratedNormalizer;
  27.     /**
  28.      * @var TranslatorInterface
  29.      */
  30.     private $translator;
  31.     public function __construct(
  32.         NormalizerInterface $decoratedNormalizer,
  33.         TranslatorInterface $translator
  34.     ) {
  35.         $this->decoratedNormalizer $decoratedNormalizer;
  36.         $this->translator $translator;
  37.     }
  38.     public function normalize($exception$format null, array $context = [])
  39.     {
  40.         $data $this->decoratedNormalizer->normalize($exception$format$context);
  41.         $data['code'] = $exception->getCode();
  42.         $contextException $context['exception'] ?? null;
  43.         if ($contextException instanceof TranslationErrorMessageExceptionInterface) {
  44.             // set error message to detail property of response to match rfc 7807
  45.             $data['detail'] = $this->translator->trans(
  46.                 $contextException->getMessageTranslationKey(),
  47.                 $contextException->getMessageTranslationParameters(),
  48.                 'admin'
  49.             );
  50.         }
  51.         if ($context['debug'] ?? false) {
  52.             if ($exception instanceof FlattenException) {
  53.                 $errors $exception->getAsString();
  54.             } else {
  55.                 $errors = (string) $exception;
  56.             }
  57.             $data['errors'] = [$errors];
  58.         }
  59.         if ($contextException instanceof RemoveDependantResourcesFoundExceptionInterface) {
  60.             $data['title'] = $this->translator->trans(
  61.                 $contextException->getTitleTranslationKey(),
  62.                 $contextException->getTitleTranslationParameters(),
  63.                 'admin'
  64.             );
  65.             $data['detail'] = $this->translator->trans(
  66.                 $contextException->getDetailTranslationKey(),
  67.                 $contextException->getDetailTranslationParameters(),
  68.                 'admin'
  69.             );
  70.             $data['dependantResourcesCount'] = $contextException->getDependantResourcesCount();
  71.             $data['dependantResourceBatches'] = $contextException->getDependantResourceBatches();
  72.             $data['resource'] = $contextException->getResource();
  73.         }
  74.         if ($contextException instanceof ReferencingResourcesFoundExceptionInterface) {
  75.             $data['referencingResourcesCount'] = $contextException->getReferencingResourcesCount();
  76.             $data['referencingResources'] = $contextException->getReferencingResources();
  77.             $data['resource'] = $contextException->getResource();
  78.         }
  79.         return $data;
  80.     }
  81.     public function supportsNormalization(mixed $datastring $format null, array $context = []): bool
  82.     {
  83.         return $data instanceof FlattenException && !($context['messenger_serialization'] ?? false);
  84.     }
  85. }