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