|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
use Nijens\OpenapiBundle\Deserialization\Attribute\DeserializedObject; |
|
6
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\InvalidRequestBodyProblemException; |
|
7
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\Violation; |
|
8
|
|
|
use Nijens\OpenapiBundle\Routing\RouteContext; |
|
9
|
|
|
use Nijens\OpenapiBundle\Serialization\SerializationContextBuilderInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
|
|
|
|
|
14
|
|
|
use Symfony\Component\Messenger\Stamp\HandledStamp; |
|
|
|
|
|
|
15
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
16
|
|
|
use Symfony\Component\Validator\Validator\ConstraintViolation; |
|
|
|
|
|
|
17
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class CommandController |
|
20
|
|
|
{ |
|
21
|
|
|
private ValidatorInterface $validator; |
|
22
|
|
|
|
|
23
|
|
|
private MessageBusInterface $messageBus; |
|
24
|
|
|
|
|
25
|
|
|
private SerializerInterface $serializer; |
|
26
|
|
|
|
|
27
|
|
|
private SerializationContextBuilderInterface $serializationContextBuilder; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
ValidatorInterface $validator, |
|
31
|
|
|
MessageBusInterface $messageBus, |
|
32
|
|
|
SerializerInterface $serializer, |
|
33
|
|
|
SerializationContextBuilderInterface $serializationContextBuilder |
|
34
|
|
|
) { |
|
35
|
|
|
$this->validator = $validator; |
|
36
|
|
|
$this->messageBus = $messageBus; |
|
37
|
|
|
$this->serializer = $serializer; |
|
38
|
|
|
$this->serializationContextBuilder = $serializationContextBuilder; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function __invoke( |
|
42
|
|
|
Request $request, |
|
43
|
|
|
#[DeserializedObject] $command, |
|
44
|
|
|
string $responseSerializationSchemaObject |
|
45
|
|
|
): JsonResponse { |
|
46
|
|
|
$this->validateCommand($command); |
|
47
|
|
|
|
|
48
|
|
|
$message = $this->messageBus->dispatch($command); |
|
49
|
|
|
|
|
50
|
|
|
/** @var HandledStamp $handledStamp */ |
|
51
|
|
|
$handledStamp = $message->last(HandledStamp::class); |
|
52
|
|
|
$result = $handledStamp->getResult(); |
|
53
|
|
|
|
|
54
|
|
|
$serializationContext = $this->serializationContextBuilder->getContextForSchemaObject( |
|
55
|
|
|
$responseSerializationSchemaObject, |
|
56
|
|
|
$request->attributes->get(RouteContext::REQUEST_ATTRIBUTE)[RouteContext::RESOURCE] |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
return JsonResponse::fromJsonString( |
|
60
|
|
|
$this->serializer->serialize($result, 'json', $serializationContext) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function validateCommand($command): void |
|
65
|
|
|
{ |
|
66
|
|
|
$validationErrors = $this->validator->validate($command); |
|
67
|
|
|
if (count($validationErrors) > 0) { |
|
68
|
|
|
$exception = new InvalidRequestBodyProblemException( |
|
69
|
|
|
'about:blank', |
|
70
|
|
|
'The request body contains errors.', |
|
71
|
|
|
Response::HTTP_BAD_REQUEST |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$violations = array_map( |
|
75
|
|
|
function (ConstraintViolation $validationError): Violation { |
|
76
|
|
|
return new Violation( |
|
77
|
|
|
$validationError->getConstraint(), |
|
78
|
|
|
$validationError->getMessage(), |
|
79
|
|
|
$validationError->getPropertyPath() |
|
80
|
|
|
); |
|
81
|
|
|
}, |
|
82
|
|
|
$validationErrors |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
throw $exception->withViolations($violations); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths