Passed
Push — master ( f2dd02...330c2d )
by Adam
12:12
created

CommentedNotification::redirectionUrl()   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\Job;
4
5
use Coyote\Job\Comment;
6
use Coyote\Services\Notification\Notification;
7
use Coyote\Services\UrlBuilder;
8
use Coyote\User;
9
use Illuminate\Bus\Queueable;
10
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
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, ShouldBroadcastNow
17
{
18
    use Queueable;
19
20
    const ID = \Coyote\Notification::JOB_COMMENT;
21
22
    /**
23
     * @var Comment
24
     */
25
    private $comment;
26
27
    private string $commentUrl;
28
29
    /**
30
     * @param Comment $comment
31
     */
32
    public function __construct(Comment $comment)
33
    {
34
        $this->comment = $comment;
35
        $this->commentUrl = UrlBuilder::jobComment($this->comment->job, $this->comment->id);
36
    }
37
38
    /**
39
     * @param User $user
40
     * @return array
41
     */
42
    public function toDatabase($user)
43
    {
44
        return [
45
            'object_id'     => $this->objectId(),
46
            'user_id'       => $user->id,
47
            'type_id'       => static::ID,
48
            'subject'       => $this->comment->job->title,
49
            'excerpt'       => excerpt($this->comment->html),
50
            'url'           => $this->commentUrl,
51
            'id'            => $this->id
52
        ];
53
    }
54
55
    /**
56
     * Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu
57
     *
58
     * @return string
59
     */
60
    public function objectId()
61
    {
62
        return substr(md5(class_basename($this) . $this->comment->job->id), 16);
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    public function sender()
69
    {
70
        return [
71
            'name' => $this->comment->user_id ? $this->comment->user->name : $this->comment->email,
72
            'user_id' => $this->comment->user_id
73
        ];
74
    }
75
76
    /**
77
     * @return MailMessage
78
     */
79
    public function toMail()
80
    {
81
        return (new MailMessage())
82
            ->subject($this->getMailSubject())
83
            ->line(
84
                sprintf(
85
                    'Do ogłoszenia <b>%s</b> dodany został nowy komentarz.',
86
                    link_to(UrlBuilder::job($this->comment->job), $this->comment->job->title)
87
                )
88
            )
89
            ->action(
90
                'Kliknij, aby go zobaczyć i odpowiedzieć',
91
                $this->redirectionUrl()
92
            )
93
            ->line('Otrzymujesz to powiadomienie ponieważ dodałeś to ogłoszenie do ulubionych lub jesteś jego autorem.');
94
    }
95
96
    /**
97
     * @return BroadcastMessage
98
     */
99
    public function toBroadcast()
100
    {
101
        return new BroadcastMessage([
102
            'headline'  => $this->getMailSubject(),
103
            'subject'   => $this->comment->job->title,
104
            'url'       => $this->redirectionUrl()
105
        ]);
106
    }
107
108
    public function toWebPush(): WebPushMessage
109
    {
110
        return (new WebPushMessage())
111
            ->title($this->getMailSubject())
112
            ->icon('/img/favicon.png')
113
            ->tag($this->redirectionUrl())
114
            ->body($this->comment->job->title)
115
            ->data(['url' => $this->redirectionUrl()])
116
            ->options(['TTL' => 1000]);
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    private function getMailSubject(): string
123
    {
124
        return sprintf('Nowy komentarz do ogłoszenia %s.', $this->comment->job->title);
125
    }
126
127
    protected function redirectionUrl(): string
128
    {
129
        return route('user.notifications.redirect', ['path' => urlencode($this->commentUrl)]);
130
    }
131
}
132