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

BroadcastChannel::__construct()   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 1
crap 2
1
<?php
2
3
namespace Illuminate\Notifications\Channels;
4
5
use RuntimeException;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Notifications\Events\BroadcastNotificationCreated;
9
10
class BroadcastChannel
11
{
12
    /**
13
     * The event dispatcher.
14
     *
15
     * @var \Illuminate\Contracts\Events\Dispatcher
16
     */
17
    protected $events;
18
19
    /**
20
     * Create a new database channel.
21
     *
22
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
23
     * @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...
24
     */
25
    public function __construct(Dispatcher $events)
26
    {
27
        $this->events = $events;
28
    }
29
30
    /**
31
     * Send the given notification.
32
     *
33
     * @param  mixed  $notifiable
34
     * @param  \Illuminate\Notifications\Notification  $notification
35
     * @return void
36
     */
37
    public function send($notifiable, Notification $notification)
38
    {
39
        $this->events->fire(new BroadcastNotificationCreated(
40
            $notifiable, $notification, $this->getData($notifiable, $notification)
41
        ));
42
    }
43
44
    /**
45
     * Get the data for the notification.
46
     *
47
     * @param  mixed  $notifiable
48
     * @param  \Illuminate\Notifications\Notification  $notification
49
     * @return array
50
     *
51
     * @throws \RuntimeException
52
     */
53
    protected function getData($notifiable, Notification $notification)
54
    {
55
        if (method_exists($notification, 'toBroadcast')) {
56
            return $notification->toBroadcast($notifiable);
0 ignored issues
show
Bug introduced by
The method toBroadcast() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        }
58
59
        if (method_exists($notification, 'toArray')) {
60
            return $notification->toArray($notifiable);
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
        }
62
63
        throw new RuntimeException(
64
            'Notification is missing toArray method.'
65
        );
66
    }
67
}
68