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

AbstractNotification::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\Post;
4
5
use Coyote\Post;
6
use Coyote\Services\Notification\Notification;
7
use Coyote\Services\UrlBuilder;
8
use Coyote\Topic;
9
use Coyote\User;
10
use Illuminate\Bus\Queueable;
11
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
12
use Illuminate\Notifications\Messages\BroadcastMessage;
13
use NotificationChannels\WebPush\WebPushMessage;
14
15
abstract class AbstractNotification extends Notification implements ShouldBroadcastNow
16
{
17
    use Queueable;
18
19
    /**
20
     * @var User|null
21
     */
22
    protected ?User $notifier;
23
24
    /**
25
     * @var Post
26
     */
27
    protected Post $post;
28
29
    protected string $postUrl;
30
31
    /**
32
     * @param User|null $notifier
33
     * @param Post $post
34
     */
35
    public function __construct(?User $notifier, Post $post)
36
    {
37
        $this->notifier = $notifier;
38
        $this->post = $post;
39
        $this->postUrl = UrlBuilder::post($this->post);
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    abstract protected function getMailSubject(): string;
46
47
    /**
48
     * @param User $user
49
     * @return array
50
     */
51
    public function via(User $user)
52
    {
53
        if (!$user->can('access', $this->post->forum)) {
54
            return [];
55
        }
56
57
        return parent::channels($user);
58
    }
59
60
    /**
61
     * @param User $user
62
     * @return array
63
     */
64
    public function toDatabase($user)
65
    {
66
        return [
67
            'object_id'     => $this->objectId(),
68
            'user_id'       => $user->id,
69
            'type_id'       => static::ID,
0 ignored issues
show
Bug introduced by
The constant Coyote\Notifications\Post\AbstractNotification::ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
            'subject'       => $this->post->topic->title,
71
            'excerpt'       => excerpt($this->post->html),
72
            'url'           => $this->postUrl,
73
            'id'            => $this->id,
74
            'content_type'  => Topic::class,
75
            'content_id'    => $this->post->topic_id
76
        ];
77
    }
78
79
    /**
80
     * Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu
81
     *
82
     * @return string
83
     */
84
    public function objectId()
85
    {
86
        return substr(md5(class_basename($this) . $this->post->id), 16);
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function sender()
93
    {
94
        return [
95
            'name' => $this->notifier->name,
96
            'user_id' => $this->notifier->id
97
        ];
98
    }
99
100
    /**
101
     * @return BroadcastMessage
102
     */
103
    public function toBroadcast()
104
    {
105
        return new BroadcastMessage([
106
            'headline'  => $this->getMailSubject(),
107
            'subject'   => excerpt($this->post->html),
108
            'url'       => $this->redirectionUrl()
109
        ]);
110
    }
111
112
    public function toWebPush()
113
    {
114
        return (new WebPushMessage())
115
            ->title($this->getMailSubject())
116
            ->icon('/img/favicon.png')
117
            ->tag($this->redirectionUrl())
118
            ->body(excerpt($this->post->html))
119
            ->data(['url' => $this->redirectionUrl()])
120
            ->options(['TTL' => 1000]);
121
    }
122
123
    protected function redirectionUrl(): string
124
    {
125
        return route('user.notifications.redirect', ['path' => urlencode($this->postUrl)]);
126
    }
127
}
128