1 | <?php |
||
2 | |||
3 | namespace FaithGen\SDK\Events; |
||
4 | |||
5 | use FaithGen\SDK\Http\Resources\Comment as CommentsResource; |
||
6 | use Illuminate\Broadcasting\InteractsWithSockets; |
||
7 | use Illuminate\Broadcasting\PrivateChannel; |
||
8 | use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; |
||
9 | use Illuminate\Foundation\Events\Dispatchable; |
||
10 | use Illuminate\Queue\SerializesModels; |
||
11 | use Illuminate\Support\Arr; |
||
12 | use Illuminate\Support\Str; |
||
13 | |||
14 | class CommentCreated implements ShouldBroadcastNow |
||
15 | { |
||
16 | use Dispatchable, InteractsWithSockets, SerializesModels; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
17 | |||
18 | protected $comment; |
||
19 | |||
20 | /** |
||
21 | * Create a new event instance. |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | public function __construct($comment) |
||
26 | { |
||
27 | $this->comment = $comment; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get the channels the event should broadcast on. |
||
32 | * |
||
33 | * @return \Illuminate\Broadcasting\Channel|array |
||
34 | */ |
||
35 | public function broadcastOn() |
||
36 | { |
||
37 | $namePieces = explode('\\', $this->comment->commentable_type); |
||
38 | $requiredName = Str::lower(Arr::last($namePieces)); |
||
39 | $requiredName = Str::plural($requiredName); |
||
40 | |||
41 | return new PrivateChannel('comments-'.$requiredName.'-'.$this->comment->commentable_id); |
||
42 | } |
||
43 | |||
44 | public function broadcastWith() |
||
45 | { |
||
46 | return [ |
||
47 | 'comment' => new CommentsResource($this->comment), |
||
48 | ]; |
||
49 | } |
||
50 | |||
51 | public function broadcastAs() |
||
52 | { |
||
53 | return 'comment.created'; |
||
54 | } |
||
55 | } |
||
56 |