1313
1414namespace CleverAge \UiProcessBundle \Controller ;
1515
16+ use CleverAge \ProcessBundle \Manager \ProcessManager ;
1617use CleverAge \UiProcessBundle \Http \Model \HttpProcessExecution ;
1718use CleverAge \UiProcessBundle \Message \ProcessExecuteMessage ;
1819use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
1920use Symfony \Component \HttpFoundation \JsonResponse ;
21+ use Symfony \Component \HttpFoundation \Response ;
2022use Symfony \Component \HttpKernel \Attribute \ValueResolver ;
2123use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
2224use Symfony \Component \Messenger \MessageBusInterface ;
@@ -30,6 +32,7 @@ public function __invoke(
3032 #[ValueResolver('http_process_execution ' )] HttpProcessExecution $ httpProcessExecution ,
3133 ValidatorInterface $ validator ,
3234 MessageBusInterface $ bus ,
35+ ProcessManager $ processManager ,
3336 ): JsonResponse {
3437 $ violations = $ validator ->validate ($ httpProcessExecution );
3538 if ($ violations ->count () > 0 ) {
@@ -39,16 +42,32 @@ public function __invoke(
3942 }
4043 throw new UnprocessableEntityHttpException (implode ('. ' , $ violationsMessages ));
4144 }
42- $ bus ->dispatch (
43- new ProcessExecuteMessage (
44- $ httpProcessExecution ->code ?? '' ,
45- $ httpProcessExecution ->input ,
46- \is_string ($ httpProcessExecution ->context )
47- ? json_decode ($ httpProcessExecution ->context , true )
48- : $ httpProcessExecution ->context
49- )
50- );
45+ if ($ httpProcessExecution ->queue ) {
46+ $ bus ->dispatch (
47+ new ProcessExecuteMessage (
48+ $ httpProcessExecution ->code ?? '' ,
49+ $ httpProcessExecution ->input ,
50+ \is_string ($ httpProcessExecution ->context )
51+ ? json_decode ($ httpProcessExecution ->context , true )
52+ : $ httpProcessExecution ->context
53+ )
54+ );
5155
52- return new JsonResponse ('Process has been added to queue. It will start as soon as possible. ' );
56+ return new JsonResponse ('Process has been added to queue. It will start as soon as possible. ' );
57+ } else {
58+ try {
59+ $ processManager ->execute (
60+ $ httpProcessExecution ->code ?? '' ,
61+ $ httpProcessExecution ->input ,
62+ \is_string ($ httpProcessExecution ->context )
63+ ? json_decode ($ httpProcessExecution ->context , true )
64+ : $ httpProcessExecution ->context
65+ );
66+ } catch (\Throwable $ e ) {
67+ return new JsonResponse ($ e ->getMessage (), Response::HTTP_INTERNAL_SERVER_ERROR );
68+ }
69+
70+ return new JsonResponse ('Process has been proceed well. ' );
71+ }
5372 }
5473}
0 commit comments