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

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