Рефакторинг работы с атрибутами

master
Rinsvent 3 years ago
parent 8d5640cf4e
commit 14746a88bc
  1. 14
      src/EventListener/RequestListener.php

@ -3,12 +3,12 @@
namespace Rinsvent\RequestBundle\EventListener; namespace Rinsvent\RequestBundle\EventListener;
use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerBuilder;
use ReflectionMethod;
use ReflectionObject; use ReflectionObject;
use Rinsvent\RequestBundle\Annotation\RequestDTO; use Rinsvent\RequestBundle\Annotation\RequestDTO;
use Rinsvent\RequestBundle\Annotation\HeaderKey; use Rinsvent\RequestBundle\Annotation\HeaderKey;
use Rinsvent\RequestBundle\DTO\Error; use Rinsvent\RequestBundle\DTO\Error;
use Rinsvent\RequestBundle\DTO\ErrorCollection; use Rinsvent\RequestBundle\DTO\ErrorCollection;
use Rinsvent\AttributeExtractor\MethodExtractor;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\RequestEvent;
@ -23,20 +23,16 @@ class RequestListener
$controller = $request->get('_controller'); $controller = $request->get('_controller');
if (is_string($controller)) { if (is_string($controller)) {
$controller = explode('::', $controller); $controller = explode('::', $controller);
$method = new ReflectionMethod($controller[0], $controller[1]);
} }
if (is_callable($controller)){ if (is_callable($controller)){
$method = new ReflectionMethod($controller[0], $controller[1]); if (is_object($controller[0])) {
$controller[0] = get_class($controller[0]);
} }
if (!isset($method)) { $methodExtractor = new MethodExtractor($controller[0], $controller[1]);
return;
} }
$attributes = $method->getAttributes(RequestDTO::class);
$attribute = $attributes[0] ?? null;
if ($attribute) {
/** @var RequestDTO $requestDTO */ /** @var RequestDTO $requestDTO */
$requestDTO = $attribute->newInstance(); if ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) {
$requestDTOInstance = $this->grabRequestDTO($requestDTO->className, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all()); $requestDTOInstance = $this->grabRequestDTO($requestDTO->className, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all());
$errorCollection = $this->validate($requestDTOInstance); $errorCollection = $this->validate($requestDTOInstance);

Loading…
Cancel
Save