Passed
Pull Request — master (#15)
by Joao
07:05
created

MailgunApiWrapper::send()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8.021

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 27
cts 29
cp 0.931
rs 7.8246
c 0
b 0
f 0
cc 8
nc 96
nop 1
crap 8.021

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helper\RequestMultiPart;
10
use ByJG\Util\HttpClient;
11
use ByJG\Util\MultiPartItem;
12
use ByJG\Util\Psr7\Request;
13
use ByJG\Util\Uri;
14
15
class MailgunApiWrapper extends PHPMailerWrapper
16
{
17
    private $client;
18
19
    private $regions = [
20
        'us' => 'api.mailgun.net',
21
        'eu' => 'api.eu.mailgun.net',
22
    ];
23
24 5
    public function __construct(Uri $uri, HttpClient $client = null)
25
    {
26 5
        parent::__construct($uri);
27
28 5
        $this->client = $client;
29 5
        if (is_null($client)) {
30 1
            $this->client = new HttpClient();
31
        }
32 5
    }
33
34
    /**
35
     * @return Request
36
     * @throws \ByJG\Util\Psr7\MessageException
37
     */
38 5
    public function getRequestObject()
39
    {
40 5
        $domainName = $this->uri->getHost();
41 5
        $apiUri = $this->getApiUri();
42
43 5
        $uri = Uri::getInstanceFromString("https://$apiUri/v3/$domainName/messages")
44 5
            ->withUserInfo('api', $this->uri->getUsername());
45
46 5
        return Request::getInstance($uri)->withMethod("POST");
47
    }
48
49
    /**
50
     * malgun://api:APIKEY@DOMAINNAME
51
     *
52
     * @param Envelope $envelope
53
     * @return bool
54
     * @throws MailApiException
55
     * @throws InvalidEMailException
56
     * @throws CurlException
57
     * @throws \ByJG\Util\Psr7\MessageException
58
     */
59 4
    public function send(Envelope $envelope)
60
    {
61 4
        $this->validate($envelope);
62
63
        $message = [
64 4
            new MultiPartItem('from', $envelope->getFrom()),
65 4
            new MultiPartItem('subject', $envelope->getSubject()),
66 4
            new MultiPartItem('html', $envelope->getBody()),
67 4
            new MultiPartItem('text', $envelope->getBodyText()),
68
        ];
69
70
71 4
        foreach ((array)$envelope->getTo() as $to) {
72 4
            $message[] = new MultiPartItem('to', $to);
73
        }
74
75 4
        foreach ((array)$envelope->getBCC() as $bcc) {
76 3
            $message[] = new MultiPartItem('bcc', $bcc);
77
        }
78
79 4
        if (!empty($envelope->getReplyTo())) {
80 4
            $message[] = new MultiPartItem('h:Reply-To', $envelope->getReplyTo());
81
        }
82
83 4
        foreach ((array)$envelope->getCC() as $cc) {
84 3
            $message[] = new MultiPartItem('cc', $cc);
85
        }
86
87 4
        foreach ((array)$envelope->getAttachments() as $name => $attachment) {
88 2
            $message[] = new MultiPartItem(
89 2
                $attachment['disposition'],
90 2
                file_get_contents($attachment['content']),
91 2
                $name,
92 2
                $attachment['content-type']
93
            );
94
        }
95
96 4
        $request = $this->getRequestObject();
97 4
        RequestMultiPart::buildMultiPart($message, $request);
98
99 4
        $result = $this->client->sendRequest($request);
100 4
        if ($result->getStatusCode() != 200) {
101
            throw new MailApiException("Mailgun result code is " . $result->getStatusCode());
102
        }
103 4
        $resultJson = json_decode($result->getBody()->getContents(), true);
104 4
        if (!isset($resultJson['id'])) {
105
            throw new MailApiException('Mailgun: ' . $resultJson['message']);
106
        }
107
108 4
        return true;
109
    }
110
111 5
    private function getApiUri()
112
    {
113 5
        $query = $this->uri->getQueryPart('region');
114 5
        if (isset($this->regions[$query])) {
115
            return $this->regions[$query];
116
        }
117
118 5
        return $this->regions['us'];
119
    }
120
121
}
122