These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * Copyright (C) 2013 Mailgun |
||
5 | * |
||
6 | * This software may be modified and distributed under the terms |
||
7 | * of the MIT license. See the LICENSE file for details. |
||
8 | */ |
||
9 | |||
10 | namespace Mailgun\Message; |
||
11 | |||
12 | use Mailgun\Message\Exceptions\LimitExceeded; |
||
13 | use Mailgun\Message\Exceptions\TooManyRecipients; |
||
14 | |||
15 | /** |
||
16 | * This class is used for composing a properly formed |
||
17 | * message object. Dealing with arrays can be cumbersome, |
||
18 | * this class makes the process easier. See the official |
||
19 | * documentation (link below) for usage instructions. |
||
20 | * |
||
21 | * @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Message/README.md |
||
22 | */ |
||
23 | class MessageBuilder |
||
24 | { |
||
25 | const RECIPIENT_COUNT_LIMIT = 1000; |
||
26 | |||
27 | const CAMPAIGN_ID_LIMIT = 3; |
||
28 | |||
29 | const TAG_LIMIT = 3; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $message = []; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $variables = []; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $counters = [ |
||
45 | 'recipients' => [ |
||
46 | 'to' => 0, |
||
47 | 'cc' => 0, |
||
48 | 'bcc' => 0, |
||
49 | ], |
||
50 | 'attributes' => [ |
||
51 | 'attachment' => 0, |
||
52 | 'campaign_id' => 0, |
||
53 | 'custom_option' => 0, |
||
54 | 'tag' => 0, |
||
55 | ], |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * @param array $params |
||
60 | * @param string $key |
||
61 | * @param mixed $default |
||
62 | * |
||
63 | * @return mixed |
||
64 | */ |
||
65 | private function get($params, $key, $default) |
||
66 | { |
||
67 | if (array_key_exists($key, $params)) { |
||
68 | return $params[$key]; |
||
69 | } |
||
70 | |||
71 | return $default; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param array $params { |
||
76 | * |
||
77 | * @var string $full_name |
||
78 | * @var string $first |
||
79 | * @var string $last |
||
80 | * } |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | private function getFullName(array $params) |
||
85 | { |
||
86 | if (isset($params['full_name'])) { |
||
87 | return $this->get($params, 'full_name', ''); |
||
88 | } |
||
89 | |||
90 | return trim(sprintf('%s %s', $this->get($params, 'first', ''), $this->get($params, 'last', ''))); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $address |
||
95 | * @param array $params { |
||
0 ignored issues
–
show
|
|||
96 | * |
||
97 | * @var string $full_name |
||
98 | * @var string $first |
||
99 | * @var string $last |
||
100 | * } |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | protected function parseAddress($address, array $variables) |
||
105 | { |
||
106 | $fullName = $this->getFullName($variables); |
||
107 | if (!empty($fullName)) { |
||
108 | return sprintf('"%s" <%s>', $fullName, $address); |
||
109 | } |
||
110 | |||
111 | return $address; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param string $headerName |
||
116 | * @param string $address |
||
117 | * @param array $variables { |
||
118 | * |
||
119 | * @var string $full_name |
||
120 | * @var string $first |
||
121 | * @var string $last |
||
122 | * } |
||
123 | */ |
||
124 | View Code Duplication | protected function addRecipient($headerName, $address, array $variables) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
125 | { |
||
126 | $compiledAddress = $this->parseAddress($address, $variables); |
||
127 | |||
128 | if ('h:reply-to' === $headerName) { |
||
129 | $this->message[$headerName] = $compiledAddress; |
||
130 | } elseif (isset($this->message[$headerName])) { |
||
131 | $this->message[$headerName][] = $compiledAddress; |
||
132 | } else { |
||
133 | $this->message[$headerName] = [$compiledAddress]; |
||
134 | } |
||
135 | if (array_key_exists($headerName, $this->counters['recipients'])) { |
||
136 | $this->counters['recipients'][$headerName] += 1; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $address |
||
142 | * @param array $variables { |
||
143 | * |
||
144 | * @var string $id If used with BatchMessage |
||
145 | * @var string $full_name |
||
146 | * @var string $first |
||
147 | * @var string $last |
||
148 | * } |
||
149 | * |
||
150 | * @throws TooManyRecipients |
||
151 | */ |
||
152 | View Code Duplication | public function addToRecipient($address, array $variables = []) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
153 | { |
||
154 | if ($this->counters['recipients']['to'] > self::RECIPIENT_COUNT_LIMIT) { |
||
155 | throw TooManyRecipients::create('to'); |
||
156 | } |
||
157 | $this->addRecipient('to', $address, $variables); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param string $address |
||
162 | * @param array $variables { |
||
163 | * |
||
164 | * @var string $id If used with BatchMessage |
||
165 | * @var string $full_name |
||
166 | * @var string $first |
||
167 | * @var string $last |
||
168 | * } |
||
169 | * |
||
170 | * @throws TooManyRecipients |
||
171 | */ |
||
172 | View Code Duplication | public function addCcRecipient($address, array $variables = []) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
173 | { |
||
174 | if ($this->counters['recipients']['cc'] > self::RECIPIENT_COUNT_LIMIT) { |
||
175 | throw TooManyRecipients::create('cc'); |
||
176 | } |
||
177 | |||
178 | $this->addRecipient('cc', $address, $variables); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param string $address |
||
183 | * @param array $variables { |
||
184 | * |
||
185 | * @var string $id If used with BatchMessage |
||
186 | * @var string $full_name |
||
187 | * @var string $first |
||
188 | * @var string $last |
||
189 | * } |
||
190 | * |
||
191 | * @throws TooManyRecipients |
||
192 | */ |
||
193 | public function addBccRecipient($address, array $variables) |
||
194 | { |
||
195 | if ($this->counters['recipients']['bcc'] > self::RECIPIENT_COUNT_LIMIT) { |
||
196 | throw TooManyRecipients::create('bcc'); |
||
197 | } |
||
198 | |||
199 | $this->addRecipient('bcc', $address, $variables); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $address |
||
204 | * @param array $variables { |
||
205 | * |
||
206 | * @var string $id If used with BatchMessage |
||
207 | * @var string $full_name |
||
208 | * @var string $first |
||
209 | * @var string $last |
||
210 | * } |
||
211 | */ |
||
212 | public function setFromAddress($address, array $variables = []) |
||
213 | { |
||
214 | $this->addRecipient('from', $address, $variables); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param string $address |
||
219 | * @param array $variables { |
||
220 | * |
||
221 | * @var string $id If used with BatchMessage |
||
222 | * @var string $full_name |
||
223 | * @var string $first |
||
224 | * @var string $last |
||
225 | * } |
||
226 | */ |
||
227 | public function setReplyToAddress($address, array $variables = []) |
||
228 | { |
||
229 | $this->addRecipient('h:reply-to', $address, $variables); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $subject |
||
234 | */ |
||
235 | public function setSubject($subject) |
||
236 | { |
||
237 | $this->message['subject'] = $subject; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $headerName |
||
242 | * @param mixed $headerData |
||
243 | */ |
||
244 | View Code Duplication | public function addCustomHeader($headerName, $headerData) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
245 | { |
||
246 | if (!preg_match('/^h:/i', $headerName)) { |
||
247 | $headerName = 'h:'.$headerName; |
||
248 | } |
||
249 | |||
250 | if (!array_key_exists($headerName, $this->message)) { |
||
251 | $this->message[$headerName] = $headerData; |
||
252 | } else { |
||
253 | if (is_array($this->message[$headerName])) { |
||
254 | $this->message[$headerName][] = $headerData; |
||
255 | } else { |
||
256 | $this->message[$headerName] = [$this->message[$headerName], $headerData]; |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $textBody |
||
263 | */ |
||
264 | public function setTextBody($textBody) |
||
265 | { |
||
266 | $this->message['text'] = $textBody; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $htmlBody |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | public function setHtmlBody($htmlBody) |
||
275 | { |
||
276 | $this->message['html'] = $htmlBody; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param string $attachmentPath |
||
281 | * @param string|null $attachmentName |
||
282 | */ |
||
283 | View Code Duplication | public function addAttachment($attachmentPath, $attachmentName = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
284 | { |
||
285 | if (!isset($this->message['attachment'])) { |
||
286 | $this->message['attachment'] = []; |
||
287 | } |
||
288 | |||
289 | $this->message['attachment'][] = [ |
||
290 | 'filePath' => $attachmentPath, |
||
291 | 'remoteName' => $attachmentName, |
||
292 | ]; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param string $inlineImagePath |
||
297 | * @param string|null $inlineImageName |
||
298 | */ |
||
299 | View Code Duplication | public function addInlineImage($inlineImagePath, $inlineImageName = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
300 | { |
||
301 | if (!isset($this->message['inline'])) { |
||
302 | $this->message['inline'] = []; |
||
303 | } |
||
304 | |||
305 | $this->message['inline'][] = [ |
||
306 | 'filePath' => $inlineImagePath, |
||
307 | 'remoteName' => $inlineImageName, |
||
308 | ]; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param bool $enabled |
||
313 | */ |
||
314 | public function setTestMode($enabled) |
||
315 | { |
||
316 | $this->message['o:testmode'] = $this->boolToString($enabled); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param string $campaignId |
||
321 | * |
||
322 | * @throws LimitExceeded |
||
323 | */ |
||
324 | View Code Duplication | public function addCampaignId($campaignId) |
|
325 | { |
||
326 | if ($this->counters['attributes']['campaign_id'] >= self::CAMPAIGN_ID_LIMIT) { |
||
327 | throw LimitExceeded::create('campaigns', self::CAMPAIGN_ID_LIMIT); |
||
328 | } |
||
329 | if (isset($this->message['o:campaign'])) { |
||
330 | array_push($this->message['o:campaign'], (string) $campaignId); |
||
331 | } else { |
||
332 | $this->message['o:campaign'] = [(string) $campaignId]; |
||
333 | } |
||
334 | $this->counters['attributes']['campaign_id'] += 1; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param string $tag |
||
339 | * |
||
340 | * @throws LimitExceeded |
||
341 | */ |
||
342 | View Code Duplication | public function addTag($tag) |
|
343 | { |
||
344 | if ($this->counters['attributes']['tag'] >= self::TAG_LIMIT) { |
||
345 | throw LimitExceeded::create('tags', self::TAG_LIMIT); |
||
346 | } |
||
347 | |||
348 | if (isset($this->message['o:tag'])) { |
||
349 | array_push($this->message['o:tag'], $tag); |
||
350 | } else { |
||
351 | $this->message['o:tag'] = [$tag]; |
||
352 | } |
||
353 | $this->counters['attributes']['tag'] += 1; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param bool $enabled |
||
358 | */ |
||
359 | public function setDkim($enabled) |
||
360 | { |
||
361 | $this->message['o:dkim'] = $this->boolToString($enabled); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @param bool $enabled |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | public function setOpenTracking($enabled) |
||
370 | { |
||
371 | $this->message['o:tracking-opens'] = $this->boolToString($enabled); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @param bool $enabled |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | public function setClickTracking($enabled) |
||
380 | { |
||
381 | $this->message['o:tracking-clicks'] = $this->boolToString($enabled); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param string $timeDate |
||
386 | * @param string|null $timeZone |
||
387 | * |
||
388 | * @return string |
||
389 | */ |
||
390 | View Code Duplication | public function setDeliveryTime($timeDate, $timeZone = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
391 | { |
||
392 | if (null !== $timeZone) { |
||
393 | $timeZoneObj = new \DateTimeZone($timeZone); |
||
394 | } else { |
||
395 | $timeZoneObj = new \DateTimeZone('UTC'); |
||
396 | } |
||
397 | |||
398 | $dateTimeObj = new \DateTime($timeDate, $timeZoneObj); |
||
399 | $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); |
||
400 | $this->message['o:deliverytime'] = $formattedTimeDate; |
||
401 | |||
402 | return $this->message['o:deliverytime']; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param string $customName |
||
407 | * @param mixed $data |
||
408 | */ |
||
409 | public function addCustomData($customName, $data) |
||
410 | { |
||
411 | $this->message['v:'.$customName] = json_encode($data); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param string $parameterName |
||
416 | * @param mixed $data |
||
417 | * |
||
418 | * @return mixed |
||
419 | */ |
||
420 | View Code Duplication | public function addCustomParameter($parameterName, $data) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
421 | { |
||
422 | if (isset($this->message[$parameterName])) { |
||
423 | $this->message[$parameterName][] = $data; |
||
424 | } else { |
||
425 | $this->message[$parameterName] = [$data]; |
||
426 | } |
||
427 | |||
428 | return $this->message[$parameterName]; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @param array $message |
||
433 | */ |
||
434 | public function setMessage($message) |
||
435 | { |
||
436 | $this->message = $message; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @return array |
||
441 | */ |
||
442 | public function getMessage() |
||
443 | { |
||
444 | return $this->message; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @param $enabled |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | private function boolToString($enabled) |
||
453 | { |
||
454 | View Code Duplication | if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
455 | $enabled = 'yes'; |
||
456 | } elseif ('html' === $enabled) { |
||
457 | $enabled = 'html'; |
||
458 | } else { |
||
459 | $enabled = 'no'; |
||
460 | } |
||
461 | |||
462 | return $enabled; |
||
463 | } |
||
464 | } |
||
465 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.