Passed
Push — master ( 3a97d5...2b44a8 )
by Adam
21:57 queued 12s
created

FlagCreatedNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Notifications;
4
5
use Coyote\Flag;
6
use Coyote\Services\Notification\DatabaseChannel;
7
use Coyote\Services\Notification\Notification;
8
use Coyote\User;
9
use Illuminate\Bus\Queueable;
10
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
11
use Illuminate\Contracts\Queue\ShouldQueue;
12
use Illuminate\Notifications\Channels\BroadcastChannel;
13
use Illuminate\Notifications\Messages\BroadcastMessage;
14
use NotificationChannels\WebPush\WebPushChannel;
15
use NotificationChannels\WebPush\WebPushMessage;
16
17
class FlagCreatedNotification extends Notification implements ShouldQueue, ShouldBroadcast
18
{
19
    use Queueable;
20
21
    const ID = \Coyote\Notification::FLAG;
22
23
    /**
24
     * @var Flag
25
     */
26
    private $flag;
27
28
    /**
29
     * @param Flag $flag
30
     */
31
    public function __construct(Flag $flag)
32
    {
33
        $this->flag = $flag;
34
    }
35
36
    /**
37
     * Get the notification's delivery channels.
38
     *
39
     * @param  \Coyote\User  $user
40
     * @return array
41
     */
42
    public function via(User $user)
43
    {
44
        $this->broadcastChannel = 'user:' . $user->id;
45
46
        return $this->channels($user);
47
    }
48
49
    /**
50
     * @param \Coyote\User $user
51
     * @return array
52
     */
53
    public function toDatabase($user)
54
    {
55
        return [
56
            'object_id'     => $this->objectId(),
57
            'user_id'       => $user->id,
58
            'type_id'       => static::ID,
59
            'subject'       => $this->flag->type->name,
60
            'excerpt'       => str_limit($this->flag->text, 250),
61
            'url'           => $this->flag->url,
62
            'id'            => $this->id
63
        ];
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function sender()
70
    {
71
        return [
72
            'user_id'       => $this->flag->user_id,
73
            'name'          => $this->flag->user->name
74
        ];
75
    }
76
77
    /**
78
     * Generowanie unikalnego ciagu znakow dla wpisu na mikro
79
     *
80
     * @return string
81
     */
82
    public function objectId()
83
    {
84
        return substr(md5(static::ID . $this->flag->url), 16);
85
    }
86
87
    /**
88
     * @return BroadcastMessage
89
     */
90
    public function toBroadcast()
91
    {
92
        return new BroadcastMessage([
93
            'headline'  => $this->flag->user->name . ' dodał nowy raport',
94
            'subject'   => $this->flag->type->name,
95
            'url'       => $this->notificationUrl()
96
        ]);
97
    }
98
99
    public function toWebPush(): WebPushMessage
100
    {
101
        return (new WebPushMessage())
102
            ->title($this->flag->user->name . ' dodał nowy raport')
103
            ->icon('/apple-touch.png')
104
            ->body($this->flag->type->name)
105
            ->data(['url' => $this->notificationUrl()])
106
            ->options(['TTL' => 1000]);
107
    }
108
109
    protected function channels(User $user): array
110
    {
111
        return [DatabaseChannel::class, BroadcastChannel::class, WebPushChannel::class];
112
    }
113
}
114