MessageBuilder::setFrom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Da\Mailer\Builder;
4
5
use Da\Mailer\Mail\Dto\EmailAddress;
6
use Da\Mailer\Mail\Dto\File;
7
use Da\Mailer\Model\MailMessage;
8
use Symfony\Component\Mime\Address;
9
use Symfony\Component\Mime\Email;
10
11
class MessageBuilder extends Buildable
12
{
13
    /**
14
     * @param MailMessage $mailMessage
15
     * @return Email
16
     * @throws \Exception
17
     */
18
    public static function make($mailMessage = null): Email
19
    {
20
        $message = new Email();
21
        $message->subject($mailMessage->subject);
22
23
        self::setFrom($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setFrom() does only seem to accept Da\Mailer\Model\MailMessage, 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

23
        self::setFrom(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
24
        self::setTo($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setTo() does only seem to accept Da\Mailer\Model\MailMessage, 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

24
        self::setTo(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
25
        self::setCc($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setCc() does only seem to accept Da\Mailer\Model\MailMessage, 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

25
        self::setCc(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
26
        self::setBcc($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setBcc() does only seem to accept Da\Mailer\Model\MailMessage, 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

26
        self::setBcc(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
27
        self::setHtml($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setHtml() does only seem to accept Da\Mailer\Model\MailMessage, 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

27
        self::setHtml(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
28
        self::setText($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setText() does only seem to accept Da\Mailer\Model\MailMessage, 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

28
        self::setText(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
29
        self::setAttachments($mailMessage, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailMessage can also be of type null; however, parameter $mailMessage of Da\Mailer\Builder\MessageBuilder::setAttachments() does only seem to accept Da\Mailer\Model\MailMessage, 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

29
        self::setAttachments(/** @scrutinizer ignore-type */ $mailMessage, $message);
Loading history...
30
31
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $message returns the type Symfony\Component\Mime\Email which is incompatible with the return type mandated by Da\Mailer\Builder\Buildable::make() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
32
    }
33
34
    /**
35
     * @param string|array|EmailAddress $emails
36
     * @param string $method
37
     * @param Email $message
38
     * @return void
39
     */
40
    protected static function setEmail($emails, string $method, Email $message)
41
    {
42
        if (is_string($emails)) {
43
            $message->{$method}($emails);
44
45
            return;
46
        }
47
48
        if (is_array($emails)) {
49
            foreach ($emails as $email) {
50
                if ($email instanceof EmailAddress) {
51
                    $email = $email->parseToMailer();
52
                }
53
54
                $message->{'add' . strtoupper($method)}($email);
55
            }
56
57
            return;
58
        }
59
60
        $message->{$method}($emails->parseToMailer());
61
    }
62
63
    /**
64
     * @param MailMessage $mailMessage
65
     * @param Email $message
66
     * @return void
67
     */
68
    public static function setFrom(MailMessage $mailMessage, Email $message): void
69
    {
70
        self::setEmail($mailMessage->from, 'from', $message);
71
    }
72
73
    /**
74
     * @param MailMessage $mailMessage
75
     * @param Email $message
76
     * @return void
77
     */
78
    public static function setTo(MailMessage $mailMessage, Email $message): void
79
    {
80
        self::setEmail($mailMessage->to, 'to', $message);
81
    }
82
83
    /**
84
     * @param MailMessage $mailMessage
85
     * @param Email $message
86
     * @return void
87
     */
88
    protected static function setCc(MailMessage $mailMessage, Email $message)
89
    {
90
        if (! is_null($mailMessage->cc)) {
0 ignored issues
show
introduced by
The condition is_null($mailMessage->cc) is always false.
Loading history...
91
            self::setEmail($mailMessage->cc, 'cc', $message);
92
        }
93
    }
94
95
    /**
96
     * @param MailMessage $mailMessage
97
     * @param Email $message
98
     * @return void
99
     */
100
    protected static function setBcc(MailMessage $mailMessage, Email $message)
101
    {
102
        if (! is_null($mailMessage->bcc)) {
0 ignored issues
show
introduced by
The condition is_null($mailMessage->bcc) is always false.
Loading history...
103
            self::setEmail($mailMessage->bcc, 'bcc', $message);
104
        }
105
    }
106
107
    /**
108
     * @param MailMessage $mailMessage
109
     * @param Email $message
110
     * @return void
111
     * @throws \Exception
112
     */
113
    protected static function setHtml(MailMessage $mailMessage, Email $message)
114
    {
115
        $config = self::getConfig();
116
        $html = $mailMessage->bodyHtml;
117
118
        if (isset($html)) {
119
            $html = self::extractBodyMessage($html);
120
121
            $message->html($html, $config['mail-charset']);
122
        }
123
    }
124
125
    /**
126
     * @param MailMessage $mailMessage
127
     * @param Email $message
128
     * @return void
129
     * @throws \Exception
130
     */
131
    protected static function setText(MailMessage $mailMessage, Email $message)
132
    {
133
        $config = self::getConfig();
134
        $text = $mailMessage->bodyText;
135
136
        if (isset($mailMessage->bodyText)) {
137
            $text = self::extractBodyMessage($text);
138
139
            $message->text($text, $config['mail-charset']);
140
        }
141
    }
142
143
    /**
144
     * @param MailMessage $mailMessage
145
     * @param Email $message
146
     * @return void
147
     */
148
    protected static function setAttachments(MailMessage $mailMessage, Email $message)
149
    {
150
        /** @var File $attachment */
151
        foreach ($mailMessage->getAttachments() as $attachment) {
152
            $message->attachFromPath($attachment->getPath(), $attachment->getName());
153
        }
154
    }
155
156
    /**
157
     * @param string $message
158
     * @return false|resource|string
159
     */
160
    protected static function extractBodyMessage(string $message)
161
    {
162
        return realpath($message)
163
            ? fopen($message, 'r')
164
            : $message;
165
    }
166
}
167