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

AbstractNotification::toWebPush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Notifications\Microblog;
4
5
use Coyote\Microblog;
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\Notifications\Messages\BroadcastMessage;
12
use NotificationChannels\WebPush\WebPushMessage;
13
14
abstract class AbstractNotification extends Notification implements ShouldBroadcastNow
15
{
16
    use Queueable;
17
18
    /**
19
     * Indicate if the job should be deleted when models are missing.
20
     *
21
     * @var bool
22
     */
23
    public $deleteWhenMissingModels = true;
24
25
    /**
26
     * @var Microblog
27
     */
28
    protected $microblog;
29
30
    /**
31
     * @var User
32
     */
33
    public $notifier;
34
35
    /**
36
     * @var string
37
     */
38
    protected $url;
39
40
    /**
41
     * @param Microblog $microblog
42
     */
43
    public function __construct(Microblog $microblog)
44
    {
45
        $this->microblog = $microblog;
46
        $this->notifier = $this->microblog->user;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function sender()
53
    {
54
        return [
55
            'user_id'       => $this->notifier->id,
56
            'name'          => $this->notifier->name
57
        ];
58
    }
59
60
    /**
61
     * Generowanie unikalnego ciagu znakow dla wpisu na mikro
62
     *
63
     * @return string
64
     */
65
    public function objectId()
66
    {
67
        return substr(md5(class_basename($this) . ($this->microblog->parent_id ?: $this->microblog->id)), 16);
68
    }
69
70
    /**
71
     * @return BroadcastMessage
72
     */
73
    public function toBroadcast(): BroadcastMessage
74
    {
75
        return new BroadcastMessage([
76
            'headline'  => $this->getMailSubject(),
77
            'subject'   => excerpt($this->microblog->html),
78
            'url'       => $this->notificationUrl(),
79
        ]);
80
    }
81
82
    public function toWebPush(): WebPushMessage
83
    {
84
        return (new WebPushMessage())
85
            ->title($this->getMailSubject())
86
            ->icon('/apple-touch.png')
87
            ->body(excerpt($this->microblog->html))
88
            ->data(['url' => $this->notificationUrl()])
89
            ->options(['TTL' => 1000]);
90
    }
91
92
    protected function microblogUrl(): string
93
    {
94
        return $this->microblog->parent_id ? UrlBuilder::microblogComment($this->microblog) : UrlBuilder::microblog($this->microblog);
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    abstract protected function getMailSubject(): string;
101
}
102