From f59162978676962fd6374a8e262c1f355d859294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D1=80=D0=B8=D0=B2=D0=B8=D1=87=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9?= Date: Wed, 20 Jul 2022 13:51:43 +0300 Subject: [PATCH] Update value resolvers --- ArgumentResolver/AbstractValueResolver.php | 21 +--------- ArgumentResolver/CallbackValueResolver.php | 34 +-------------- ArgumentResolver/ClientValueResolver.php | 41 +------------------ .../CallbackValueResolverTest.php | 8 +--- .../ClientValueResolverTest.php | 8 +--- ...rTest.php => JMSSerializerAdapterTest.php} | 0 6 files changed, 10 insertions(+), 102 deletions(-) rename Tests/Serializer/{JSMSerializerAdapterTest.php => JMSSerializerAdapterTest.php} (100%) diff --git a/ArgumentResolver/AbstractValueResolver.php b/ArgumentResolver/AbstractValueResolver.php index e840f02..272bbef 100644 --- a/ArgumentResolver/AbstractValueResolver.php +++ b/ArgumentResolver/AbstractValueResolver.php @@ -5,30 +5,13 @@ namespace RetailCrm\ServiceBundle\ArgumentResolver; use RetailCrm\ServiceBundle\Exceptions\InvalidRequestArgumentException; use Symfony\Component\Validator\Validator\ValidatorInterface; -/** - * Class AbstractValueResolver - * - * @package RetailCrm\ServiceBundle\ArgumentResolver - */ abstract class AbstractValueResolver { - protected $validator; - - /** - * AbstractValueResolver constructor. - * - * @param ValidatorInterface $validator - */ - public function __construct(ValidatorInterface $validator) + public function __construct( + protected ValidatorInterface $validator) { - $this->validator = $validator; } - /** - * @param object $data - * - * @return void - */ protected function validate(object $data): void { $errors = $this->validator->validate($data); diff --git a/ArgumentResolver/CallbackValueResolver.php b/ArgumentResolver/CallbackValueResolver.php index 3c412e5..cee8f18 100644 --- a/ArgumentResolver/CallbackValueResolver.php +++ b/ArgumentResolver/CallbackValueResolver.php @@ -9,37 +9,16 @@ use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; use Generator; use Symfony\Component\Validator\Validator\ValidatorInterface; -/** - * Class CallbackValueResolver - * - * @package RetailCrm\ServiceBundle\ArgumentResolver - */ class CallbackValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface { - private $serializer; - private $requestSchema; - - /** - * CallbackValueResolver constructor. - * - * @param Adapter $serializer - * @param ValidatorInterface $validator - * @param array $requestSchema - */ public function __construct( - Adapter $serializer, ValidatorInterface $validator, - array $requestSchema + private Adapter $serializer, + private array $requestSchema ) { parent::__construct($validator); - - $this->serializer = $serializer; - $this->requestSchema = $requestSchema; } - /** - * {@inheritdoc } - */ public function supports(Request $request, ArgumentMetadata $argument): bool { if (empty($this->requestSchema) || $request->getMethod() !== Request::METHOD_POST) { @@ -49,9 +28,6 @@ class CallbackValueResolver extends AbstractValueResolver implements ArgumentVal return null !== $this->search($request, $argument); } - /** - * {@inheritdoc } - */ public function resolve(Request $request, ArgumentMetadata $argument): Generator { $parameter = $this->search($request, $argument); @@ -61,12 +37,6 @@ class CallbackValueResolver extends AbstractValueResolver implements ArgumentVal yield $data; } - /** - * @param Request $request - * @param ArgumentMetadata $argument - * - * @return string|null - */ private function search(Request $request, ArgumentMetadata $argument): ?string { foreach ($this->requestSchema as $callback) { diff --git a/ArgumentResolver/ClientValueResolver.php b/ArgumentResolver/ClientValueResolver.php index 7d9ed18..ded400e 100644 --- a/ArgumentResolver/ClientValueResolver.php +++ b/ArgumentResolver/ClientValueResolver.php @@ -9,46 +9,21 @@ use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; use Symfony\Component\Validator\Validator\ValidatorInterface; use Generator; -/** - * Class ClientValueResolver - * - * @package RetailCrm\ServiceBundle\ArgumentResolver - */ class ClientValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface { - private $serializer; - private $requestSchema; - - /** - * ClientValueResolver constructor. - * - * - * @param Adapter $serializer - * @param ValidatorInterface $validator - * @param array $requestSchema - */ public function __construct( - Adapter $serializer, ValidatorInterface $validator, - array $requestSchema + private Adapter $serializer, + private array $requestSchema ) { parent::__construct($validator); - - $this->serializer = $serializer; - $this->requestSchema = $requestSchema; } - /** - * {@inheritdoc} - */ public function supports(Request $request, ArgumentMetadata $argument): bool { return in_array($argument->getType(), $this->requestSchema, true); } - /** - * {@inheritdoc} - */ public function resolve(Request $request, ArgumentMetadata $argument): Generator { if (Request::METHOD_GET === $request->getMethod()) { @@ -62,23 +37,11 @@ class ClientValueResolver extends AbstractValueResolver implements ArgumentValue yield $dto; } - /** - * @param array $data - * @param string $type - * - * @return object - */ private function handleGetData(array $data, string $type): object { return $this->serializer->arrayToObject($data, $type); } - /** - * @param string $data - * @param string $type - * - * @return object - */ private function handlePostData(string $data, string $type): object { return $this->serializer->deserialize($data, $type); diff --git a/Tests/ArgumentResolver/CallbackValueResolverTest.php b/Tests/ArgumentResolver/CallbackValueResolverTest.php index c2d0c52..240ebd0 100644 --- a/Tests/ArgumentResolver/CallbackValueResolverTest.php +++ b/Tests/ArgumentResolver/CallbackValueResolverTest.php @@ -15,11 +15,6 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\Validation; use Generator; -/** - * Class CallbackValueResolverTest - * - * @package RetailCrm\ServiceBundle\Tests\ArgumentResolver - */ class CallbackValueResolverTest extends TestCase { private $resolver; @@ -28,10 +23,11 @@ class CallbackValueResolverTest extends TestCase { $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); $this->resolver = new CallbackValueResolver( - new SymfonySerializerAdapter($serializer, $serializer), Validation::createValidatorBuilder() ->enableAnnotationMapping() + ->addDefaultDoctrineAnnotationReader() ->getValidator(), + new SymfonySerializerAdapter($serializer, $serializer), [ [ 'type' => RequestDto::class, diff --git a/Tests/ArgumentResolver/ClientValueResolverTest.php b/Tests/ArgumentResolver/ClientValueResolverTest.php index d281863..c473941 100644 --- a/Tests/ArgumentResolver/ClientValueResolverTest.php +++ b/Tests/ArgumentResolver/ClientValueResolverTest.php @@ -15,11 +15,6 @@ use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\Validation; use Generator; -/** - * Class ClientValueResolverTest - * - * @package RetailCrm\ServiceBundle\Tests\ArgumentResolver - */ class ClientValueResolverTest extends TestCase { private $resolver; @@ -28,10 +23,11 @@ class ClientValueResolverTest extends TestCase { $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); $this->resolver = new ClientValueResolver( - new SymfonySerializerAdapter($serializer, $serializer), Validation::createValidatorBuilder() ->enableAnnotationMapping() + ->addDefaultDoctrineAnnotationReader() ->getValidator(), + new SymfonySerializerAdapter($serializer, $serializer), [ RequestDto::class ] diff --git a/Tests/Serializer/JSMSerializerAdapterTest.php b/Tests/Serializer/JMSSerializerAdapterTest.php similarity index 100% rename from Tests/Serializer/JSMSerializerAdapterTest.php rename to Tests/Serializer/JMSSerializerAdapterTest.php