Passed
Branch master (883225)
by Joao
02:33
created

PHPMailerWrapper::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
ccs 11
cts 12
cp 0.9167
rs 9.3142
cc 3
eloc 12
nc 4
nop 1
crap 3.0052
1
<?php
2
3
namespace ByJG\Mail\Wrapper;
4
5
use ByJG\Convert\FromUTF8;
6
use ByJG\Mail\Envelope;
7
use ByJG\Mail\Exception\MailApiException;
8
use ByJG\Mail\Override\PHPMailerOverride;
9
use ByJG\Mail\Util;
10
11
class PHPMailerWrapper extends BaseWrapper
12
{
13
    /**
14
     * @return \ByJG\Mail\Override\PHPMailerOverride
15
     */
16 4
    public function getMailer()
17
    {
18
        // the true param means it will throw exceptions on errors, which we need to catch
19 4
        return new PHPMailerOverride(true);
20
    }
21
22
    /**
23
     * @param Envelope $envelope
24
     * @return PHPMailerOverride
25
     * @throws \phpmailerException
26
     */
27 8
    protected function prepareMailer(Envelope $envelope)
28
    {
29 8
        $mail = $this->getMailer();
30 8
        $mail->Subject = FromUTF8::toIso88591Email($envelope->getSubject());
31 8
        $mail->CharSet = "utf-8";
32 8
        $mail->Body = $envelope->getBody();
33 8
        if ($envelope->isHtml()) {
34 8
            $mail->msgHTML($envelope->getBody());
35 8
            $mail->AltBody = $envelope->getBodyText();
36
        }
37
38 8
        $mail->isSMTP(); // telling the class to use SMTP
39
40 8
        if ($this->uri->getScheme() != "smtp") {
41 4
            $mail->SMTPSecure = $this->uri->getScheme(); // ssl ou tls!
42
        }
43
44 8
        $replyTo = Util::decomposeEmail($envelope->getReplyTo());
45 8
        $mail->addReplyTo($replyTo["email"], $replyTo["name"]);
46
47
        // Define From email
48 8
        $from = Util::decomposeEmail($envelope->getFrom());
49 8
        $mail->setFrom($from["email"], $from["name"]);
50
51
        // Add Recipients
52 8
        foreach ((array)$envelope->getTo() as $toItem) {
53 8
            $to = Util::decomposeEmail($toItem);
54 8
            $mail->addAddress($to["email"], $to["name"]);
55
        }
56
57
        // Add Carbon Copy
58 8
        foreach ((array)$envelope->getCC() as $ccItem) {
59 6
            $cc = Util::decomposeEmail($ccItem);
60 6
            $mail->addCC($cc["email"], $cc["name"]);
61
        }
62
63
        // Add Blind Carbon Copy
64 8
        foreach ((array)$envelope->getBCC() as $bccItem) {
65 6
            $bcc = Util::decomposeEmail($bccItem);
66 6
            $mail->addCustomHeader("Bcc: " . $bcc["email"]);
67
        }
68
69
        // Attachments
70 8
        foreach ((array)$envelope->getAttachments() as $name => $value) {
71 4
            switch ($value['disposition']) {
72 4
                case 'attachment':
73 2
                    $mail->addAttachment(
74 2
                        $value['content'],
75 2
                        $name,
76 2
                        'base64',
77 2
                        $value['content-type'],
78 2
                        'attachment'
79
                    );
80 2
                    break;
81
82 2
                case 'inline':
83 2
                    $mail->addEmbeddedImage($value['content'], $name, $name, 'base64', $value['content-type']);
84 2
                    break;
85
86
                default:
87 4
                    throw new \InvalidArgumentException('Invalid attachment type');
88
            }
89
        }
90
91 8
        return $mail;
92
    }
93
94
    /**
95
     * @param Envelope $envelope
96
     * @return bool
97
     * @throws \ByJG\Mail\Exception\MailApiException
98
     * @throws \Exception
99
     * @throws \phpmailerException
100
     */
101 4
    public function send(Envelope $envelope)
102
    {
103 4
        $this->validate($envelope);
104
105 4
        $mail = $this->prepareMailer($envelope);
106
107 4
        $mail->Host = $this->uri->getHost();
108 4
        $mail->Port = $this->uri->getPort();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->uri->getPort() can also be of type string. However, the property $Port is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
109
110 4
        if (!empty($this->uri->getUsername())) {
111 4
            $mail->SMTPAuth = true;
112 4
            $mail->Username = $this->uri->getUsername(); // SMTP account username
113 4
            $mail->Password = $this->uri->getPassword();        // SMTP account password
114
        }
115
116 4
        if (!$mail->send()) {
117
            throw new MailApiException($mail->ErrorInfo);
118
        }
119
120 4
        return true;
121
    }
122
}
123