Mailgun   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 83
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B send() 0 24 1
A getRecipients() 0 11 3
1
<?php namespace nyx\notify\transports\mail\drivers;
2
3
// Internal dependencies
4
use nyx\notify\transports\mail;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, nyx\notify\transports\mail\drivers\mail.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
/**
7
 * Mailgun Mail Driver
8
 *
9
 * Mailgun specific functions (like tagging or campaigns) can be used by passing the respective X-MAILGUN-* headers
10
 * along with the message, as described here: https://documentation.mailgun.com/user_manual.html#sending-via-smtp
11
 *
12
 * Example:
13
 *  $headers = $message->getHeaders();
14
 *  $headers->addTextHeader('X-Mailgun-Variables', '{"msg_id": "nyx123", "my_campaign_id": 8}');
15
 *  $headers->addTextHeader('X-Mailgun-Tag', 'first.tag');
16
 *  $headers->addTextHeader('X-Mailgun-Tag', 'second.tag');
17
 *
18
 * @package     Nyx\Notify
19
 * @version     0.1.0
20
 * @author      Michal Chojnacki <[email protected]>
21
 * @copyright   2012-2017 Nyx Dev Team
22
 * @link        https://github.com/unyx/nyx
23
 */
24
class Mailgun implements mail\interfaces\Driver
25
{
26
    /**
27
     * The traits of a Mailgun Mail Driver instance.
28
     */
29
    use traits\ExposesBcc;
30
31
    /**
32
     * @var string  The API key used to authorize requests to Mailgun's API.
33
     */
34
    protected $key;
35
36
    /**
37
     * @var string  The API endpoint used by the Driver.
38
     */
39
    protected $endpoint;
40
41
    /**
42
     * @var \GuzzleHttp\ClientInterface The underlying HTTP Client instance.
43
     */
44
    protected $client;
45
46
    /**
47
     * Creates a new Mailgun Mail Driver instance.
48
     *
49
     * @param   \GuzzleHttp\ClientInterface $client     The HTTP Client to use.
50
     * @param   string                      $key        The API key to be used to authorize requests to Mailgun's API.
51
     * @param   string                      $domain     The domain to use to send mails from (must be registered with Mailgun).
52
     */
53
    public function __construct(\GuzzleHttp\ClientInterface $client, string $key, string $domain)
54
    {
55
        $this->key      = $key;
56
        $this->endpoint = 'https://api.mailgun.net/v3/'.$domain.'/messages.mime';
57
        $this->client   = $client;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function send(\Swift_Mime_Message $message, &$failures = null)
64
    {
65
        $recipients = $this->getRecipients($message);
66
67
        // Unset the BCC recipients temporarily since we don't want to pass them along with the MIME.
68
        $this->hideBcc($message);
69
70
        try {
71
            $this->client->request('POST', $this->endpoint, [
72
                'auth' => [
73
                    'api', $this->key
74
                ],
75
                'multipart' => [
76
                    ['name' => 'to',      'contents' => implode(',', $recipients)],
77
                    ['name' => 'message', 'contents' => $message->toString(), 'filename' => 'message.mime'],
78
                ]
79
            ]);
80
        } finally {
81
            // Always restore the BCC recipients.
82
            $this->restoreBcc($message);
83
        }
84
85
        return count($recipients);
86
    }
87
88
    /**
89
     * Returns an array of all of the recipients of the message (to, cc and bcc) in a format understood by
90
     * Mailgun's "messages.mime" endpoint for the "to" field.
91
     *
92
     * @param   \Swift_Mime_Message $message
93
     * @return  array
94
     */
95
    protected function getRecipients(\Swift_Mime_Message $message) : array
96
    {
97
        $recipients = (array) $message->getTo() + (array) $message->getCc() + (array) $message->getBcc();
98
        $result     = [];
99
100
        foreach ($recipients as $address => $name) {
101
            $result[] = $name ? $name." <{$address}>" : $address;
102
        }
103
104
        return $result;
105
    }
106
}
107