forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessExecuteController.php
More file actions
73 lines (66 loc) · 2.86 KB
/
ProcessExecuteController.php
File metadata and controls
73 lines (66 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Controller;
use CleverAge\ProcessBundle\Manager\ProcessManager;
use CleverAge\UiProcessBundle\Http\Model\HttpProcessExecution;
use CleverAge\UiProcessBundle\Message\ProcessExecuteMessage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\ValueResolver;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
#[Route(path: '/http/process/execute', name: 'http_process_execute', methods: ['POST'])]
class ProcessExecuteController extends AbstractController
{
public function __invoke(
#[ValueResolver('http_process_execution')] HttpProcessExecution $httpProcessExecution,
ValidatorInterface $validator,
MessageBusInterface $bus,
ProcessManager $processManager,
): JsonResponse {
$violations = $validator->validate($httpProcessExecution);
if ($violations->count() > 0) {
$violationsMessages = [];
foreach ($violations as $violation) {
$violationsMessages[] = $violation->getMessage();
}
throw new UnprocessableEntityHttpException(implode('. ', $violationsMessages));
}
if ($httpProcessExecution->queue) {
$bus->dispatch(
new ProcessExecuteMessage(
$httpProcessExecution->code ?? '',
$httpProcessExecution->input,
\is_string($httpProcessExecution->context)
? json_decode($httpProcessExecution->context, true)
: $httpProcessExecution->context
)
);
return new JsonResponse('Process has been added to queue. It will start as soon as possible.');
} else {
try {
$processManager->execute(
$httpProcessExecution->code ?? '',
$httpProcessExecution->input,
\is_string($httpProcessExecution->context)
? json_decode($httpProcessExecution->context, true)
: $httpProcessExecution->context
);
} catch (\Throwable $e) {
return new JsonResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return new JsonResponse('Process has been proceed well.');
}
}
}