2021-02-05 14:47:54 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\ServiceBundle\ArgumentResolver;
|
|
|
|
|
2021-02-17 09:31:36 +03:00
|
|
|
use RetailCrm\ServiceBundle\Serializer\Adapter;
|
2021-02-05 14:47:54 +03:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
|
|
|
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.
|
|
|
|
*
|
2021-02-17 09:31:36 +03:00
|
|
|
*
|
|
|
|
* @param Adapter $serializer
|
2021-02-05 14:47:54 +03:00
|
|
|
* @param ValidatorInterface $validator
|
|
|
|
* @param array $requestSchema
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2021-02-17 09:31:36 +03:00
|
|
|
Adapter $serializer,
|
2021-02-05 14:47:54 +03:00
|
|
|
ValidatorInterface $validator,
|
|
|
|
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()) {
|
|
|
|
$dto = $this->handleGetData($request->query->all(), $argument->getType());
|
|
|
|
} else {
|
|
|
|
$dto = $this->handlePostData($request->getContent(), $argument->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validate($dto);
|
|
|
|
|
|
|
|
yield $dto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
private function handleGetData(array $data, string $type): object
|
|
|
|
{
|
2021-02-17 09:31:36 +03:00
|
|
|
return $this->serializer->arrayToObject($data, $type);
|
2021-02-05 14:47:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $data
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
private function handlePostData(string $data, string $type): object
|
|
|
|
{
|
2021-02-17 09:31:36 +03:00
|
|
|
return $this->serializer->deserialize($data, $type);
|
2021-02-05 14:47:54 +03:00
|
|
|
}
|
|
|
|
}
|