-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathWaypointActivityChanged.php
More file actions
119 lines (105 loc) · 3.06 KB
/
Copy pathWaypointActivityChanged.php
File metadata and controls
119 lines (105 loc) · 3.06 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Fleetbase\FleetOps\Events;
use Fleetbase\FleetOps\Flow\Activity;
use Fleetbase\FleetOps\Models\Order;
use Fleetbase\FleetOps\Models\Waypoint;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
class WaypointActivityChanged implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;
/**
* The waypoint which is completed.
*/
public ?Waypoint $waypoint = null;
/**
* The activity which triggered the waypoint completed.
*/
public ?Activity $activity = null;
/**
* The event id.
*
* @var string
*/
public $eventId;
/**
* The datetime instance the broadcast ws triggered.
*
* @var string
*/
public $sentAt;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Waypoint $waypoint, Activity $activity)
{
$this->waypoint = $waypoint;
$this->activity = $activity;
$this->eventId = uniqid('event_');
$this->sentAt = Carbon::now()->toDateTimeString();
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
$channels = [
new Channel('api.' . session('api_credential')),
new Channel('waypoint.' . $this->waypoint->public_id),
new Channel('waypoint.' . $this->waypoint->uuid),
];
$order = $this->getModelRecord();
if ($order) {
$channels[] = new Channel('company.' . session('company', data_get($order, 'company.uuid')));
$channels[] = new Channel('company.' . data_get($order, 'company.public_id'));
$channels[] = new Channel('order.' . $order->uuid);
$channels[] = new Channel('order.' . $order->public_id);
}
return $channels;
}
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'waypoint.activity';
}
/**
* Get the data to broadcast.
*
* @return array
*/
public function broadcastWith()
{
return [
'id' => $this->eventId,
'api_version' => config('api.version'),
'event' => $this->broadcastAs(),
'created_at' => $this->sentAt,
'data' => [
'waypoint' => $this->waypoint->public_id,
'place' => data_get($this->waypoint, 'place.public_id'),
'activity' => $this->activity->toArray(),
],
];
}
/**
* Get the assosciated order model record for this waypoint.
*/
public function getModelRecord(): ?Order
{
return Order::where('payload_uuid', $this->waypoint->payload_uuid)->first();
}
}