Passed
Push — master ( d57c11...8c6e4f )
by Joao
03:16 queued 36s
created

PHPMailerWrapper::getMailer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     *
24
     * @param Envelope $envelope
25
     * @return PHPMailerOverride
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 8
        }
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 4
        }
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 8
        }
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 8
        }
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 8
        }
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
                        'attachment'
79 2
                    );
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
                    throw new \InvalidArgumentException('Invalid attachment type');
88 4
            }
89 8
        }
90
91 8
        return $mail;
92
    }
93
94
    /**
95
     * @param Envelope $envelope
96
     * @return bool
97
     * @throws \ByJG\Mail\Exception\MailApiException
98
     */
99 4
    public function send(Envelope $envelope)
100
    {
101 4
        $this->validate($envelope);
102
103 4
        $mail = $this->prepareMailer($envelope);
104
105 4
        $mail->Host = $this->uri->getHost();
106 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...
107
108 4
        if (!empty($this->uri->getUsername())) {
109 4
            $mail->SMTPAuth = true;
110 4
            $mail->Username = $this->uri->getUsername(); // SMTP account username
111 4
            $mail->Password = $this->uri->getPassword();        // SMTP account password
112 4
        }
113
114 4
        if (!$mail->send()) {
115
            throw new MailApiException($mail->ErrorInfo);
116
        }
117
118 4
        return true;
119
    }
120
}
121