Completed
Pull Request — master (#23)
by
unknown
09:20
created

BroadcastNotificationCreated::broadcastOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Illuminate\Notifications\Events;
4
5
use Illuminate\Queue\SerializesModels;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Broadcasting\PrivateChannel;
8
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
9
10
class BroadcastNotificationCreated implements ShouldBroadcast
11
{
12
    use SerializesModels;
13
14
    /**
15
     * The notifiable entity who received the notification.
16
     *
17
     * @var mixed
18
     */
19
    public $notifiable;
20
21
    /**
22
     * The notification instance.
23
     *
24
     * @var \Illuminate\Notifications\Notification
25
     */
26
    public $notification;
27
28
    /**
29
     * The notification data.
30
     *
31
     * @var array
32
     */
33
    public $data = [];
34
35
    /**
36
     * Create a new event instance.
37
     *
38
     * @param  mixed  $notifiable
39
     * @param  \Illuminate\Notifications\Notification  $notification
40
     * @param  array  $data
41
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
42
     */
43
    public function __construct($notifiable, $notification, $data)
44
    {
45
        $this->data = $data;
46
        $this->notifiable = $notifiable;
47
        $this->notification = $notification;
48
    }
49
50
    /**
51
     * Get the channels the event should broadcast on.
52
     *
53
     * @return array
54
     */
55
    public function broadcastOn()
56
    {
57
        return [new PrivateChannel($this->channelName())];
58
    }
59
60
    /**
61
     * Get the data that should be sent with the broadcasted event.
62
     *
63
     * @return array
64
     */
65
    public function broadcastWith()
66
    {
67
        return array_merge($this->data, [
68
            'id' => $this->notification->id,
69
            'type' => get_class($this->notification),
70
        ]);
71
    }
72
73
    /**
74
     * Get the broadcast channel name for the event.
75
     *
76
     * @return string
77
     */
78
    protected function channelName()
79
    {
80
        $class = str_replace('\\', '.', get_class($this->notifiable));
81
82
        return $class.'.'.$this->notifiable->getKey();
83
    }
84
}
85