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

MailgunApiWrapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 70
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequestObject() 0 8 1
C send() 0 46 7
1
<?php
2
3
namespace ByJG\Mail\Wrapper;
4
5
use ByJG\Mail\Envelope;
6
use ByJG\Mail\Exception\MailApiException;
7
use ByJG\Util\MultiPartItem;
8
use ByJG\Util\WebRequest;
9
10
class MailgunApiWrapper extends PHPMailerWrapper
11
{
12
    /**
13
     * @return \ByJG\Util\WebRequest
14
     */
15 1
    public function getRequestObject()
16
    {
17 1
        $domainName = $this->uri->getHost();
18 1
        $request = new WebRequest("https://api.mailgun.net/v3/$domainName/messages");
19 1
        $request->setCredentials('api', $this->uri->getUsername());
20
21 1
        return $request;
22
    }
23
24
    /**
25
     * malgun://api:APIKEY@DOMAINNAME
26
     *
27
     * @param Envelope $envelope
28
     * @return bool
29
     * @throws \ByJG\Mail\Exception\MailApiException
30
     * @throws \ByJG\Util\CurlException
31
     * @throws \Exception
32
     */
33 4
    public function send(Envelope $envelope)
34
    {
35 4
        $this->validate($envelope);
36
37
        $message = [
38 4
            new MultiPartItem('from', $envelope->getFrom()),
39 4
            new MultiPartItem('subject', $envelope->getSubject()),
40 4
            new MultiPartItem('html', $envelope->getBody()),
41 4
            new MultiPartItem('text', $envelope->getBodyText()),
42
        ];
43
44
45 4
        foreach ((array)$envelope->getTo() as $to) {
46 4
            $message[] = new MultiPartItem('to', $to);
47
        }
48
49 4
        foreach ((array)$envelope->getBCC() as $bcc) {
50 3
            $message[] = new MultiPartItem('bcc', $bcc);
51
        }
52
53 4
        if (!empty($envelope->getReplyTo())) {
54 4
            $message[] = new MultiPartItem('h:Reply-To', $envelope->getReplyTo());
55
        }
56
57 4
        foreach ((array)$envelope->getCC() as $cc) {
58 3
            $message[] = new MultiPartItem('cc', $cc);
59
        }
60
61 4
        foreach ((array)$envelope->getAttachments() as $name => $attachment) {
62 2
            $message[] = new MultiPartItem(
63 2
                $attachment['disposition'],
64 2
                file_get_contents($attachment['content']),
65 2
                $name,
66 2
                $attachment['content-type']
67
            );
68
        }
69
70 4
        $request = $this->getRequestObject();
71 4
        $result = $request->postMultiPartForm($message);
72 4
        $resultJson = json_decode($result, true);
73 4
        if (!isset($resultJson['id'])) {
74
            throw new MailApiException('Mailgun: ' . $resultJson['message']);
75
        }
76
77 4
        return true;
78
    }
79
}
80