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

ContentChangedNotification::notificationUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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;
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 ContentChangedNotification extends Notification implements ShouldBroadcast, ShouldQueue
17
{
18
    use Queueable;
19
20
    const ID = \Coyote\Notification::WIKI_SUBSCRIBER;
21
22
    /**
23
     * @var Wiki
24
     */
25
    private $wiki;
26
27
    /**
28
     * @var Wiki\Log
29
     */
30
    private $log;
31
32
    /**
33
     * ContentChangedNotification constructor.
34
     * @param Wiki $wiki
35
     */
36
    public function __construct(Wiki $wiki)
37
    {
38
        $this->wiki = $wiki;
39
        $this->log = $wiki->logs()->orderBy('id', 'DESC')->limit(1)->get()->first();
40
    }
41
42
    /**
43
     * Get the mail representation of the notification.
44
     *
45
     * @return \Illuminate\Notifications\Messages\MailMessage
46
     */
47
    public function toMail()
48
    {
49
        return (new MailMessage())
50
            ->subject($this->getMailSubject())
51
            ->line(sprintf('%s wprowadził zmiany na stronie %s, którą obserwujesz', $this->log->user->name, $this->wiki->title))
52
            ->action('Zobacz stronę', $this->notificationUrl());
53
    }
54
55
    /**
56
     * @param User $user
57
     * @return array
58
     */
59
    public function toDatabase($user)
60
    {
61
        return [
62
            'object_id'     => $this->objectId(),
63
            'user_id'       => $user->id,
64
            'type_id'       => static::ID,
65
            'subject'       => $this->wiki->title,
66
            'excerpt'       => excerpt($this->wiki->excerpt),
67
            'url'           => UrlBuilder::wiki($this->wiki),
68
            'id'            => $this->id
69
        ];
70
    }
71
72
    /**
73
     * @return BroadcastMessage
74
     */
75
    public function toBroadcast()
76
    {
77
        return new BroadcastMessage([
78
            'headline'  => $this->getMailSubject(),
79
            'subject'   => $this->wiki->title,
80
            'url'       => $this->notificationUrl()
81
        ]);
82
    }
83
84
    public function toWebPush(): WebPushMessage
85
    {
86
        return (new WebPushMessage())
87
            ->title($this->getMailSubject())
88
            ->icon(url('/apple-touch.png'))
89
            ->body($this->wiki->title)
90
            ->tag($this->notificationUrl())
91
            ->data(['url' => $this->notificationUrl()])
92
            ->options(['TTL' => 1000]);
93
    }
94
95
    /**
96
     * Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu
97
     *
98
     * @return string
99
     */
100
    public function objectId()
101
    {
102
        return substr(md5(class_basename($this) . $this->wiki->id), 16);
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function sender()
109
    {
110
        return [
111
            'user_id'       => $this->log->user_id,
112
            'name'          => $this->log->user->name
113
        ];
114
    }
115
116
    protected function notificationUrl(): string
117
    {
118
        return route('user.notifications.redirect', ['path' => urlencode(UrlBuilder::wiki($this->wiki))]);
119
    }
120
121
    protected function getMailSubject(): string
122
    {
123
        return sprintf('%s zmodyfikował stronę %s', $this->log->user->name, $this->wiki->title);
124
    }
125
}
126