Issues (9)

src/Drivers/MailjetDriver.php (2 issues)

1
<?php
2
3
namespace DansMaCulotte\MailTemplate\Drivers;
4
5
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration;
6
use DansMaCulotte\MailTemplate\Exceptions\SendError;
7
use Mailjet\Client;
8
use Mailjet\Resources;
9
10
/**
11
 * Class MailjetDriver
12
 * @package DansMaCulotte\MailTemplate\Drivers
13
 */
14
class MailjetDriver implements Driver
15
{
16
    /** @var Client|null  */
17
    public $client = null;
18
19
    /** @var array */
20
    private $debugEmail = null;
21
22
    public $message = [];
23
24
    /**
25
     * MailjetDriver constructor.
26
     * @param $config
27
     * @throws InvalidConfiguration
28
     */
29
    public function __construct($config)
30
    {
31
        if (!isset($config['key'])) {
32
            throw InvalidConfiguration::invalidCredential('mailjet', 'key');
33
        }
34
35
        if (!isset($config['secret'])) {
36
            throw InvalidConfiguration::invalidCredential('mailjet', 'secret');
37
        }
38
39
        if (isset($config['debug_email'])) {
40
            $this->debugEmail = $config['debug_email'];
41
            if (!(isset($this->debugEmail['Name']) && isset($this->debugEmail['Email']))) {
42
                throw new InvalidConfiguration('debug_email in mailjet configuration must have "Name" and "Email" keys');
43
            }
44
        }
45
46
        $this->client = new Client($config['key'], $config['secret'], true, [
47
            'version' => 'v3.1',
48
        ]);
49
    }
50
51
    /**
52
     * @return Driver
53
     */
54
    public function make(): Driver
55
    {
56
        $this->message = [];
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $name
63
     * @param string $email
64
     * @return Driver
65
     */
66
    public function setFrom(string $name, string $email): Driver
67
    {
68
        $this->message['From']['Name'] = $name;
69
        $this->message['From']['Email'] = $email;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $template
76
     * @return Driver
77
     */
78
    public function setTemplate($template): Driver
79
    {
80
        $this->message['TemplateID'] = (int) $template;
81
        $this->message['TemplateLanguage'] = true;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $subject
88
     * @return Driver
89
     */
90
    public function setSubject(string $subject): Driver
91
    {
92
        $this->message['Subject'] = $subject;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $name
99
     * @param string $email
100
     * @return Driver
101
     */
102
    public function setRecipient(string $name, string $email): Driver
103
    {
104
        $this->message['To'][] = [
105
            'Name' => $name,
106
            'Email' => $email,
107
        ];
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $name
114
     * @param string $email
115
     * @return Driver
116
     */
117
    public function setBcc(string $name, string $email): Driver
118
    {
119
        $this->message['Bcc'][] = [
120
            'Name' => $name,
121
            'Email' => $email,
122
        ];
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param array $variables
129
     * @return Driver
130
     */
131
    public function setVariables(array $variables): Driver
132
    {
133
        foreach ($variables as $variableKey => $variableValue) {
134
            $this->message['Variables'][$variableKey] = $variableValue;
135
        }
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param string $language
142
     * @return Driver
143
     */
144
    public function setLanguage(string $language): Driver
145
    {
146
        $this->message['Variables']['language'] = $language;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return array
153
     * @throws SendError
154
     */
155
    public function send(): array
156
    {
157
        if ($this->debugEmail) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->debugEmail of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
158
            $this->message['TemplateErrorReporting'] = $this->debugEmail;
159
        }
160
161
        $response = $this->client->post(Resources::$Email, [
0 ignored issues
show
The method post() 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

161
        /** @scrutinizer ignore-call */ 
162
        $response = $this->client->post(Resources::$Email, [

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...
162
            'body' => [
163
                'Messages' => [
164
                    $this->message,
165
                ],
166
            ],
167
        ]);
168
169
        if ($response->success() === false) {
170
            throw SendError::responseError('mailjet');
171
        }
172
173
        return $response->getData();
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function toArray(): array
180
    {
181
        return [
182
            'body' => [
183
                'Messages' => [
184
                    $this->message,
185
                ],
186
            ],
187
        ];
188
    }
189
}
190