GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Message::getMimeVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Email;
15
16
// ------------------------------------------------------------------------
17
18
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
19
20
/**
21
 * Class Message
22
 *
23
 * @package O2System\Email
24
 */
25
class Message
26
{
27
    /**
28
     * Message::PRIORITY_HIGHEST
29
     *
30
     * Highest message priority.
31
     *
32
     * @var int
33
     */
34
    const PRIORITY_HIGHEST = 1;
35
    /**
36
     * Message::PRIORITY_HIGH
37
     *
38
     * High message priority.
39
     *
40
     * @var int
41
     */
42
    const PRIORITY_HIGH = 2;
43
    /**
44
     * Message::PRIORITY_NORMAL
45
     *
46
     * Normal message priority.
47
     *
48
     * @var int
49
     */
50
    const PRIORITY_NORMAL = 3;
51
    /**
52
     * Message::PRIORITY_HIGHEST
53
     *
54
     * Highest message priority.
55
     *
56
     * @var int
57
     */
58
    const PRIORITY_LOW = 4;
59
    /**
60
     * Message::PRIORITY_LOWEST
61
     *
62
     * Lowest message priority.
63
     *
64
     * @var int
65
     */
66
    const PRIORITY_LOWEST = 5;
67
    /**
68
     * Message::$from
69
     *
70
     * Email from.
71
     *
72
     * @var Address
73
     */
74
    protected $from;
75
    /**
76
     * Message::$replyTo
77
     *
78
     * Email reply-to.
79
     *
80
     * @var Address
81
     */
82
    protected $replyTo;
83
    /**
84
     * Message::$returnTo
85
     *
86
     * Email return path.
87
     *
88
     * @var string
89
     */
90
    protected $returnPath;
91
    /**
92
     * Message::$to
93
     *
94
     * Email to Receiver, or receivers of the mail.
95
     *
96
     * @var array
97
     */
98
    protected $to = [];
99
    /**
100
     * Message::$cc
101
     *
102
     * Email copy carbon of Receiver, or receivers of the mail.
103
     *
104
     * @var array
105
     */
106
    protected $cc = [];
107
    /**
108
     * Message::$bcc
109
     *
110
     * Email blank copy carbon of Receiver, or receivers of the mail.
111
     *
112
     * @var array
113
     */
114
    protected $bcc = [];
115
    /**
116
     * Message::$subscribers
117
     *
118
     * Email subscribers.
119
     *
120
     * @var array
121
     */
122
    protected $subscribers = [];
123
    /**
124
     * Message::$subject
125
     *
126
     * Subject of the email to be sent.
127
     *
128
     * @var string
129
     */
130
    protected $subject;
131
    /**
132
     * Message::$body
133
     *
134
     * Email message body to be sent.
135
     *
136
     * @var string
137
     */
138
    protected $body;
139
    /**
140
     * Message::$altBody
141
     *
142
     * Email message alternative body to be sent.
143
     *
144
     * @var string
145
     */
146
    protected $altBody;
147
    /**
148
     * Message::$headers
149
     *
150
     * Headers of the message to be sent.
151
     *
152
     * @var array
153
     */
154
    protected $headers = [];
155
    /**
156
     * Message::$attachments
157
     *
158
     * Email attachments.
159
     *
160
     * @var array
161
     */
162
    protected $attachments = [];
163
    /**
164
     * Message::$encoding
165
     *
166
     * Message encoding.
167
     *
168
     * @var string
169
     */
170
    protected $encoding = '8bit';
171
    /**
172
     * Message::$charset
173
     *
174
     * Character set (default: utf-8)
175
     *
176
     * @var string
177
     */
178
    protected $charset = 'utf-8';
179
    /**
180
     * Message::$mimeVersion
181
     *
182
     * Message mime version (default: 1.0).
183
     *
184
     * @var string
185
     */
186
    protected $mimeVersion = '1.0';
187
    /**
188
     * Message::$contentType
189
     *
190
     * Email body content type (default: text).
191
     *
192
     * @var string
193
     */
194
    protected $contentType = 'text';
195
    /**
196
     * Message::$priority
197
     *
198
     * Email priority
199
     *
200
     * @var string
201
     */
202
    protected $priority;
203
    /**
204
     * Message::$batchLimit
205
     *
206
     * Email sending batch limit
207
     *
208
     * @var int
209
     */
210
    protected $batchLimit = 100;
211
212
213
    // ------------------------------------------------------------------------
214
215
    /**
216
     * Message::getEncoding
217
     *
218
     * Gets message encoding.
219
     *
220
     * @return string
221
     */
222
    public function getEncoding()
223
    {
224
        return $this->encoding;
225
    }
226
227
    // ------------------------------------------------------------------------
228
229
    /**
230
     * Message::setCharset
231
     *
232
     * Sets mail charset.
233
     *
234
     * @param string $charset
235
     *
236
     * @return static
237
     */
238
    public function charset($charset)
239
    {
240
        /**
241
         * Character sets valid for 7-bit encoding,
242
         * excluding language suffix.
243
         */
244
        $baseCharsets = [
245
            'us-ascii',
246
            'iso-2022-',
247
        ];
248
249
        foreach ($baseCharsets as $baseCharset) {
250
            if (strpos($baseCharset, $charset) === 0) {
251
                $this->encoding('7bit');
252
                break;
253
            }
254
        }
255
256
        $this->charset = $charset;
257
258
        return $this;
259
    }
260
261
    // ------------------------------------------------------------------------
262
263
    /**
264
     * Message::encoding
265
     *
266
     * Sets message encoding.
267
     *
268
     * @param string $encoding
269
     *
270
     * @return static
271
     */
272
    public function encoding($encoding)
273
    {
274
        /**
275
         * Valid mail encodings
276
         */
277
        $bitDepths = [
278
            '7bit',
279
            '8bit',
280
        ];
281
282
        in_array($encoding, $bitDepths) OR $encoding = '8bit';
283
284
        $this->encoding = $encoding;
285
286
        return $this;
287
    }
288
289
    // ------------------------------------------------------------------------
290
291
    /**
292
     * Message::getCharset
293
     *
294
     * Gets mail charset.
295
     *
296
     * @return string
297
     */
298
    public function getCharset()
299
    {
300
        return $this->charset;
301
    }
302
303
    // ------------------------------------------------------------------------
304
305
    /**
306
     * Message::setMimeVersion
307
     *
308
     * Sets mail mime version.
309
     *
310
     * @param string $mimeVersion
311
     *
312
     * @return static
313
     */
314
    public function mimeVersion($mimeVersion)
315
    {
316
        $this->mimeVersion = $mimeVersion;
317
318
        return $this;
319
    }
320
321
    // ------------------------------------------------------------------------
322
323
    /**
324
     * Message::getMimeVersion
325
     *
326
     * Gets mail mime version.
327
     *
328
     * @return string
329
     */
330
    public function getMimeVersion()
331
    {
332
        return $this->mimeVersion;
333
    }
334
335
    // ------------------------------------------------------------------------
336
337
    /**
338
     * Message::contentType
339
     *
340
     * Sets message content type.
341
     *
342
     * @param string $contentType
343
     *
344
     * @return static
345
     */
346
    public function contentType($contentType)
347
    {
348
349
        $this->contentType = $contentType;
350
351
        return $this;
352
    }
353
354
    // ------------------------------------------------------------------------
355
356
    /**
357
     * Message::getContentType
358
     *
359
     * Gets message content type.
360
     *
361
     * @return string
362
     */
363
    public function getContentType()
364
    {
365
        return $this->contentType;
366
    }
367
368
    // ------------------------------------------------------------------------
369
370
    /**
371
     * Message::from
372
     *
373
     * Sets mail from.
374
     *
375
     * @param string $email
376
     * @param string $name
377
     *
378
     * @return static
379
     */
380
    public function from($email, $name = null)
381
    {
382
        $this->setAddress($email, $name, 'from');
383
384
        if (empty($this->replyTo)) {
385
            $this->replyTo = $this->from;
386
        }
387
388
        return $this;
389
    }
390
391
    // ------------------------------------------------------------------------
392
393
    /**
394
     * Message::setFrom
395
     *
396
     * Sets mail from.
397
     *
398
     * @param string $email
399
     * @param string $name
400
     * @param string $object from | replyTo
401
     *
402
     * @return void
403
     */
404
    protected function setAddress($email, $name = null, $object)
405
    {
406
        if ($email instanceof Address) {
0 ignored issues
show
introduced by
$email is never a sub-type of O2System\Email\Address.
Loading history...
407
            $this->{$object} = $email;
408
        } else {
409
            $this->{$object} = new Address($email, $name);
410
        }
411
    }
412
413
    // ------------------------------------------------------------------------
414
415
    /**
416
     * Message::getFrom
417
     *
418
     * Gets from mail address.
419
     *
420
     * @return \O2System\Email\Address|bool
421
     */
422
    public function getFrom()
423
    {
424
        if ($this->from instanceof Address) {
0 ignored issues
show
introduced by
$this->from is always a sub-type of O2System\Email\Address.
Loading history...
425
            return $this->from;
426
        }
427
428
        return false;
429
    }
430
431
    // ------------------------------------------------------------------------
432
433
    /**
434
     * Message::replyTo
435
     *
436
     * Sets reply to mail address.
437
     *
438
     * @param string $email
439
     * @param string $name
440
     *
441
     * @return static
442
     */
443
    public function replyTo($email, $name = null)
444
    {
445
        $this->setAddress($email, $name, 'replyTo');
446
447
        return $this;
448
    }
449
450
    // ------------------------------------------------------------------------
451
452
    /**
453
     * Message::getReplyTo
454
     *
455
     * Gets reply to mail address.
456
     *
457
     * @return \O2System\Email\Address|bool
458
     */
459
    public function getReplyTo()
460
    {
461
        if ($this->replyTo instanceof Address) {
0 ignored issues
show
introduced by
$this->replyTo is always a sub-type of O2System\Email\Address.
Loading history...
462
            return $this->replyTo;
463
        }
464
465
        return false;
466
    }
467
468
    // ------------------------------------------------------------------------
469
470
    /**
471
     * Message::returnPath
472
     *
473
     * Sets return to mail address.
474
     *
475
     * @param string $path
476
     *
477
     * @return static
478
     */
479
    public function returnPath($path)
480
    {
481
        if (strpos($path, '@') !== false) {
482
            $this->returnPath = strstr($path, '@');
483
        } else {
484
            $this->returnPath = '@' . $path;
485
        }
486
487
        return $this;
488
    }
489
490
    // ------------------------------------------------------------------------
491
492
    /**
493
     * Message::getReturnTo
494
     *
495
     * Gets return to mail address.
496
     *
497
     * @return \O2System\Email\Address|bool
498
     */
499
    public function getReturnPath()
500
    {
501
        if (empty($this->returnPath)) {
502
            if ($this->from instanceof Address) {
0 ignored issues
show
introduced by
$this->from is always a sub-type of O2System\Email\Address.
Loading history...
503
                return $this->returnPath = strstr($this->from->getEmail(), '@');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->returnPath...>from->getEmail(), '@') returns the type string which is incompatible with the documented return type O2System\Email\Address|boolean.
Loading history...
Bug introduced by
It seems like $this->from->getEmail() can also be of type false; however, parameter $haystack of strstr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

503
                return $this->returnPath = strstr(/** @scrutinizer ignore-type */ $this->from->getEmail(), '@');
Loading history...
504
            }
505
506
            return false;
507
        }
508
509
        return $this->returnPath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->returnPath returns the type string which is incompatible with the documented return type O2System\Email\Address|boolean.
Loading history...
510
    }
511
512
    // ------------------------------------------------------------------------
513
514
    /**
515
     * Message::subject
516
     *
517
     * Sets mail subject.
518
     *
519
     * @param string $subject
520
     *
521
     * @return static
522
     */
523
    public function subject($subject)
524
    {
525
        $this->subject = trim($subject);
526
527
        return $this;
528
    }
529
530
    // ------------------------------------------------------------------------
531
532
    /**
533
     * Message::getSubject
534
     *
535
     * Gets mail subject.
536
     *
537
     * @return string
538
     */
539
    public function getSubject()
540
    {
541
        return $this->subject;
542
    }
543
544
    // ------------------------------------------------------------------------
545
546
    /**
547
     * Message::body
548
     *
549
     * Sets message body.
550
     *
551
     * @param string $body
552
     *
553
     * @return static
554
     */
555
    public function body($body)
556
    {
557
        $this->body = rtrim(str_replace("\r", '', $body));
558
559
        /* strip slashes only if magic quotes is ON
560
           if we do it with magic quotes OFF, it strips real, user-inputted chars.
561
562
           NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
563
             it will probably not exist in future versions at all.
564
        */
565
        if ( ! is_php('5.4') && get_magic_quotes_gpc()) {
566
            $this->body = stripslashes($this->body);
567
        }
568
569
        if (class_exists('O2System\Framework', false)) {
570
            $this->body = presenter()->assets->parseSourceCode($this->body);
0 ignored issues
show
Bug introduced by
The function presenter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

570
            $this->body = /** @scrutinizer ignore-call */ presenter()->assets->parseSourceCode($this->body);
Loading history...
571
        }
572
573
        $cssToInlineStyles = new CssToInlineStyles();
574
        $this->body = $cssToInlineStyles->convert($this->body);
575
576
        return $this;
577
    }
578
579
    // ------------------------------------------------------------------------
580
581
    /**
582
     * Message::getBody
583
     *
584
     * Gets mail body.
585
     *
586
     * @return string
587
     */
588
    public function getBody()
589
    {
590
        return $this->body;
591
    }
592
593
    // ------------------------------------------------------------------------
594
595
    /**
596
     * Message::altBody
597
     *
598
     * Sets message alternative body.
599
     *
600
     * @param string $body
601
     *
602
     * @return static
603
     */
604
    public function altBody($altBody)
605
    {
606
        $this->altBody = trim($altBody);
607
608
        return $this;
609
    }
610
611
    // ------------------------------------------------------------------------
612
613
    /**
614
     * Message::getAltBody
615
     *
616
     * Gets mail body.
617
     *
618
     * @return string
619
     */
620
    public function getAltBody()
621
    {
622
        if (is_html($this->body)) {
623
            $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->body, $match) ? $match[ 1 ] : $this->body;
624
            $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
625
626
            for ($i = 20; $i >= 3; $i--) {
627
                $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
628
            }
629
630
            // Reduce multiple spaces
631
            $body = preg_replace('| +|', ' ', $body);
0 ignored issues
show
Unused Code introduced by
The assignment to $body is dead and can be removed.
Loading history...
632
633
            return $this->body;
634
        }
635
636
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
637
    }
638
639
    // ------------------------------------------------------------------------
640
641
    /**
642
     * Message::priority
643
     *
644
     * Sets mail priority
645
     *
646
     * @param int $priority
647
     */
648
    public function priority($priority)
649
    {
650
        $priorities = [
651
            1 => '1 (Highest)',
652
            2 => '2 (High)',
653
            3 => '3 (Normal)',
654
            4 => '4 (Low)',
655
            5 => '5 (Lowest)',
656
        ];
657
658
        if (array_key_exists($priority, $priorities)) {
659
            $this->priority = $priorities[ $priority ];
660
        }
661
    }
662
663
    // ------------------------------------------------------------------------
664
665
    /**
666
     * Message::getPriority
667
     *
668
     * Gets mail priority.
669
     *
670
     * @return string|bool
671
     */
672
    public function getPriority()
673
    {
674
        if ( ! empty($this->priority)) {
675
            return $this->priority;
676
        }
677
678
        return false;
679
    }
680
681
    // ------------------------------------------------------------------------
682
683
    /**
684
     * Message::addHeader
685
     *
686
     * Add additional mail header.
687
     *
688
     * @param string $name
689
     * @param string $value
690
     *
691
     * @return static
692
     */
693
    public function addHeader($name, $value)
694
    {
695
        $this->headers[ $name ] = $value;
696
697
        return $this;
698
    }
699
700
    // ------------------------------------------------------------------------
701
702
    /**
703
     * Message::getHeaders
704
     *
705
     * Gets message additional headers.
706
     *
707
     * @return array
708
     */
709
    public function getHeaders()
710
    {
711
        return $this->headers;
712
    }
713
714
    // ------------------------------------------------------------------------
715
716
    /**
717
     * Message::addTo
718
     *
719
     * Add to mail.
720
     *
721
     * @param string $email
722
     * @param string $name
723
     *
724
     * @return static
725
     */
726
    public function to($email, $name = null)
727
    {
728
        $this->addAddress($email, $name, 'to');
729
730
        return $this;
731
    }
732
733
    // ------------------------------------------------------------------------
734
735
    /**
736
     * Message::addAddress
737
     *
738
     * Add mail address to object.
739
     *
740
     * @param string      $email
741
     * @param string|null $name
742
     * @param string      $object
743
     *
744
     * @return void
745
     */
746
    protected function addAddress($email, $name = null, $object)
747
    {
748
        if ($email instanceof Address) {
0 ignored issues
show
introduced by
$email is never a sub-type of O2System\Email\Address.
Loading history...
749
            $this->{$object}[] = $email;
750
        } elseif (is_array($email)) {
0 ignored issues
show
introduced by
The condition is_array($email) is always false.
Loading history...
751
752
            if (is_numeric(key($email))) {
753
                foreach ($email as $address) {
754
                    $this->{$object}[] = new Address($address);
755
                }
756
            } else {
757
                foreach ($email as $name => $address) {
758
                    $this->{$object}[] = new Address($address, $name);
759
                }
760
            }
761
762
        } elseif (strpos($email, ',') !== false) {
763
            $emails = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
764
765
            foreach ($emails as $email) {
0 ignored issues
show
introduced by
$email is overwriting one of the parameters of this function.
Loading history...
766
                $this->{$object}[] = new Address($email);
767
            }
768
        } else {
769
            $this->{$object}[] = new Address($email, $name);
770
        }
771
    }
772
773
    // ------------------------------------------------------------------------
774
775
    /**
776
     * Message::getTo
777
     *
778
     * Gets mail to addresses.
779
     *
780
     * @return array|bool
781
     */
782
    public function getTo()
783
    {
784
        if (count($this->to)) {
785
            return $this->to;
786
        }
787
788
        return false;
789
    }
790
791
    // ------------------------------------------------------------------------
792
793
    /**
794
     * Message::addCc
795
     *
796
     * Add to copy carbon mail.
797
     *
798
     * @param string $email
799
     * @param string $name
800
     *
801
     * @return static
802
     */
803
    public function cc($email, $name = null)
804
    {
805
        $this->addAddress($email, $name, 'cc');
806
807
        return $this;
808
    }
809
810
    // ------------------------------------------------------------------------
811
812
    /**
813
     * Message::getCc
814
     *
815
     * Gets mail cc addresses.
816
     *
817
     * @return array|bool
818
     */
819
    public function getCc()
820
    {
821
        if (count($this->cc)) {
822
            return $this->cc;
823
        }
824
825
        return false;
826
    }
827
828
    // ------------------------------------------------------------------------
829
830
    /**
831
     * Message::addBcc
832
     *
833
     * Add to blank copy carbon mail.
834
     *
835
     * @param string $email
836
     * @param string $name
837
     *
838
     * @return static
839
     */
840
    public function bcc($email, $name = null, $limit = 100)
841
    {
842
        $this->batchLimit = (int)$limit;
843
        $this->addAddress($email, $name, 'bcc');
844
845
        return $this;
846
    }
847
848
    // ------------------------------------------------------------------------
849
850
    /**
851
     * Message::getBcc
852
     *
853
     * Gets mail bcc addresses.
854
     *
855
     * @return array|bool
856
     */
857
    public function getBcc()
858
    {
859
        if (count($this->bcc)) {
860
            return $this->bcc;
861
        }
862
863
        return false;
864
    }
865
866
    // ------------------------------------------------------------------------
867
868
    /**
869
     * Message::subscriber
870
     *
871
     * Add mailing list subscribers.
872
     *
873
     * @param array $email
874
     * @param int   $limit
875
     *
876
     * @return static
877
     */
878
    public function subscribers($email, $limit = 100)
879
    {
880
        $this->batchLimit = (int)$limit;
881
        $this->addAddress($email, null, 'subscribers');
0 ignored issues
show
Bug introduced by
$email of type array is incompatible with the type string expected by parameter $email of O2System\Email\Message::addAddress(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

881
        $this->addAddress(/** @scrutinizer ignore-type */ $email, null, 'subscribers');
Loading history...
882
883
        return $this;
884
    }
885
886
    // ------------------------------------------------------------------------
887
888
    /**
889
     * Message::getSubscribers
890
     *
891
     * Gets mail subscribers addresses.
892
     *
893
     * @return array|bool
894
     */
895
    public function getSubscribers()
896
    {
897
        if (count($this->subscribers)) {
898
            return $this->subscribers;
899
        }
900
901
        return false;
902
    }
903
904
    // ------------------------------------------------------------------------
905
906
    /**
907
     * Message::addAttachment
908
     *
909
     * Add mail attachment.
910
     *
911
     * @param string      $attachment
912
     * @param string|null $filename
913
     *
914
     * @return bool
915
     */
916
    public function addAttachment($attachment, $filename = null)
917
    {
918
        if (is_file($attachment)) {
919
            $filename = isset($filename) ? $filename : pathinfo($attachment, PATHINFO_BASENAME);
920
921
            if ( ! in_array($attachment, $this->attachments)) {
922
                $this->attachments[ $filename ] = $attachment;
923
            }
924
        }
925
926
        return false;
927
    }
928
929
    // ------------------------------------------------------------------------
930
931
    /**
932
     * Message::getAttachments
933
     *
934
     * Gets mail attachments.
935
     *
936
     * @return array|bool
937
     */
938
    public function getAttachments()
939
    {
940
        if (count($this->attachments)) {
941
            return $this->attachments;
942
        }
943
944
        return false;
945
    }
946
}