Passed
Push — master ( cf340d...4e06a8 )
by Arthur
06:10
created

WebNotification::via()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 14.10.18
6
 * Time: 19:50
7
 */
8
9
namespace Foundation\Abstracts\Notifications;
10
11
12
use Foundation\Channels\DatabaseNotificationChannel;
13
use Foundation\Channels\WebNotificationChannel;
14
use Illuminate\Notifications\Notification;
15
16
abstract class WebNotification extends Notification
17
{
18
    protected $targetModel;
19
20
    /**
21
     * WebNotification constructor.
22
     * @param $targetModel
23
     */
24
    public function __construct($targetModel)
25
    {
26
        $this->targetModel = $targetModel;
27
    }
28
29
    /**
30
     * Get the array representation of the notification.
31
     *
32
     * @param  mixed $notifiable
33
     * @return array
34
     */
35
    public function toDatabase($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function toDatabase(/** @scrutinizer ignore-unused */ $notifiable)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        return [
38
            'title' => $this->title(),
39
            'message' => $this->message(),
40
            'target' => get_short_class_name($this->targetModel),
41
            'target_id' => $this->targetModel->getKey(),
42
            'tag' => $this->tag(),
43
        ];
44
    }
45
46
    /**
47
     * @param $notifiable
48
     * @return array
49
     */
50
    public function toBroadcast($notifiable)
51
    {
52
        $notification = $notifiable->unreadNotifications->last();
53
        return [
54
            'id' => $notification->getKey(),
55
            'target_id' => $this->targetModel->getKey(),
56
            'target' => get_short_class_name($this->targetModel),
57
            'tag' => $this->tag(),
58
            'title' => $this->title(),
59
            'message' => $this->message(),
60
            'is_read' => isset($notification->read_at) ? true : false
61
        ];
62
    }
63
64
    /**
65
     * The title for the web notification
66
     * @return string
67
     */
68
    abstract protected function title(): string;
69
70
    /**
71
     * The message for the web notification
72
     * @return string
73
     */
74
    abstract protected function message(): string;
75
76
    /**
77
     * The tag for the web notification
78
     * success | info | warning | danger
79
     * @return string
80
     */
81
    protected function tag()
82
    {
83
        return 'info';
84
    }
85
86
    /**
87
     * Do not change the order database must be called before broadcast.
88
     * Otherwise we cannot get the appropriate id to broadcast
89
     * @param $notifiable
90
     * @return array
91
     */
92
    public function via($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
    public function via(/** @scrutinizer ignore-unused */ $notifiable)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        return [
95
            DatabaseNotificationChannel::class,
96
            WebNotificationChannel::class
97
        ];
98
    }
99
}
100