DatabaseChannel::getData()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 4
cts 8
cp 0.5
rs 9.2
cc 4
eloc 8
nc 4
nop 2
crap 6
1
<?php
2
3
namespace Illuminate\Notifications\Channels;
4
5
use RuntimeException;
6
use Illuminate\Notifications\Notification;
7
8
class DatabaseChannel
9
{
10
    /**
11
     * Send the given notification.
12
     *
13
     * @param  mixed  $notifiable
14
     * @param  \Illuminate\Notifications\Notification  $notification
15
     * @return void
16
     */
17 1
    public function send($notifiable, Notification $notification)
18
    {
19 1
        $notifiable->routeNotificationFor('database')->create([
20 1
            'id' => $notification->id,
21 1
            'type' => get_class($notification),
22 1
            'data' => $this->getData($notifiable, $notification),
23
            'read_at' => null,
24
        ]);
25 1
    }
26
27
    /**
28
     * Get the data for the notification.
29
     *
30
     * @param  mixed  $notifiable
31
     * @param  \Illuminate\Notifications\Notification  $notification
32
     * @return array
33
     *
34
     * @throws \RuntimeException
35
     */
36 1
    protected function getData($notifiable, Notification $notification)
37
    {
38 1
        if (method_exists($notification, 'toDatabase')) {
39 1
            $data = $notification->toDatabase($notifiable);
0 ignored issues
show
Bug introduced by
The method toDatabase() 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...
40
41 1
            return is_array($data) ? $data : $data->data;
42
        } elseif (method_exists($notification, 'toArray')) {
43
            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...
44
        }
45
46
        throw new RuntimeException(
47
            'Notification is missing toDatabase / toArray method.'
48
        );
49
    }
50
}
51