Passed
Push — master ( c025bc...4ff0c8 )
by Joao
39s queued 10s
created

MailgunApiWrapper::getApiUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
1
<?php
2
3
namespace ByJG\Mail\Wrapper;
4
5
use ByJG\Mail\Envelope;
6
use ByJG\Mail\Exception\InvalidEMailException;
7
use ByJG\Mail\Exception\MailApiException;
8
use ByJG\Util\CurlException;
9
use ByJG\Util\MultiPartItem;
10
use ByJG\Util\WebRequest;
11
12
class MailgunApiWrapper extends PHPMailerWrapper
13
{
14
15
    private $regions = [
16
        'us' => 'api.mailgun.net',
17
        'eu' => 'api.eu.mailgun.net',
18
    ];
19
20
    /**
21
     * @return \ByJG\Util\WebRequest
22
     */
23 1
    public function getRequestObject()
24
    {
25 1
        $domainName = $this->uri->getHost();
26 1
        $apiUri = $this->getApiUri();
27 1
        $request = new WebRequest("https://$apiUri/v3/$domainName/messages");
28 1
        $request->setCredentials('api', $this->uri->getUsername());
29
30 1
        return $request;
31
    }
32
33
    /**
34
     * malgun://api:APIKEY@DOMAINNAME
35
     *
36
     * @param Envelope $envelope
37
     * @return bool
38
     * @throws MailApiException
39
     * @throws InvalidEMailException
40
     * @throws CurlException
41
     */
42 4
    public function send(Envelope $envelope)
43
    {
44 4
        $this->validate($envelope);
45
46
        $message = [
47 4
            new MultiPartItem('from', $envelope->getFrom()),
48 4
            new MultiPartItem('subject', $envelope->getSubject()),
49 4
            new MultiPartItem('html', $envelope->getBody()),
50 4
            new MultiPartItem('text', $envelope->getBodyText()),
51
        ];
52
53
54 4
        foreach ((array)$envelope->getTo() as $to) {
55 4
            $message[] = new MultiPartItem('to', $to);
56
        }
57
58 4
        foreach ((array)$envelope->getBCC() as $bcc) {
59 3
            $message[] = new MultiPartItem('bcc', $bcc);
60
        }
61
62 4
        if (!empty($envelope->getReplyTo())) {
63 4
            $message[] = new MultiPartItem('h:Reply-To', $envelope->getReplyTo());
64
        }
65
66 4
        foreach ((array)$envelope->getCC() as $cc) {
67 3
            $message[] = new MultiPartItem('cc', $cc);
68
        }
69
70 4
        foreach ((array)$envelope->getAttachments() as $name => $attachment) {
71 2
            $message[] = new MultiPartItem(
72 2
                $attachment['disposition'],
73 2
                file_get_contents($attachment['content']),
74 2
                $name,
75 2
                $attachment['content-type']
76
            );
77
        }
78
79 4
        $request = $this->getRequestObject();
80 4
        $result = $request->postMultiPartForm($message);
81 4
        $resultJson = json_decode($result, true);
82 4
        if (!isset($resultJson['id'])) {
83
            throw new MailApiException('Mailgun: ' . $resultJson['message']);
84
        }
85
86 4
        return true;
87
    }
88
89 1
    private function getApiUri()
90
    {
91 1
        $query = $this->uri->getQueryPart('region');
92 1
        if (isset($this->regions[$query])) {
93
            return $this->regions[$query];
94
        }
95
96 1
        return $this->regions['us'];
97
    }
98
99
}
100