vendor/appaydin/pd-api/Listener/RequestBodyTransformerListener.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the pd-admin pd-api package.
  4.  *
  5.  * @package     pd-api
  6.  * @license     LICENSE
  7.  * @author      Ramazan APAYDIN <apaydin541@gmail.com>
  8.  * @link        https://github.com/appaydin/pd-api
  9.  */
  10. namespace Pd\ApiBundle\Listener;
  11. use Pd\ApiBundle\Exception\InvalidRequestBodyException;
  12. use Pd\ApiBundle\PdApiBundle;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Serializer\Encoder\DecoderInterface;
  17. class RequestBodyTransformerListener implements EventSubscriberInterface
  18. {
  19.     public function __construct(private DecoderInterface $decoder)
  20.     {
  21.     }
  22.     public function onKernelRequest(RequestEvent $event): void
  23.     {
  24.         $request $event->getRequest();
  25.         if (!$request->attributes->get(PdApiBundle::ZONE_ATTRIBUTE)) {
  26.             return;
  27.         }
  28.         // XML/JSON Body Decode
  29.         $content $request->getContent();
  30.         if ($content && \in_array($request->getContentType(), ['xml''json'], true)) {
  31.             try {
  32.                 $request->request->replace(
  33.                     $this->decoder->decode($content$request->getContentType())
  34.                 );
  35.             } catch (\Exception $e) {
  36.                 throw new InvalidRequestBodyException();
  37.             }
  38.         }
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [KernelEvents::REQUEST => [['onKernelRequest'100]]];
  43.     }
  44. }