Message   F
last analyzed

Complexity

Total Complexity 74

Size/Duplication

Total Lines 658
Duplicated Lines 0 %

Coupling/Cohesion

Components 8
Dependencies 3

Importance

Changes 0
Metric Value
wmc 74
lcom 8
cbo 3
dl 0
loc 658
rs 2.422
c 0
b 0
f 0

45 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharset() 0 4 1
A setCharset() 0 4 1
A getFrom() 0 4 1
A setFrom() 0 5 1
A getSender() 0 4 1
A setSender() 0 5 1
A getTo() 0 4 1
A setTo() 0 8 2
A getReplyTo() 0 4 1
A setReplyTo() 0 5 1
A getCc() 0 4 1
A setCc() 0 8 2
A getBcc() 0 4 1
A setBcc() 0 8 2
A getSubject() 0 4 1
A setSubject() 0 5 1
A getTextBody() 0 4 1
A setTextBody() 0 5 1
A getHtmlBody() 0 4 1
A setHtmlBody() 0 5 1
A getTag() 0 4 1
A setTag() 0 5 1
A setTrackOpens() 0 5 1
A getTrackOpens() 0 4 1
A setTrackClicks() 0 5 1
A getTrackClicks() 0 4 1
A setTemplateId() 0 5 1
A getTemplateId() 0 4 1
A setTemplateLanguage() 0 5 1
A getTemplateLanguage() 0 4 1
A setTemplateModel() 0 8 2
A getTemplateModel() 0 4 1
A setInlineCss() 0 5 1
A getInlineCss() 0 4 1
A addHeader() 0 4 1
A getHeaders() 0 4 2
A getAttachments() 0 19 3
A getInlinedAttachments() 0 19 3
A attach() 0 18 3
A attachContent() 0 18 3
A embed() 0 19 3
A embedContent() 0 19 3
A toString() 0 4 1
B stringifyEmails() 0 22 7
B convertEmails() 0 37 7

How to fix   Complexity   

Complex Class

Complex classes like Message often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Message, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Message.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version XXX
11
 * @link http://www.sweelix.net
12
 * @package sweelix\mailjet
13
 */
14
15
namespace sweelix\mailjet;
16
17
18
use yii\base\InvalidConfigException;
19
use yii\base\InvalidParamException;
20
use yii\base\NotSupportedException;
21
use yii\helpers\ArrayHelper;
22
use yii\mail\BaseMessage;
23
use Yii;
24
use yii\mail\MailerInterface;
25
26
/**
27
 * This component allow user to send an email
28
 *
29
 * @author Philippe Gaultier <[email protected]>
30
 * @copyright 2010-2017 Philippe Gaultier
31
 * @license http://www.sweelix.net/license license
32
 * @version XXX
33
 * @link http://www.sweelix.net
34
 * @package sweelix\mailjet
35
 * @since XXX
36
 */
37
class Message extends BaseMessage
38
{
39
    /**
40
     * @var string|array from
41
     */
42
    protected $from;
43
44
    /**
45
     * @var string|array from
46
     */
47
    protected $sender;
48
49
    /**
50
     * @var array
51
     */
52
    protected $to = [];
53
54
    /**
55
     * @var string|array reply to
56
     */
57
    protected $replyTo;
58
59
    /**
60
     * @var array
61
     */
62
    protected $cc = [];
63
64
    /**
65
     * @var array
66
     */
67
    protected $bcc = [];
68
69
    /**
70
     * @var string
71
     */
72
    protected $subject;
73
74
    /**
75
     * @var string
76
     */
77
    protected $textBody;
78
79
    /**
80
     * @var string
81
     */
82
    protected $htmlBody;
83
84
    /**
85
     * @var array
86
     */
87
    protected $attachments = [];
88
89
    /**
90
     * @var array
91
     */
92
    protected $inlinedAttachments = [];
93
94
    /**
95
     * @var string
96
     */
97
    protected $tag;
98
99
    /**
100
     * @var string
101
     */
102
    protected $trackOpens = 'account_default';
103
104
    /**
105
     * @var string
106
     */
107
    protected $trackClicks = 'account_default';
108
109
    /**
110
     * @var array
111
     */
112
    protected $headers = [];
113
114
    /**
115
     * @var integer
116
     */
117
    protected $templateId;
118
119
    /**
120
     * @var bool
121
     */
122
    protected $templateLanguage;
123
124
    /**
125
     * @var array model associated with the template
126
     */
127
    protected $templateModel = [];
128
129
    /**
130
     * @var bool
131
     */
132
    protected $inlineCss = true;
133
134
    protected $charset = 'utf-8';
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function getCharset()
140
    {
141
        return $this->charset;
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147
    public function setCharset($charset)
148
    {
149
        throw new NotSupportedException();
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    public function getFrom()
156
    {
157
        return self::stringifyEmails($this->from);
158
    }
159
160
    /**
161
     * @inheritdoc
162
     */
163
    public function setFrom($from)
164
    {
165
        $this->from = $from;
166
        return $this;
167
    }
168
169
    /**
170
     * @return array|string
171
     * @since XXX
172
     */
173
    public function getSender()
174
    {
175
        return $this->sender;
176
    }
177
178
    /**
179
     * @param string|array $sender
180
     * @return $this
181
     * @since XXX
182
     */
183
    public function setSender($sender)
184
    {
185
        $this->sender = $sender;
186
        return $this;
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192
    public function getTo()
193
    {
194
        return $this->to;
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200
    public function setTo($to)
201
    {
202
        if (is_string($to) === true) {
203
            $to = [$to];
204
        }
205
        $this->to = $to;
206
        return $this;
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212
    public function getReplyTo()
213
    {
214
        return self::stringifyEmails($this->replyTo);
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220
    public function setReplyTo($replyTo)
221
    {
222
        $this->replyTo = $replyTo;
223
        return $this;
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229
    public function getCc()
230
    {
231
        return $this->cc;
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237
    public function setCc($cc)
238
    {
239
        if (is_string($cc) === true) {
240
            $cc = [$cc];
241
        }
242
        $this->cc = $cc;
243
        return $this;
244
    }
245
246
    /**
247
     * @inheritdoc
248
     */
249
    public function getBcc()
250
    {
251
        return $this->bcc;
252
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257
    public function setBcc($bcc)
258
    {
259
        if (is_string($bcc) === true) {
260
            $bcc = [$bcc];
261
        }
262
        $this->bcc = $bcc;
263
        return $this;
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269
    public function getSubject()
270
    {
271
        return $this->subject;
272
    }
273
274
    /**
275
     * @inheritdoc
276
     */
277
    public function setSubject($subject)
278
    {
279
        $this->subject = $subject;
280
        return $this;
281
    }
282
283
    /**
284
     * @return string|null text body of the message
285
     * @since XXX
286
     */
287
    public function getTextBody()
288
    {
289
        return $this->textBody;
290
    }
291
292
    /**
293
     * @inheritdoc
294
     */
295
    public function setTextBody($text)
296
    {
297
        $this->textBody = $text;
298
        return $this;
299
    }
300
301
    /**
302
     * @return string|null html body of the message
303
     * @since XXX
304
     */
305
    public function getHtmlBody()
306
    {
307
        return $this->htmlBody;
308
    }
309
310
    /**
311
     * @inheritdoc
312
     */
313
    public function setHtmlBody($html)
314
    {
315
        $this->htmlBody = $html;
316
        return $this;
317
    }
318
319
    /**
320
     * @return string tag associated to the email
321
     * @since XXX
322
     */
323
    public function getTag()
324
    {
325
        return $this->tag;
326
    }
327
328
    /**
329
     * @param string $tag tag which should be associated to the email
330
     * @return $this
331
     * @since XXX
332
     */
333
    public function setTag($tag)
334
    {
335
        $this->tag = $tag;
336
        return $this;
337
    }
338
339
    /**
340
     * @param string $trackOpens can be account_default, disabled, enabled
341
     * @return $this
342
     * @since XXX
343
     */
344
    public function setTrackOpens($trackOpens)
345
    {
346
        $this->trackOpens = $trackOpens;
347
        return $this;
348
    }
349
350
    /**
351
     * @return string tracking status
352
     * @since XXX
353
     */
354
    public function getTrackOpens()
355
    {
356
        return $this->trackOpens;
357
    }
358
359
    /**
360
     * @param string $trackClicks can be account_default, disabled, enabled
361
     * @return $this
362
     * @since XXX
363
     */
364
    public function setTrackClicks($trackClicks)
365
    {
366
        $this->trackClicks = $trackClicks;
367
        return $this;
368
    }
369
370
    /**
371
     * @return string tracking status
372
     * @since XXX
373
     */
374
    public function getTrackClicks()
375
    {
376
        return $this->trackClicks;
377
    }
378
379
    /**
380
     * @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded
381
     * @return $this
382
     * @since XXX
383
     */
384
    public function setTemplateId($templateId)
385
    {
386
        $this->templateId = $templateId;
387
        return $this;
388
    }
389
390
    /**
391
     * @return integer|null current templateId
392
     * @since XXX
393
     */
394
    public function getTemplateId()
395
    {
396
        return $this->templateId;
397
    }
398
399
    /**
400
     * @param integer $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded
0 ignored issues
show
Bug introduced by
There is no parameter named $templateId. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
401
     * @return $this
402
     * @since XXX
403
     */
404
    public function setTemplateLanguage($processLanguage)
405
    {
406
        $this->templateLanguage = $processLanguage;
407
        return $this;
408
    }
409
410
    /**
411
     * @return integer|null current templateId
412
     * @since XXX
413
     */
414
    public function getTemplateLanguage()
415
    {
416
        return $this->templateLanguage;
417
    }
418
    /**
419
     * @param array $templateModel model associated with the template
420
     * @return $this
421
     * @since XXX
422
     */
423
    public function setTemplateModel($templateModel)
424
    {
425
        $this->templateModel = $templateModel;
426
        if (empty($this->templateModel) === false) {
427
            $this->templateLanguage = true;
428
        }
429
        return $this;
430
    }
431
432
    /**
433
     * @return array current template model
434
     * @since XXX
435
     */
436
    public function getTemplateModel()
437
    {
438
        return $this->templateModel;
439
    }
440
441
    /**
442
     * @param bool $inlineCss define if css should be inlined
443
     * @return $this
444
     * @since XXX
445
     */
446
    public function setInlineCss($inlineCss)
447
    {
448
        $this->inlineCss = $inlineCss;
449
        return $this;
450
    }
451
452
    /**
453
     * @return bool define if css should be inlined
454
     * @since XXX
455
     */
456
    public function getInlineCss()
457
    {
458
        return $this->inlineCss;
459
    }
460
461
    /**
462
     * @param string $headerName
463
     * @param string $headerValue
464
     * @since XXX
465
     */
466
    public function addHeader($headerName, $headerValue)
467
    {
468
        $this->headers[$headerName] = $headerValue;
469
    }
470
471
    /**
472
     * @return array|null headers which should be added to the mail
473
     * @since XXX
474
     */
475
    public function getHeaders()
476
    {
477
        return empty($this->headers) ? [] : $this->headers;
478
    }
479
480
    /**
481
     * @return array|null list of attachments
482
     * @since XXX
483
     */
484
    public function getAttachments()
485
    {
486
        if (empty($this->attachments) === true) {
487
            return null;
488
        } else {
489
            $attachments = array_map(function($attachment) {
490
                $item = [
491
                    'ContentType' => $attachment['ContentType'],
492
                    'Filename' => $attachment['Name'],
493
                    'Base64Content' => $attachment['Content'],
494
                ];
495
                if (isset($attachment['ContentID']) === true) {
496
                    $item['ContentID'] = $attachment['ContentID'];
497
                }
498
                return $item;
499
            }, $this->attachments);
500
            return $attachments;
501
        }
502
    }
503
504
    /**
505
     * @return array|null list of inlinedAttachments
506
     * @since XXX
507
     */
508
    public function getInlinedAttachments()
509
    {
510
        if (empty($this->inlinedAttachments) === true) {
511
            return null;
512
        } else {
513
            $inlinedAttachments = array_map(function($inlinedAttachment) {
514
                $item = [
515
                    'ContentType' => $inlinedAttachment['ContentType'],
516
                    'Filename' => $inlinedAttachment['Name'],
517
                    'Base64Content' => $inlinedAttachment['Content'],
518
                ];
519
                if (isset($inlinedAttachment['ContentID']) === true) {
520
                    $item['ContentID'] = $inlinedAttachment['ContentID'];
521
                }
522
                return $item;
523
            }, $this->inlinedAttachments);
524
            return $inlinedAttachments;
525
        }
526
    }
527
528
529
    /**
530
     * @inheritdoc
531
     */
532
    public function attach($fileName, array $options = [])
533
    {
534
        $attachment = [
535
            'Content' => base64_encode(file_get_contents($fileName))
536
        ];
537
        if (!empty($options['fileName'])) {
538
            $attachment['Name'] = $options['fileName'];
539
        } else {
540
            $attachment['Name'] = pathinfo($fileName, PATHINFO_BASENAME);
541
        }
542
        if (!empty($options['contentType'])) {
543
            $attachment['ContentType'] = $options['contentType'];
544
        } else {
545
            $attachment['ContentType'] = 'application/octet-stream';
546
        }
547
        $this->attachments[] = $attachment;
548
        return $this;
549
    }
550
551
    /**
552
     * @inheritdoc
553
     */
554
    public function attachContent($content, array $options = [])
555
    {
556
        $attachment = [
557
            'Content' => base64_encode($content)
558
        ];
559
        if (!empty($options['fileName'])) {
560
            $attachment['Name'] = $options['fileName'];
561
        } else {
562
            throw new InvalidParamException('Filename is missing');
563
        }
564
        if (!empty($options['contentType'])) {
565
            $attachment['ContentType'] = $options['contentType'];
566
        } else {
567
            $attachment['ContentType'] = 'application/octet-stream';
568
        }
569
        $this->attachments[] = $attachment;
570
        return $this;
571
    }
572
573
    /**
574
     * @inheritdoc
575
     */
576
    public function embed($fileName, array $options = [])
577
    {
578
        $embed = [
579
            'Content' => base64_encode(file_get_contents($fileName))
580
        ];
581
        if (!empty($options['fileName'])) {
582
            $embed['Name'] = $options['fileName'];
583
        } else {
584
            $embed['Name'] = pathinfo($fileName, PATHINFO_BASENAME);
585
        }
586
        if (!empty($options['contentType'])) {
587
            $embed['ContentType'] = $options['contentType'];
588
        } else {
589
            $embed['ContentType'] = 'application/octet-stream';
590
        }
591
        $embed['ContentID'] = uniqid();
592
        $this->inlinedAttachments[] = $embed;
593
        return $embed['ContentID'];
594
    }
595
596
    /**
597
     * @inheritdoc
598
     */
599
    public function embedContent($content, array $options = [])
600
    {
601
        $embed = [
602
            'Content' => base64_encode($content)
603
        ];
604
        if (!empty($options['fileName'])) {
605
            $embed['Name'] = $options['fileName'];
606
        } else {
607
            throw new InvalidParamException('Filename is missing');
608
        }
609
        if (!empty($options['contentType'])) {
610
            $embed['ContentType'] = $options['contentType'];
611
        } else {
612
            $embed['ContentType'] = 'application/octet-stream';
613
        }
614
        $embed['ContentID'] = uniqid();
615
        $this->inlinedAttachments[] = $embed;
616
        return $embed['ContentID'];
617
    }
618
619
    /**
620
     * @inheritdoc
621
     * @todo make real serialization to make message compliant with MailjetAPI
622
     */
623
    public function toString()
624
    {
625
        return serialize($this);
626
    }
627
628
629
    /**
630
     * @param array|string $emailsData email can be defined as string. In this case no transformation is done
631
     *                                 or as an array ['[email protected]', '[email protected]' => 'Email 2']
632
     * @return string|null
633
     * @since XXX
634
     */
635
    public static function stringifyEmails($emailsData)
636
    {
637
        $emails = null;
638
        if (empty($emailsData) === false) {
639
            if (is_array($emailsData) === true) {
640
                foreach ($emailsData as $key => $email) {
641
                    if (is_int($key) === true) {
642
                        $emails[] = $email;
643
                    } else {
644
                        if (preg_match('/[.,:]/', $email) > 0) {
645
                            $email = '"'. $email .'"';
646
                        }
647
                        $emails[] = $email . ' ' . '<' . $key . '>';
648
                    }
649
                }
650
                $emails = implode(', ', $emails);
651
            } elseif (is_string($emailsData) === true) {
652
                $emails = $emailsData;
653
            }
654
        }
655
        return $emails;
656
    }
657
    public static function convertEmails($emailsData)
658
    {
659
        $emails = [];
660
        if (empty($emailsData) === false) {
661
            if (is_array($emailsData) === true) {
662
                foreach ($emailsData as $key => $email) {
663
                    if (is_int($key) === true) {
664
                        $emails[] = [
665
                            'Email' => $email,
666
                        ];
667
                    } else {
668
                        /*if (preg_match('/[.,:]/', $email) > 0) {
669
                            $email = '"'. $email .'"';
670
                        }*/
671
                        $emails[] = [
672
                            'Email' => $key,
673
                            'Name' => $email,
674
                        ];
675
                    }
676
                }
677
            } elseif (is_string($emailsData) === true) {
678
                // "Test, Le" <[email protected]>
679
                if (preg_match('/"([^"]+)"\s<([^>]+)>/', $emailsData, $matches) > 0) {
680
                    $emails[] = [
681
                        'Email' => $matches[2],
682
                        'Name' => $matches[1],
683
                    ];
684
                } else {
685
                    $emails[] = [
686
                        'Email' => $emailsData,
687
                    ];
688
                }
689
            }
690
        }
691
        return $emails;
692
693
    }
694
}