Passed
Push — master ( f218e6...f51c1b )
by Arthur
04:59
created

WebNotification   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tag() 0 3 1
A toBroadcast() 0 12 2
A __construct() 0 3 1
A via() 0 5 1
A toDatabase() 0 8 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 14.10.18
6
 * Time: 19:50.
7
 */
8
9
namespace Modules\Notification\Abstracts;
10
11
use Illuminate\Notifications\Channels\BroadcastChannel;
12
use Illuminate\Notifications\Channels\DatabaseChannel;
13
use Illuminate\Notifications\Notification;
14
15
abstract class WebNotification extends Notification
16
{
17
    private $targetModel;
18
19
    /**
20
     * WebNotification constructor.
21
     *
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
     *
34
     * @return array
35
     */
36
    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

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

100
    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...
101
    {
102
        return [
103
            DatabaseChannel::class,
104
            BroadcastChannel::class,
105
        ];
106
    }
107
}
108