1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ReliQArts\Mardin\Events; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Manager; |
6
|
|
|
use League\Fractal\Resource\Item; |
7
|
|
|
use Illuminate\Broadcasting\Channel; |
8
|
|
|
use Illuminate\Queue\SerializesModels; |
9
|
|
|
use ReliQArts\Mardin\Contracts\Message; |
10
|
|
|
use Illuminate\Broadcasting\PrivateChannel; |
11
|
|
|
use Illuminate\Broadcasting\InteractsWithSockets; |
12
|
|
|
use ReliQArts\Mardin\Transformers\MessageTransformer; |
13
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
14
|
|
|
|
15
|
|
|
class NewMessage implements ShouldBroadcast |
16
|
|
|
{ |
17
|
|
|
use InteractsWithSockets, SerializesModels; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name of the queue on which to place the event. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $broadcastQueue = 'mardin'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array Channels for message. |
28
|
|
|
*/ |
29
|
|
|
private $channels; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Message Message. |
33
|
|
|
*/ |
34
|
|
|
public $message; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new event instance. |
38
|
|
|
* |
39
|
|
|
* @return void |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function __construct(Message $message) |
42
|
|
|
{ |
43
|
|
|
$this->message = $message; |
44
|
|
|
$this->channels = []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The event's broadcast name. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function broadcastAs() |
53
|
|
|
{ |
54
|
|
|
return 'newMessage'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the channels the event should broadcast on. |
59
|
|
|
* |
60
|
|
|
* @return Channel|array |
61
|
|
|
*/ |
62
|
|
|
public function broadcastOn() |
63
|
|
|
{ |
64
|
|
|
// Get channels for Broadcast |
65
|
|
|
foreach ($this->message->thread->participantsUserIds() as $recipient) { |
|
|
|
|
66
|
|
|
if ($recipient == $this->message->user->id) { |
|
|
|
|
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
$this->channels[] = new PrivateChannel("Mardin.Messages.User.{$recipient}"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Set Message as Fractal Item |
73
|
|
|
$fractal = new Manager; |
74
|
|
|
$fractal->parseIncludes('thread'); |
75
|
|
|
$this->message = $fractal->createData(new Item($this->message, new MessageTransformer))->toArray(); |
76
|
|
|
|
77
|
|
|
return $this->channels; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.