1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Intercom; |
4
|
|
|
|
5
|
|
|
class IntercomMessage |
6
|
|
|
{ |
7
|
|
|
public const TYPE_EMAIL = 'email'; |
8
|
|
|
|
9
|
|
|
public const TYPE_INAPP = 'inapp'; |
10
|
|
|
|
11
|
|
|
public const TEMPLATE_PLAIN = 'plain'; |
12
|
|
|
|
13
|
|
|
public const TEMPLATE_PERSONAL = 'personal'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $body |
17
|
|
|
* |
18
|
|
|
* @return IntercomMessage |
19
|
|
|
*/ |
20
|
7 |
|
public static function create(?string $body = null): self |
21
|
|
|
{ |
22
|
7 |
|
return new static($body); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
public $payload; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|null $body |
32
|
|
|
*/ |
33
|
23 |
|
public function __construct(?string $body = null) |
34
|
|
|
{ |
35
|
23 |
|
if (null !== $body) { |
36
|
8 |
|
$this->body($body); |
37
|
|
|
} |
38
|
|
|
|
39
|
23 |
|
$this->inapp(); |
40
|
23 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $body |
44
|
|
|
* |
45
|
|
|
* @return IntercomMessage |
46
|
|
|
*/ |
47
|
10 |
|
public function body(string $body): self |
48
|
|
|
{ |
49
|
10 |
|
$this->payload['body'] = $body; |
50
|
|
|
|
51
|
10 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return IntercomMessage |
56
|
|
|
*/ |
57
|
3 |
|
public function email(): self |
58
|
|
|
{ |
59
|
3 |
|
$this->payload['message_type'] = self::TYPE_EMAIL; |
60
|
|
|
|
61
|
3 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return IntercomMessage |
66
|
|
|
*/ |
67
|
23 |
|
public function inapp(): self |
68
|
|
|
{ |
69
|
23 |
|
$this->payload['message_type'] = self::TYPE_INAPP; |
70
|
|
|
|
71
|
23 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $value |
76
|
|
|
* |
77
|
|
|
* @return IntercomMessage |
78
|
|
|
*/ |
79
|
2 |
|
public function subject(string $value): self |
80
|
|
|
{ |
81
|
2 |
|
$this->payload['subject'] = $value; |
82
|
|
|
|
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return IntercomMessage |
88
|
|
|
*/ |
89
|
1 |
|
public function plain(): self |
90
|
|
|
{ |
91
|
1 |
|
$this->payload['template'] = self::TEMPLATE_PLAIN; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return IntercomMessage |
98
|
|
|
*/ |
99
|
2 |
|
public function personal(): self |
100
|
|
|
{ |
101
|
2 |
|
$this->payload['template'] = self::TEMPLATE_PERSONAL; |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $adminId |
108
|
|
|
* |
109
|
|
|
* @return IntercomMessage |
110
|
|
|
*/ |
111
|
8 |
|
public function from(string $adminId): self |
112
|
|
|
{ |
113
|
8 |
|
$this->payload['from'] = [ |
114
|
8 |
|
'type' => 'admin', |
115
|
8 |
|
'id' => $adminId, |
116
|
|
|
]; |
117
|
|
|
|
118
|
8 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $value |
123
|
|
|
* |
124
|
|
|
* @return IntercomMessage |
125
|
|
|
*/ |
126
|
2 |
|
public function to(array $value): self |
127
|
|
|
{ |
128
|
2 |
|
$this->payload['to'] = $value; |
129
|
|
|
|
130
|
2 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $id |
135
|
|
|
* |
136
|
|
|
* @return IntercomMessage |
137
|
|
|
*/ |
138
|
6 |
|
public function toUserId(string $id): self |
139
|
|
|
{ |
140
|
6 |
|
$this->payload['to'] = [ |
141
|
6 |
|
'type' => 'user', |
142
|
6 |
|
'id' => $id, |
143
|
|
|
]; |
144
|
|
|
|
145
|
6 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $email |
150
|
|
|
* |
151
|
|
|
* @return IntercomMessage |
152
|
|
|
*/ |
153
|
2 |
|
public function toUserEmail(string $email): self |
154
|
|
|
{ |
155
|
2 |
|
$this->payload['to'] = [ |
156
|
2 |
|
'type' => 'user', |
157
|
2 |
|
'email' => $email, |
158
|
|
|
]; |
159
|
|
|
|
160
|
2 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $id |
165
|
|
|
* |
166
|
|
|
* @return IntercomMessage |
167
|
|
|
*/ |
168
|
1 |
|
public function toContactId(string $id): self |
169
|
|
|
{ |
170
|
1 |
|
$this->payload['to'] = [ |
171
|
1 |
|
'type' => 'contact', |
172
|
1 |
|
'id' => $id, |
173
|
|
|
]; |
174
|
|
|
|
175
|
1 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
5 |
|
public function isValid(): bool |
182
|
|
|
{ |
183
|
|
|
return isset( |
184
|
5 |
|
$this->payload['body'], |
185
|
5 |
|
$this->payload['from'], |
186
|
5 |
|
$this->payload['to'] |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
6 |
|
public function toIsGiven(): bool |
194
|
|
|
{ |
195
|
6 |
|
return isset($this->payload['to']); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
4 |
|
public function toArray(): array |
202
|
|
|
{ |
203
|
4 |
|
return $this->payload; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|