Completed
Push — master ( 845351...636e27 )
by Gaël
13:17
created

MailgunDriver::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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