Issues (3)

src/Wrapper/MailgunApiWrapper.php (2 issues)

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;
0 ignored issues
show
The type ByJG\Util\CurlException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 1
    public static function schema()
25
    {
26 1
        return ['mailgun'];
27
    }
28
29 5
    public function __construct(Uri $uri, HttpClient $client = null)
30
    {
31 5
        parent::__construct($uri);
32
33 5
        $this->client = $client;
34 5
        if (is_null($client)) {
35 1
            $this->client = new HttpClient();
36
        }
37
    }
38
39
    /**
40
     * @return Request
41
     * @throws \ByJG\Util\Psr7\MessageException
42
     */
43 5
    public function getRequestObject()
44
    {
45 5
        $domainName = $this->uri->getHost();
46 5
        $apiUri = $this->getApiUri();
47
48 5
        $uri = Uri::getInstanceFromString("https://$apiUri/v3/$domainName/messages")
49 5
            ->withUserInfo('api', $this->uri->getUsername());
50
51 5
        return Request::getInstance($uri)->withMethod("POST");
52
    }
53
54
    /**
55
     * malgun://api:APIKEY@DOMAINNAME
56
     *
57
     * @param Envelope $envelope
58
     * @return bool
59
     * @throws MailApiException
60
     * @throws InvalidEMailException
61
     * @throws CurlException
62
     * @throws \ByJG\Util\Psr7\MessageException
63
     */
64 4
    public function send(Envelope $envelope)
65
    {
66 4
        $this->validate($envelope);
67
68 4
        $message = [
69 4
            new MultiPartItem('from', $envelope->getFrom()),
70 4
            new MultiPartItem('subject', $envelope->getSubject()),
71 4
            new MultiPartItem('html', $envelope->getBody()),
72 4
            new MultiPartItem('text', $envelope->getBodyText()),
73 4
        ];
74
75
76 4
        foreach ((array)$envelope->getTo() as $to) {
77 4
            $message[] = new MultiPartItem('to', $to);
78
        }
79
80 4
        foreach ((array)$envelope->getBCC() as $bcc) {
81 3
            $message[] = new MultiPartItem('bcc', $bcc);
82
        }
83
84 4
        if (!empty($envelope->getReplyTo())) {
85 4
            $message[] = new MultiPartItem('h:Reply-To', $envelope->getReplyTo());
86
        }
87
88 4
        foreach ((array)$envelope->getCC() as $cc) {
89 3
            $message[] = new MultiPartItem('cc', $cc);
90
        }
91
92 4
        foreach ((array)$envelope->getAttachments() as $name => $attachment) {
93 2
            $message[] = new MultiPartItem(
94 2
                $attachment['disposition'],
95 2
                file_get_contents($attachment['content']),
96 2
                $name,
97 2
                $attachment['content-type']
98 2
            );
99
        }
100
101 4
        $request = RequestMultiPart::buildMultiPart($message, $this->getRequestObject());
102
103 4
        $result = $this->client->sendRequest($request);
0 ignored issues
show
The method sendRequest() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        /** @scrutinizer ignore-call */ 
104
        $result = $this->client->sendRequest($request);

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...
104 4
        if ($result->getStatusCode() != 200) {
105
            throw new MailApiException("Mailgun result code is " . $result->getStatusCode());
106
        }
107 4
        $resultJson = json_decode($result->getBody()->getContents(), true);
108 4
        if (!isset($resultJson['id'])) {
109
            throw new MailApiException('Mailgun: ' . $resultJson['message']);
110
        }
111
112 4
        return true;
113
    }
114
115 5
    private function getApiUri()
116
    {
117 5
        $query = $this->uri->getQueryPart('region');
118 5
        if (isset($this->regions[$query])) {
119
            return $this->regions[$query];
120
        }
121
122 5
        return $this->regions['us'];
123
    }
124
125
}
126