forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestScript.php
More file actions
93 lines (80 loc) · 2.6 KB
/
TestScript.php
File metadata and controls
93 lines (80 loc) · 2.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace ProcessMaker\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use ProcessMaker\Enums\ScriptExecutorType;
use ProcessMaker\Events\ScriptResponseEvent;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\User;
use Throwable;
class TestScript implements ShouldQueue
{
use Dispatchable,
InteractsWithQueue,
Queueable,
SerializesModels;
protected $script;
protected $current_user;
protected $code;
protected $data;
protected $configuration;
protected $nonce;
/**
* Create a new job instance to execute a script.
*
* @param ProcessMaker\Models\Script $script
* @param ProcessMaker\Models\User $current_user
* @param string $code
* @param array $data
* @param array $configuration
*/
public function __construct(Script $script, User $current_user, $code, array $data, array $configuration, $nonce = null)
{
$this->script = $script;
$this->current_user = $current_user;
$this->code = $code;
$this->data = $data;
$this->configuration = $configuration;
$this->nonce = $nonce;
}
/**
* Execute the script task.
*
* @return void
*/
public function handle()
{
try {
// Just set the code but do not save the object (preview only)
$this->script->code = $this->code;
$metadata = [
'nonce' => $this->nonce,
'current_user' => $this->current_user?->id,
];
$response = $this->script->runScript($this->data, $this->configuration, '', null, 0, $metadata);
\Log::debug('Response from runScript: ' . print_r($response, true));
if (!config('script-runner-microservice.enabled') ||
$this->script->scriptExecutor && $this->script->scriptExecutor->type === ScriptExecutorType::Custom) {
$this->sendResponse(200, $response);
}
} catch (Throwable $exception) {
$this->sendResponse(500, [
'exception' => get_class($exception),
'message' => $exception->getMessage(),
]);
}
}
/**
* Send a response to the user interface
*
* @param int $status
* @param array $response
*/
private function sendResponse($status, array $response)
{
event(new ScriptResponseEvent($this->current_user, $status, $response, null, $this->nonce));
}
}