1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace NotificationChannels\Pushbullet; |
6
|
|
|
|
7
|
|
|
use NotificationChannels\Pushbullet\Targets\Targetable; |
8
|
|
|
|
9
|
|
|
class PushbulletMessage |
10
|
|
|
{ |
11
|
|
|
private const TYPE_NOTE = 'note'; |
12
|
|
|
private const TYPE_LINK = 'link'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Type of message (currently: note or link). |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $type = 'note'; |
20
|
|
|
|
21
|
|
|
/** @var \NotificationChannels\Pushbullet\Targets\Targetable */ |
22
|
|
|
protected $target; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Notification title. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $title; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Notification message. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $message; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Url if notification is of link type. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $url; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $message |
47
|
|
|
* |
48
|
|
|
* @return static |
49
|
|
|
*/ |
50
|
1 |
|
public static function create($message): self |
51
|
|
|
{ |
52
|
1 |
|
return new static($message); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $message |
57
|
|
|
*/ |
58
|
9 |
|
public function __construct($message) |
59
|
|
|
{ |
60
|
9 |
|
$this->message = $message; |
61
|
9 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \NotificationChannels\Pushbullet\Targets\Targetable $targetable |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
1 |
|
public function target(Targetable $targetable): self |
69
|
|
|
{ |
70
|
1 |
|
$this->target = $targetable; |
71
|
|
|
|
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Specify that notification is of `note` type. |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
1 |
|
public function note(): self |
81
|
|
|
{ |
82
|
1 |
|
$this->type = self::TYPE_NOTE; |
83
|
|
|
|
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Specify that notification is of `link` type. |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
2 |
|
public function link(): self |
93
|
|
|
{ |
94
|
2 |
|
$this->type = self::TYPE_LINK; |
95
|
|
|
|
96
|
2 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set notification title. |
101
|
|
|
* |
102
|
|
|
* @param string $title |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
2 |
|
public function title($title): self |
107
|
|
|
{ |
108
|
2 |
|
$this->title = $title; |
109
|
|
|
|
110
|
2 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set notification message. |
115
|
|
|
* |
116
|
|
|
* @param string $message |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
1 |
|
public function message($message): self |
121
|
|
|
{ |
122
|
1 |
|
$this->message = $message; |
123
|
|
|
|
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set notification url (if notification is of `link` type). |
129
|
|
|
* |
130
|
|
|
* @param string $url |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
1 |
|
public function url($url): self |
135
|
|
|
{ |
136
|
1 |
|
$this->url = $url; |
137
|
|
|
|
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get array representation of message for Pushbullet client. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
1 |
|
public function toArray(): array |
147
|
|
|
{ |
148
|
|
|
$payload = [ |
149
|
1 |
|
'type' => $this->type, |
150
|
1 |
|
'title' => $this->title, |
151
|
1 |
|
'body' => $this->message, |
152
|
|
|
]; |
153
|
|
|
|
154
|
1 |
|
return array_merge( |
155
|
1 |
|
$payload, |
156
|
1 |
|
$this->target->getTarget(), |
157
|
1 |
|
$this->getUrlParameter() |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
1 |
|
private function isLink(): bool |
165
|
|
|
{ |
166
|
1 |
|
return $this->type === self::TYPE_LINK; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
1 |
|
private function getUrlParameter(): array |
173
|
|
|
{ |
174
|
1 |
|
return $this->isLink() ? ['url' => $this->url] : []; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|