Completed
Push — master ( 453b10...bec1da )
by Tobias
04:06
created

MessageBuilder::parseAddress()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0261
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\Messages;
11
12
use Mailgun\Constants\Api;
13
use Mailgun\Constants\ExceptionMessages;
14
use Mailgun\Messages\Exceptions\InvalidParameter;
15
use Mailgun\Messages\Exceptions\TooManyParameters;
16
17
/**
18
 * This class is used for composing a properly formed
19
 * message object. Dealing with arrays can be cumbersome,
20
 * this class makes the process easier. See the official
21
 * documentation (link below) for usage instructions.
22
 *
23
 * @deprecated Will be removed in 3.0. Use Mailgun\Message\MessageBuilder
24
 * @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
25
 */
26
class MessageBuilder
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $message = [];
32
33
    /**
34
     * @var array
35
     */
36
    protected $variables = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $files = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $counters = [
47
        'recipients' => [
48
            'to' => 0,
49
            'cc' => 0,
50
            'bcc' => 0,
51
        ],
52
        'attributes' => [
53
            'attachment' => 0,
54
            'campaign_id' => 0,
55
            'custom_option' => 0,
56
            'tag' => 0,
57
        ],
58
    ];
59
60
    /**
61
     * @param array  $params
62
     * @param string $key
63
     * @param mixed  $default
64
     *
65
     * @return mixed
66
     */
67 20
    protected function safeGet($params, $key, $default)
68
    {
69 20
        if (array_key_exists($key, $params)) {
70 17
            return $params[$key];
71
        }
72
73 4
        return $default;
74
    }
75
76
    /**
77
     * @param array $params
78
     *
79
     * @return mixed|string
80
     */
81 20
    protected function getFullName($params)
82
    {
83 20
        if (array_key_exists('first', $params)) {
84 17
            $first = $this->safeGet($params, 'first', '');
85 17
            $last = $this->safeGet($params, 'last', '');
86
87 17
            return trim("$first $last");
88
        }
89
90 4
        return $this->safeGet($params, 'full_name', '');
91
    }
92
93
    /**
94
     * @param string $address
95
     * @param array  $variables
96
     *
97
     * @return string
98
     */
99 20
    protected function parseAddress($address, $variables)
100
    {
101 20
        if (!is_array($variables)) {
102
            return $address;
103
        }
104 20
        $fullName = $this->getFullName($variables);
105 20
        if (null != $fullName) {
106 17
            return sprintf('"%s" <%s>', $fullName, $address);
107
        }
108
109 4
        return $address;
110
    }
111
112
    /**
113
     * @param string $headerName
114
     * @param string $address
115
     * @param array  $variables
116
     */
117 10 View Code Duplication
    protected function addRecipient($headerName, $address, $variables)
0 ignored issues
show
Duplication introduced by
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...
118
    {
119 10
        $compiledAddress = $this->parseAddress($address, $variables);
120
121 10
        if ('h:reply-to' === $headerName) {
122 1
            $this->message[$headerName] = $compiledAddress;
123 10
        } elseif (isset($this->message[$headerName])) {
124
            array_push($this->message[$headerName], $compiledAddress);
125
        } else {
126 9
            $this->message[$headerName] = [$compiledAddress];
127
        }
128 10
        if (array_key_exists($headerName, $this->counters['recipients'])) {
129 8
            $this->counters['recipients'][$headerName] += 1;
130 8
        }
131 10
    }
132
133
    /**
134
     * @param string     $address
135
     * @param array|null $variables
136
     *
137
     * @throws TooManyParameters
138
     *
139
     * @return mixed
140
     */
141 12 View Code Duplication
    public function addToRecipient($address, $variables = null)
0 ignored issues
show
Duplication introduced by
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...
142
    {
143 12
        if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) {
144
            throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
145
        }
146
147 12
        $variables = is_array($variables) ? $variables : [];
148
149 12
        $this->addRecipient('to', $address, $variables);
150
151 12
        return end($this->message['to']);
152
    }
153
154
    /**
155
     * @param string     $address
156
     * @param array|null $variables
157
     *
158
     * @throws TooManyParameters
159
     *
160
     * @return mixed
161
     */
162 3 View Code Duplication
    public function addCcRecipient($address, $variables = null)
0 ignored issues
show
Duplication introduced by
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...
163
    {
164 3
        if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) {
165
            throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
166
        }
167
168 3
        $variables = is_array($variables) ? $variables : [];
169
170 3
        $this->addRecipient('cc', $address, $variables);
171
172 3
        return end($this->message['cc']);
173
    }
174
175
    /**
176
     * @param string     $address
177
     * @param array|null $variables
178
     *
179
     * @throws TooManyParameters
180
     *
181
     * @return mixed
182
     */
183 3 View Code Duplication
    public function addBccRecipient($address, $variables = null)
0 ignored issues
show
Duplication introduced by
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...
184
    {
185 3
        if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) {
186
            throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
187
        }
188
189 3
        $variables = is_array($variables) ? $variables : [];
190
191 3
        $this->addRecipient('bcc', $address, $variables);
192
193 3
        return end($this->message['bcc']);
194
    }
195
196
    /**
197
     * @param string     $address
198
     * @param array|null $variables
199
     *
200
     * @return mixed
201
     */
202 6 View Code Duplication
    public function setFromAddress($address, $variables = null)
0 ignored issues
show
Duplication introduced by
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...
203
    {
204 6
        $variables = is_array($variables) ? $variables : [];
205
206 6
        $this->addRecipient('from', $address, $variables);
207
208 6
        return $this->message['from'];
209
    }
210
211
    /**
212
     * @param string     $address
213
     * @param array|null $variables
214
     *
215
     * @return mixed
216
     */
217 1 View Code Duplication
    public function setReplyToAddress($address, $variables = null)
0 ignored issues
show
Duplication introduced by
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...
218
    {
219 1
        $variables = is_array($variables) ? $variables : [];
220
221 1
        $this->addRecipient('h:reply-to', $address, $variables);
222
223 1
        return $this->message['h:reply-to'];
224
    }
225
226
    /**
227
     * @param string $subject
228
     *
229
     * @return mixed
230
     */
231 6
    public function setSubject($subject = '')
232
    {
233 6
        if (null == $subject || '' == $subject) {
234
            $subject = ' ';
235
        }
236 6
        $this->message['subject'] = $subject;
237
238 6
        return $this->message['subject'];
239
    }
240
241
    /**
242
     * @param string $headerName
243
     * @param mixed  $headerData
244
     *
245
     * @return mixed
246
     */
247 3 View Code Duplication
    public function addCustomHeader($headerName, $headerData)
0 ignored issues
show
Duplication introduced by
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...
248
    {
249 3
        if (!preg_match('/^h:/i', $headerName)) {
250 3
            $headerName = 'h:'.$headerName;
251 3
        }
252
253 3
        if (array_key_exists($headerName, $this->message)) {
254 2
            if (is_array($this->message[$headerName])) {
255
                $this->message[$headerName][] = $headerData;
256
            } else {
257 2
                $this->message[$headerName] = [$this->message[$headerName], $headerData];
258
            }
259 2
        } else {
260 3
            $this->message[$headerName] = $headerData;
261
        }
262
263 3
        return $this->message[$headerName];
264
    }
265
266
    /**
267
     * @param string $textBody
268
     *
269
     * @return string
270
     */
271 6
    public function setTextBody($textBody)
272
    {
273 6
        if (null == $textBody || '' == $textBody) {
274
            $textBody = ' ';
275
        }
276 6
        $this->message['text'] = $textBody;
277
278 6
        return $this->message['text'];
279
    }
280
281
    /**
282
     * @param string $htmlBody
283
     *
284
     * @return string
285
     */
286 1
    public function setHtmlBody($htmlBody)
287
    {
288 1
        if (null == $htmlBody || '' == $htmlBody) {
289
            $htmlBody = ' ';
290
        }
291 1
        $this->message['html'] = $htmlBody;
292
293 1
        return $this->message['html'];
294
    }
295
296
    /**
297
     * @param string      $attachmentPath
298
     * @param string|null $attachmentName
299
     *
300
     * @return bool
301
     */
302 2
    public function addAttachment($attachmentPath, $attachmentName = null)
303
    {
304 2
        if (isset($this->files['attachment'])) {
305
            $attachment = [
306 2
                'filePath' => $attachmentPath,
307 2
                'remoteName' => $attachmentName,
308 2
            ];
309 2
            array_push($this->files['attachment'], $attachment);
310 2
        } else {
311 2
            $this->files['attachment'] = [
312
                [
313 2
                    'filePath' => $attachmentPath,
314 2
                    'remoteName' => $attachmentName,
315 2
                ],
316
            ];
317
        }
318
319 2
        return true;
320
    }
321
322
    /**
323
     * @param string      $inlineImagePath
324
     * @param string|null $inlineImageName
325
     *
326
     * @throws InvalidParameter
327
     *
328
     * @return bool
329
     */
330 3
    public function addInlineImage($inlineImagePath, $inlineImageName = null)
331
    {
332 3
        if (0 !== strpos($inlineImagePath, '@')) {
333
            throw new InvalidParameter(ExceptionMessages::INVALID_PARAMETER_INLINE);
334
        }
335
336 3
        $this->files['inline'][] = [
337 3
            'filePath' => $inlineImagePath,
338 3
            'remoteName' => $inlineImageName,
339
        ];
340
341 3
        return true;
342
    }
343
344
    /**
345
     * @param bool $testMode
346
     *
347
     * @return string
348
     */
349 1 View Code Duplication
    public function setTestMode($testMode)
0 ignored issues
show
Duplication introduced by
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...
350
    {
351 1
        if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
352 1
            $testMode = 'yes';
353 1
        } else {
354 1
            $testMode = 'no';
355
        }
356 1
        $this->message['o:testmode'] = $testMode;
357
358 1
        return $this->message['o:testmode'];
359
    }
360
361
    /**
362
     * @param string|int $campaignId
363
     *
364
     * @throws TooManyParameters
365
     *
366
     * @return string|int
367
     */
368 2 View Code Duplication
    public function addCampaignId($campaignId)
0 ignored issues
show
Duplication introduced by
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...
369
    {
370 2
        if ($this->counters['attributes']['campaign_id'] < Api::CAMPAIGN_ID_LIMIT) {
371 2
            if (isset($this->message['o:campaign'])) {
372 2
                array_push($this->message['o:campaign'], (string) $campaignId);
373 2
            } else {
374 2
                $this->message['o:campaign'] = [(string) $campaignId];
375
            }
376 2
            $this->counters['attributes']['campaign_id'] += 1;
377
378 2
            return $this->message['o:campaign'];
379
        } else {
380
            throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_CAMPAIGNS);
381
        }
382
    }
383
384
    /**
385
     * @param string $tag
386
     *
387
     * @throws TooManyParameters
388
     */
389 View Code Duplication
    public function addTag($tag)
0 ignored issues
show
Duplication introduced by
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...
390
    {
391
        if ($this->counters['attributes']['tag'] < Api::TAG_LIMIT) {
392
            if (isset($this->message['o:tag'])) {
393
                array_push($this->message['o:tag'], $tag);
394
            } else {
395
                $this->message['o:tag'] = [$tag];
396
            }
397
            $this->counters['attributes']['tag'] += 1;
398
399
            return $this->message['o:tag'];
400
        } else {
401
            throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_TAGS);
402
        }
403
    }
404
405
    /**
406
     * @param bool $enabled
407
     *
408
     * @return mixed
409
     */
410 1 View Code Duplication
    public function setDkim($enabled)
0 ignored issues
show
Duplication introduced by
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...
411
    {
412 1
        if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
413 1
            $enabled = 'yes';
414 1
        } else {
415 1
            $enabled = 'no';
416
        }
417 1
        $this->message['o:dkim'] = $enabled;
418
419 1
        return $this->message['o:dkim'];
420
    }
421
422
    /**
423
     * @param bool $enabled
424
     *
425
     * @return string
426
     */
427 1 View Code Duplication
    public function setOpenTracking($enabled)
0 ignored issues
show
Duplication introduced by
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...
428
    {
429 1
        if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
430 1
            $enabled = 'yes';
431 1
        } else {
432 1
            $enabled = 'no';
433
        }
434 1
        $this->message['o:tracking-opens'] = $enabled;
435
436 1
        return $this->message['o:tracking-opens'];
437
    }
438
439
    /**
440
     * @param bool $enabled
441
     *
442
     * @return string
443
     */
444 1
    public function setClickTracking($enabled)
445
    {
446 1 View Code Duplication
        if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
0 ignored issues
show
Duplication introduced by
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...
447 1
            $enabled = 'yes';
448 1
        } elseif ('html' == $enabled) {
449
            $enabled = 'html';
450
        } else {
451 1
            $enabled = 'no';
452
        }
453 1
        $this->message['o:tracking-clicks'] = $enabled;
454
455 1
        return $this->message['o:tracking-clicks'];
456
    }
457
458
    /**
459
     * @param string      $timeDate
460
     * @param string|null $timeZone
461
     *
462
     * @return string
463
     */
464 1 View Code Duplication
    public function setDeliveryTime($timeDate, $timeZone = null)
0 ignored issues
show
Duplication introduced by
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...
465
    {
466 1
        if (isset($timeZone)) {
467 1
            $timeZoneObj = new \DateTimeZone("$timeZone");
468 1
        } else {
469 1
            $timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE);
470
        }
471
472 1
        $dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
473 1
        $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822);
474 1
        $this->message['o:deliverytime'] = $formattedTimeDate;
475
476 1
        return $this->message['o:deliverytime'];
477
    }
478
479
    /**
480
     * @param string $customName
481
     * @param mixed  $data
482
     */
483 1
    public function addCustomData($customName, $data)
484
    {
485 1
        $this->message['v:'.$customName] = json_encode($data);
486 1
    }
487
488
    /**
489
     * @param string $parameterName
490
     * @param mixed  $data
491
     *
492
     * @return mixed
493
     */
494 1 View Code Duplication
    public function addCustomParameter($parameterName, $data)
0 ignored issues
show
Duplication introduced by
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...
495
    {
496 1
        if (isset($this->message[$parameterName])) {
497
            array_push($this->message[$parameterName], $data);
498
499
            return $this->message[$parameterName];
500
        } else {
501 1
            $this->message[$parameterName] = [$data];
502
503 1
            return $this->message[$parameterName];
504
        }
505
    }
506
507
    /**
508
     * @param array $message
509
     */
510 1
    public function setMessage($message)
511
    {
512 1
        $this->message = $message;
513 1
    }
514
515
    /**
516
     * @return array
517
     */
518 32
    public function getMessage()
519
    {
520 32
        return $this->message;
521
    }
522
523
    /**
524
     * @return array
525
     */
526 5
    public function getFiles()
527
    {
528 5
        return $this->files;
529
    }
530
}
531