MandrillDriver::setSubject()   A
last analyzed

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 1
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 Mandrill;
8
use Mandrill_Error;
9
10
/**
11
 * Class MandrillDriver
12
 * @package DansMaCulotte\MailTemplate\Drivers
13
 */
14
class MandrillDriver implements Driver
15
{
16
    /** @var Mandrill|null  */
17
    public $client = null;
18
19
    /** @var array */
20
    public $body = [];
21
22
    /** @var array */
23
    public $message = [];
24
25
    /**
26
     * MandrillDriver constructor.
27
     * @param $config
28
     * @throws InvalidConfiguration
29
     * @throws Mandrill_Error
30
     */
31
    public function __construct($config)
32
    {
33
        if (!isset($config['secret'])) {
34
            throw InvalidConfiguration::invalidCredential('mandrill', 'secret');
35
        }
36
37
        $this->client = new Mandrill($config['secret']);
38
    }
39
40
    /**
41
     * @return Driver
42
     */
43
    public function make(): Driver
44
    {
45
        $this->body = [];
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'] = $name;
59
        $this->message['from_email'] = $email;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $template
66
     * @return Driver
67
     */
68
    public function setTemplate($template): Driver
69
    {
70
        $this->body['template'] = $template;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $subject
77
     * @return Driver
78
     */
79
    public function setSubject(string $subject): Driver
80
    {
81
        $this->message['subject'] = $subject;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param string $email
89
     * @return Driver
90
     */
91
    public function setRecipient(string $name, string $email): Driver
92
    {
93
        $this->message['to'][] = [
94
            'name' => $name,
95
            'email' => $email,
96
            'type' => 'to',
97
        ];
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $name
104
     * @param string $email
105
     * @return Driver
106
     */
107
    public function setBcc(string $name, string $email): Driver
108
    {
109
        $this->message['bcc'][] = [
110
            'name' => $name,
111
            'email' => $email,
112
            'type' => 'bcc',
113
        ];
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param array $variables
120
     * @return Driver
121
     */
122
    public function setVariables(array $variables): Driver
123
    {
124
        foreach ($variables as $variableKey => $variableValue) {
125
            $this->message['global_merge_vars'][] = [
126
                'name' => strtoupper($variableKey),
127
                'content' => $variableValue,
128
            ];
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param string $language
136
     * @return Driver
137
     */
138
    public function setLanguage(string $language): Driver
139
    {
140
        $this->message['global_merge_vars'][] = [
141
            'name' => 'MC_LANGUAGE',
142
            'content' => $language,
143
        ];
144
145
        return $this;
146
    }
147
148
149
    /**
150
     * @return array
151
     * @throws SendError
152
     */
153
    public function send(): array
154
    {
155
        $response = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
156
157
        try {
158
            $response = $this->client->messages->sendTemplate(
159
                $this->body['template'],
160
                [],
161
                $this->message
162
            );
163
        } catch (Mandrill_Error $exception) {
164
            throw SendError::responseError('mandrill', 0, $exception);
165
        }
166
167
        return $response;
168
    }
169
170
    public function toArray(): array
171
    {
172
        return [
173
            'body' => [
174
                'message' => $this->message,
175
            ],
176
        ];
177
    }
178
}
179