1313
1414namespace CleverAge \UiProcessBundle \Controller \Admin \Process ;
1515
16+ use CleverAge \ProcessBundle \Exception \MissingProcessException ;
1617use CleverAge \UiProcessBundle \Entity \User ;
1718use CleverAge \UiProcessBundle \Form \Type \LaunchType ;
1819use CleverAge \UiProcessBundle \Manager \ProcessConfigurationsManager ;
3940#[IsGranted('ROLE_USER ' )]
4041class LaunchAction extends AbstractController
4142{
43+ public function __construct (private readonly MessageBusInterface $ messageBus )
44+ {
45+ }
46+
4247 public function __invoke (
4348 RequestStack $ requestStack ,
44- MessageBusInterface $ messageBus ,
4549 string $ uploadDirectory ,
4650 ProcessConfigurationsManager $ processConfigurationsManager ,
4751 AdminContext $ context ,
4852 ): Response {
49- $ uiOptions = $ processConfigurationsManager ->getUiOptions ($ requestStack ->getMainRequest ()?->get('process ' ) ?? '' );
53+ $ processCode = $ requestStack ->getMainRequest ()?->get('process ' );
54+ if (null === $ processCode ) {
55+ throw new MissingProcessException ();
56+ }
57+ $ uiOptions = $ processConfigurationsManager ->getUiOptions ($ processCode );
58+ if (null === $ uiOptions ) {
59+ throw new \InvalidArgumentException ('Missing UI Options ' );
60+ }
61+ if (null === $ uiOptions ['ui_launch_mode ' ] || 'modal ' === $ uiOptions ['ui_launch_mode ' ]) {
62+ $ this ->dispatch ($ processCode );
63+ $ this ->addFlash (
64+ 'success ' ,
65+ 'Process has been added to queue. It will start as soon as possible '
66+ );
67+
68+ return $ this ->redirectToRoute ('process ' , ['routeName ' => 'process_list ' ]);
69+ }
5070 $ form = $ this ->createForm (
5171 LaunchType::class,
5272 null ,
5373 [
54- 'constraints ' => $ uiOptions ['constraints ' ] ?? [] ,
55- 'process_code ' => $ requestStack -> getMainRequest ()?->get( ' process ' ) ,
74+ 'constraints ' => $ uiOptions ['constraints ' ],
75+ 'process_code ' => $ processCode ,
5676 ]
5777 );
5878 if (false === $ form ->isSubmitted ()) {
59- $ default = $ uiOptions ['default ' ] ?? [] ;
79+ $ default = $ uiOptions ['default ' ];
6080 if (false === $ form ->get ('input ' )->getConfig ()->getType ()->getInnerType () instanceof TextType
6181 && isset ($ default ['input ' ])
6282 ) {
@@ -72,16 +92,11 @@ public function __invoke(
7292 (new Filesystem ())->dumpFile ($ filename , $ input ->getContent ());
7393 $ input = $ filename ;
7494 }
75-
76- $ message = new ProcessExecuteMessage (
95+ $ this ->dispatch (
7796 $ form ->getConfig ()->getOption ('process_code ' ),
7897 $ input ,
79- array_merge (
80- ['execution_user ' => $ this ->getUser ()?->getEmail()],
81- $ form ->get ('context ' )->getData ()
82- )
98+ $ form ->get ('context ' )->getData ()
8399 );
84- $ messageBus ->dispatch ($ message );
85100 $ this ->addFlash (
86101 'success ' ,
87102 'Process has been added to queue. It will start as soon as possible '
@@ -99,6 +114,22 @@ public function __invoke(
99114 );
100115 }
101116
117+ /**
118+ * @param mixed[] $context
119+ */
120+ protected function dispatch (string $ processCode , mixed $ input = null , array $ context = []): void
121+ {
122+ $ message = new ProcessExecuteMessage (
123+ $ processCode ,
124+ $ input ,
125+ array_merge (
126+ ['execution_user ' => $ this ->getUser ()?->getEmail()],
127+ $ context
128+ )
129+ );
130+ $ this ->messageBus ->dispatch ($ message );
131+ }
132+
102133 protected function getUser (): ?User
103134 {
104135 /** @var User $user */
0 commit comments