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 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.
|
|
|
|
*
|
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
|
|
|
|
{
|
|
|
|
if (empty($this->requestSchema) || $request->getMethod() !== Request::METHOD_POST) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null !== $this->search($request, $argument);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc }
|
|
|
|
*/
|
|
|
|
public function resolve(Request $request, ArgumentMetadata $argument): Generator
|
|
|
|
{
|
|
|
|
$parameter = $this->search($request, $argument);
|
2021-02-17 09:31:36 +03:00
|
|
|
$data = $this->serializer->deserialize($request->request->get($parameter), $argument->getType());
|
2021-02-05 14:47:54 +03:00
|
|
|
$this->validate($data);
|
|
|
|
|
|
|
|
yield $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param ArgumentMetadata $argument
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
private function search(Request $request, ArgumentMetadata $argument): ?string
|
|
|
|
{
|
|
|
|
foreach ($this->requestSchema as $callback) {
|
|
|
|
if ($argument->getType() !== $callback['type']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($callback['params'] as $param) {
|
|
|
|
if ($request->request->has($param)) {
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|