forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteScript.php
More file actions
95 lines (82 loc) · 2.44 KB
/
ExecuteScript.php
File metadata and controls
95 lines (82 loc) · 2.44 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
94
95
<?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\Contracts\ScriptInterface;
use ProcessMaker\Events\ScriptResponseEvent;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\User;
use Throwable;
class ExecuteScript implements ShouldQueue
{
use Dispatchable,
InteractsWithQueue,
Queueable,
SerializesModels;
protected $script;
protected $current_user;
protected $code;
protected $data;
protected $configuration;
protected $watcher;
protected $sync;
/**
* Create a new job instance to execute a script.
*
* @param Script $script
* @param User $current_user
* @param string $code
* @param array $data
* @param $watcher
* @param array $configuration
*/
public function __construct(ScriptInterface $script, User $current_user, $code, array $data, $watcher, array $configuration = [], $sync = false)
{
$this->script = $script;
$this->current_user = $current_user;
$this->code = $code;
$this->data = $data;
$this->configuration = $configuration;
$this->watcher = $watcher;
$this->sync = $sync;
}
/**
* Execute the script task.
*
* @return void
*/
public function handle()
{
//throw new \Exception('This method must be overridden.');
try {
// Just set the code but do not save the object (preview only)
$this->script->code = $this->code;
$response = $this->script->runScript($this->data, $this->configuration);
if ($this->sync) {
return $response;
}
$this->sendResponse(200, $response);
} catch (Throwable $exception) {
if ($this->sync) {
throw $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, $this->watcher));
}
}