1
0
mirror of synced 2024-11-28 15:46:04 +03:00

Update value resolvers

This commit is contained in:
Кривич Сергей 2022-07-20 13:51:43 +03:00
parent 9ef87a2d35
commit f591629786
6 changed files with 10 additions and 102 deletions

View File

@ -5,30 +5,13 @@ namespace RetailCrm\ServiceBundle\ArgumentResolver;
use RetailCrm\ServiceBundle\Exceptions\InvalidRequestArgumentException; use RetailCrm\ServiceBundle\Exceptions\InvalidRequestArgumentException;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Class AbstractValueResolver
*
* @package RetailCrm\ServiceBundle\ArgumentResolver
*/
abstract class AbstractValueResolver abstract class AbstractValueResolver
{ {
protected $validator; public function __construct(
protected ValidatorInterface $validator)
/**
* AbstractValueResolver constructor.
*
* @param ValidatorInterface $validator
*/
public function __construct(ValidatorInterface $validator)
{ {
$this->validator = $validator;
} }
/**
* @param object $data
*
* @return void
*/
protected function validate(object $data): void protected function validate(object $data): void
{ {
$errors = $this->validator->validate($data); $errors = $this->validator->validate($data);

View File

@ -9,37 +9,16 @@ use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Generator; use Generator;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Class CallbackValueResolver
*
* @package RetailCrm\ServiceBundle\ArgumentResolver
*/
class CallbackValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface class CallbackValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface
{ {
private $serializer;
private $requestSchema;
/**
* CallbackValueResolver constructor.
*
* @param Adapter $serializer
* @param ValidatorInterface $validator
* @param array $requestSchema
*/
public function __construct( public function __construct(
Adapter $serializer,
ValidatorInterface $validator, ValidatorInterface $validator,
array $requestSchema private Adapter $serializer,
private array $requestSchema
) { ) {
parent::__construct($validator); parent::__construct($validator);
$this->serializer = $serializer;
$this->requestSchema = $requestSchema;
} }
/**
* {@inheritdoc }
*/
public function supports(Request $request, ArgumentMetadata $argument): bool public function supports(Request $request, ArgumentMetadata $argument): bool
{ {
if (empty($this->requestSchema) || $request->getMethod() !== Request::METHOD_POST) { 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); return null !== $this->search($request, $argument);
} }
/**
* {@inheritdoc }
*/
public function resolve(Request $request, ArgumentMetadata $argument): Generator public function resolve(Request $request, ArgumentMetadata $argument): Generator
{ {
$parameter = $this->search($request, $argument); $parameter = $this->search($request, $argument);
@ -61,12 +37,6 @@ class CallbackValueResolver extends AbstractValueResolver implements ArgumentVal
yield $data; yield $data;
} }
/**
* @param Request $request
* @param ArgumentMetadata $argument
*
* @return string|null
*/
private function search(Request $request, ArgumentMetadata $argument): ?string private function search(Request $request, ArgumentMetadata $argument): ?string
{ {
foreach ($this->requestSchema as $callback) { foreach ($this->requestSchema as $callback) {

View File

@ -9,46 +9,21 @@ use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
use Generator; use Generator;
/**
* Class ClientValueResolver
*
* @package RetailCrm\ServiceBundle\ArgumentResolver
*/
class ClientValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface class ClientValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface
{ {
private $serializer;
private $requestSchema;
/**
* ClientValueResolver constructor.
*
*
* @param Adapter $serializer
* @param ValidatorInterface $validator
* @param array $requestSchema
*/
public function __construct( public function __construct(
Adapter $serializer,
ValidatorInterface $validator, ValidatorInterface $validator,
array $requestSchema private Adapter $serializer,
private array $requestSchema
) { ) {
parent::__construct($validator); parent::__construct($validator);
$this->serializer = $serializer;
$this->requestSchema = $requestSchema;
} }
/**
* {@inheritdoc}
*/
public function supports(Request $request, ArgumentMetadata $argument): bool public function supports(Request $request, ArgumentMetadata $argument): bool
{ {
return in_array($argument->getType(), $this->requestSchema, true); return in_array($argument->getType(), $this->requestSchema, true);
} }
/**
* {@inheritdoc}
*/
public function resolve(Request $request, ArgumentMetadata $argument): Generator public function resolve(Request $request, ArgumentMetadata $argument): Generator
{ {
if (Request::METHOD_GET === $request->getMethod()) { if (Request::METHOD_GET === $request->getMethod()) {
@ -62,23 +37,11 @@ class ClientValueResolver extends AbstractValueResolver implements ArgumentValue
yield $dto; yield $dto;
} }
/**
* @param array $data
* @param string $type
*
* @return object
*/
private function handleGetData(array $data, string $type): object private function handleGetData(array $data, string $type): object
{ {
return $this->serializer->arrayToObject($data, $type); return $this->serializer->arrayToObject($data, $type);
} }
/**
* @param string $data
* @param string $type
*
* @return object
*/
private function handlePostData(string $data, string $type): object private function handlePostData(string $data, string $type): object
{ {
return $this->serializer->deserialize($data, $type); return $this->serializer->deserialize($data, $type);

View File

@ -15,11 +15,6 @@ use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Validation;
use Generator; use Generator;
/**
* Class CallbackValueResolverTest
*
* @package RetailCrm\ServiceBundle\Tests\ArgumentResolver
*/
class CallbackValueResolverTest extends TestCase class CallbackValueResolverTest extends TestCase
{ {
private $resolver; private $resolver;
@ -28,10 +23,11 @@ class CallbackValueResolverTest extends TestCase
{ {
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
$this->resolver = new CallbackValueResolver( $this->resolver = new CallbackValueResolver(
new SymfonySerializerAdapter($serializer, $serializer),
Validation::createValidatorBuilder() Validation::createValidatorBuilder()
->enableAnnotationMapping() ->enableAnnotationMapping()
->addDefaultDoctrineAnnotationReader()
->getValidator(), ->getValidator(),
new SymfonySerializerAdapter($serializer, $serializer),
[ [
[ [
'type' => RequestDto::class, 'type' => RequestDto::class,

View File

@ -15,11 +15,6 @@ use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Validation;
use Generator; use Generator;
/**
* Class ClientValueResolverTest
*
* @package RetailCrm\ServiceBundle\Tests\ArgumentResolver
*/
class ClientValueResolverTest extends TestCase class ClientValueResolverTest extends TestCase
{ {
private $resolver; private $resolver;
@ -28,10 +23,11 @@ class ClientValueResolverTest extends TestCase
{ {
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
$this->resolver = new ClientValueResolver( $this->resolver = new ClientValueResolver(
new SymfonySerializerAdapter($serializer, $serializer),
Validation::createValidatorBuilder() Validation::createValidatorBuilder()
->enableAnnotationMapping() ->enableAnnotationMapping()
->addDefaultDoctrineAnnotationReader()
->getValidator(), ->getValidator(),
new SymfonySerializerAdapter($serializer, $serializer),
[ [
RequestDto::class RequestDto::class
] ]