AshPowell /
APAnalytics
| 1 | <?php |
||
| 2 | |||
| 3 | namespace AshPowell\APAnalytics\Events; |
||
| 4 | |||
| 5 | use Illuminate\Broadcasting\InteractsWithSockets; |
||
| 6 | use Illuminate\Broadcasting\PresenceChannel; |
||
| 7 | use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
||
| 8 | use Illuminate\Foundation\Events\Dispatchable; |
||
| 9 | use Illuminate\Queue\SerializesModels; |
||
| 10 | use Illuminate\Support\Arr; |
||
| 11 | |||
| 12 | class AnalyticTracked implements ShouldBroadcast |
||
| 13 | { |
||
| 14 | use Dispatchable, InteractsWithSockets, SerializesModels; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 15 | |||
| 16 | public $collection; |
||
| 17 | public $basename; |
||
| 18 | public $item; |
||
| 19 | public $itemId; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Create a new event instance. |
||
| 23 | * |
||
| 24 | * @return void |
||
| 25 | * @param mixed $item |
||
| 26 | * @param mixed $basename |
||
| 27 | * @param mixed $collection |
||
| 28 | */ |
||
| 29 | public function __construct($collection, $basename, $item) |
||
| 30 | { |
||
| 31 | $this->queue = 'analytics'; |
||
|
0 ignored issues
–
show
|
|||
| 32 | |||
| 33 | $this->collection = $collection; |
||
| 34 | $this->basename = $basename; |
||
| 35 | $this->item = $item; |
||
| 36 | $this->itemId = Arr::get($this->item, "{$this->basename}.id"); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get the channels the event should broadcast on. |
||
| 41 | * |
||
| 42 | * @return \Illuminate\Broadcasting\Channel|array |
||
| 43 | */ |
||
| 44 | public function broadcastOn() |
||
| 45 | { |
||
| 46 | $postEvent = in_array($this->collection, config('apanalytics.format_collections')); |
||
| 47 | |||
| 48 | if ($postEvent) { |
||
| 49 | return new PresenceChannel("analytics.{$this->collection}.{$this->basename}.{$this->itemId}"); |
||
| 50 | } |
||
| 51 | |||
| 52 | return new PresenceChannel("analytics.{$this->collection}.{$this->basename}.all"); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the data to broadcast. |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function broadcastWith() |
||
| 61 | { |
||
| 62 | $created_at = Arr::get($this->item, 'created_at') ?? mongoTime(); |
||
| 63 | |||
| 64 | return [ |
||
| 65 | 'collection' => $this->collection, |
||
| 66 | 'itemType' => $this->basename, |
||
| 67 | 'itemId' => $this->itemId, |
||
| 68 | 'created_at' => $created_at->toDateTime()->format('c'), |
||
| 69 | ]; |
||
| 70 | } |
||
| 71 | } |
||
| 72 |