Passed
Push — master ( 7598d6...0f7c6a )
by Adam
12:47
created

CommentedNotification::notificationUrl()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Notifications\Wiki;
4
5
use Coyote\Services\Notification\Notification;
6
use Coyote\Services\UrlBuilder;
7
use Coyote\User;
8
use Coyote\Wiki\Comment;
9
use Illuminate\Bus\Queueable;
10
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
11
use Illuminate\Contracts\Queue\ShouldQueue;
12
use Illuminate\Notifications\Messages\BroadcastMessage;
13
use Illuminate\Notifications\Messages\MailMessage;
14
use NotificationChannels\WebPush\WebPushMessage;
15
16
class CommentedNotification extends Notification implements ShouldQueue, ShouldBroadcast
17
{
18
    use Queueable;
19
20
    const ID = \Coyote\Notification::WIKI_COMMENT;
21
22
    /**
23
     * @var Comment
24
     */
25
    private $comment;
26
27
    /**
28
     * CommentedNotification constructor.
29
     * @param Comment $comment
30
     */
31
    public function __construct(Comment $comment)
32
    {
33
        $this->comment = $comment;
34
    }
35
36
    /**
37
     * Get the mail representation of the notification.
38
     *
39
     * @return \Illuminate\Notifications\Messages\MailMessage
40
     */
41
    public function toMail()
42
    {
43
        return (new MailMessage())
44
            ->subject($this->getMailSubject())
45
            ->line(sprintf('%s dodał komentarz do strony, którą obserwujesz', $this->comment->user->name))
46
            ->action('Zobacz komentarz', $this->notificationUrl());
47
    }
48
49
    /**
50
     * @param 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->comment->wiki->title,
60
            'excerpt'       => excerpt($this->comment->html),
61
            'url'           => UrlBuilder::wikiComment($this->comment->wiki, $this->comment->id),
62
            'id'            => $this->id
63
        ];
64
    }
65
66
    /**
67
     * @return BroadcastMessage
68
     */
69
    public function toBroadcast()
70
    {
71
        return new BroadcastMessage([
72
            'headline'  => $this->getMailSubject(),
73
            'subject'   => $this->comment->wiki->title,
74
            'url'       => $this->notificationUrl()
75
        ]);
76
    }
77
78
    public function toWebPush(): WebPushMessage
79
    {
80
        return (new WebPushMessage())
81
            ->title($this->getMailSubject())
82
            ->icon(url('/apple-touch.png'))
83
            ->body($this->comment->wiki->title)
84
            ->tag($this->notificationUrl())
85
            ->data(['url' => $this->notificationUrl()])
86
            ->options(['TTL' => 1000]);
87
    }
88
89
    /**
90
     * Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu
91
     *
92
     * @return string
93
     */
94
    public function objectId()
95
    {
96
        return substr(md5(class_basename($this) . $this->comment->wiki->id), 16);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function sender()
103
    {
104
        return [
105
            'user_id'       => $this->comment->user_id,
106
            'name'          => $this->comment->user->name
107
        ];
108
    }
109
110
    protected function getMailSubject(): string
111
    {
112
        return sprintf('%s dodał komentarz do strony %s', $this->comment->user->name, $this->comment->wiki->title);
113
    }
114
115
    protected function notificationUrl(): string
116
    {
117
        return route('user.notifications.redirect', ['path' => urlencode(UrlBuilder::wikiComment($this->comment->wiki, $this->comment->id))]);
118
    }
119
}
120