Completed
Push — master ( e14b0e...2f3639 )
by Joao
02:07
created

MailgunApiWrapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B send() 0 34 4
1
<?php
2
3
namespace ByJG\Mail\Wrapper;
4
5
use ByJG\Mail\Envelope;
6
use ByJG\Mail\Wrapper\PHPMailerWrapper;
7
use ByJG\Util\UploadFile;
8
use ByJG\Util\WebRequest;
9
use Exception;
10
11
class MailgunApiWrapper extends PHPMailerWrapper
12
{
13
    /**
14
     * malgun://APIKEY@DOMAINNAME
15
     *
16
     * @param Envelope $envelope
17
     * @throws Exception
18
     */
19
    public function send(Envelope $envelope)
20
    {
21
        $mail = $this->prepareMailer($envelope);
22
23
        // Call the preSend to set all PHPMailer variables and get the correct header and body;
24
        $message = $mail->getFullMessageEnvelope();
25
26
        // Fix BCC header because PHPMailer does not send to us
27
        foreach ((array) $envelope->getBCC() as $bccEmail) {
28
            $message = 'Bcc: '.$bccEmail."\n".$message;
29
        }
30
31
        $domainName = $this->connection->getServer();
32
33
        $request = new WebRequest("https://api.mailgun.net/v3/$domainName/messages.mime");
34
        $request->setCredentials($this->connection->getUsername(), $this->connection->getPassword());
35
36
        $upload = [
37
            new UploadFile('message', $message, 'message.mime')
38
        ];
39
        // Add "To;"
40
        foreach ((array) $envelope->getTo() as $toEmail) {
41
            $upload[] = new UploadFile('to', $toEmail);
42
        }
43
44
        $result = $request->postUploadFile($upload);
0 ignored issues
show
Bug introduced by
The method postUploadFile() does not seem to exist on object<ByJG\Util\WebRequest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
46
        $resultJson = json_decode($result, true);
47
        if (!isset($resultJson['id'])) {
48
            throw new Exception('Mailgun: '.$resultJson['message']);
49
        }
50
51
        return true;
52
    }
53
}
54