forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptResponseEvent.php
More file actions
80 lines (66 loc) · 1.92 KB
/
ScriptResponseEvent.php
File metadata and controls
80 lines (66 loc) · 1.92 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
<?php
namespace ProcessMaker\Events;
use Carbon\Carbon;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use ProcessMaker\Models\User;
class ScriptResponseEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $userId;
public $status;
public $response;
public $watcher;
public $nonce;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user, $status, array $response, $watcher = null, $nonce = null)
{
$this->userId = $user->id;
$this->status = $status;
$this->response = $response;
$this->watcher = $watcher;
$this->nonce = $nonce;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('ProcessMaker.Models.User.' . $this->userId);
}
/**
* Cache the script response to be loaded by API
*
* @return string
*/
private function cacheResponse()
{
$key = uniqid('srn', true);
Cache::put("srn.$key", $this->response, now()->addMinutes(1));
return ['key' => $key];
}
public function broadcastWith()
{
$date = new Carbon();
$response = $this->cacheResponse($this->response);
return [
'type' => '.' . \get_class($this),
'name' => __('Script executed'),
'dateTime' => $date->toIso8601String(),
'status' => $this->status,
'watcher' => $this->watcher,
'response' => $response,
'nonce' => $this->nonce,
];
}
}