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

AbstractNotification::toBroadcast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
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 $notifier;
23
24
    /**
25
     * @var Post
26
     */
27
    protected $post;
28
29
    /**
30
     * @param User|null $notifier
31
     * @param Post $post
32
     */
33
    public function __construct(?User $notifier, Post $post)
34
    {
35
        $this->notifier = $notifier;
36
        $this->post = $post;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    abstract protected function getMailSubject(): string;
43
44
    /**
45
     * @param User $user
46
     * @return array
47
     */
48
    public function via(User $user)
49
    {
50
        if (!$user->can('access', $this->post->forum)) {
51
            return [];
52
        }
53
54
        return parent::channels($user);
55
    }
56
57
    /**
58
     * @param User $user
59
     * @return array
60
     */
61
    public function toDatabase($user)
62
    {
63
        return [
64
            'object_id'     => $this->objectId(),
65
            'user_id'       => $user->id,
66
            '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...
67
            'subject'       => $this->post->topic->title,
68
            'excerpt'       => excerpt($this->post->html),
69
            'url'           => UrlBuilder::post($this->post),
70
            'id'            => $this->id,
71
            'content_type'  => Topic::class,
72
            'content_id'    => $this->post->topic_id
73
        ];
74
    }
75
76
    /**
77
     * Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu
78
     *
79
     * @return string
80
     */
81
    public function objectId()
82
    {
83
        return substr(md5(class_basename($this) . $this->post->id), 16);
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function sender()
90
    {
91
        return [
92
            'name' => $this->notifier->name,
93
            'user_id' => $this->notifier->id
94
        ];
95
    }
96
97
    /**
98
     * @return BroadcastMessage
99
     */
100
    public function toBroadcast()
101
    {
102
        return new BroadcastMessage([
103
            'headline'  => $this->getMailSubject(),
104
            'subject'   => excerpt($this->post->html),
105
            'url'       => $this->notificationUrl()
106
        ]);
107
    }
108
109
    public function toWebPush()
110
    {
111
        return (new WebPushMessage())
112
            ->title($this->getMailSubject())
113
            ->icon('/apple-touch.png')
114
            ->body(excerpt($this->post->html))
115
            ->data(['url' => $this->notificationUrl()])
116
            ->options(['TTL' => 1000]);
117
    }
118
}
119