Completed
Push — master ( d93ca7...b8d88d )
by Gaël
05:19 queued 11s
created

MailgunDriver::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace DansMaCulotte\MailTemplate\Drivers;
4
5
6
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration;
7
use DansMaCulotte\MailTemplate\Exceptions\SendError;
8
use Mailgun\Exception\HttpClientException;
9
use Mailgun\Mailgun;
10
11
class MailgunDriver implements Driver
12
{
13
14
    /** @var Mailgun|null  */
15
    public $client = null;
16
17
    /** @var array */
18
    public $body = [];
19
20
    /** @var array */
21
    public $message = [];
22
23
    /** @var string */
24
    public $domain;
25
26
    /**
27
     * Driver constructor.
28
     * @param array $config
29
     * @throws InvalidConfiguration
30
     */
31
    public function __construct(array $config)
32
    {
33
        if (!isset($config['key'])) {
34
            throw InvalidConfiguration::invalidCredential('mailgun', 'key');
35
        }
36
37
        if (!isset($config['domain'])) {
38
            throw InvalidConfiguration::invalidCredential('mailgun', 'domain');
39
        }
40
41
        $this->client = Mailgun::create($config['key']);
42
        $this->domain = $config['domain'];
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param string $email
48
     * @return Driver
49
     */
50
    public function setFrom(string $name, string $email): Driver
51
    {
52
        $this->message['from'] = "${name} <${email}>";
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $template
59
     * @return Driver
60
     */
61
    public function setTemplate(string $template): Driver
62
    {
63
        $this->message['template'] = $template;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $subject
70
     * @return Driver
71
     */
72
    public function setSubject(string $subject): Driver
73
    {
74
        $this->message['subject'] = $subject;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $name
81
     * @param string $email
82
     * @return Driver
83
     */
84
    public function setRecipient(string $name, string $email): Driver
85
    {
86
        $this->message['to'] = "${name} <${email}>";
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param array $variables
93
     * @return Driver
94
     */
95
    public function setVariables(array $variables): Driver
96
    {
97
        $this->message['h:X-Mailgun-Variables'] = json_encode($variables);
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $language
104
     * @return Driver
105
     */
106
    public function setLanguage(string $language): Driver
107
    {
108
        return $this;
109
    }
110
111
    /**
112
     * @return array
113
     * @throws SendError
114
     */
115
    public function send(): array
116
    {
117
        try {
118
            $response = $this->client->messages()->send(
0 ignored issues
show
Bug introduced by
The method messages() 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

118
            $response = $this->client->/** @scrutinizer ignore-call */ messages()->send(

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...
119
                $this->domain,
120
                $this->message
121
            );
122
123
            return [
124
                'id' => $response->getId(),
125
                'message' => $response->getMessage(),
126
            ];
127
        } catch (HttpClientException $exception) {
128
            throw SendError::responseError('mailgun', 0, $exception);
129
        }
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function toArray(): array
136
    {
137
        return [
138
            'body' => array_merge($this->body, [
139
                'Messages' => [
140
                    $this->message,
141
                ],
142
            ])
143
        ];
144
    }
145
}