Completed
Pull Request — master (#6)
by
unknown
03:48 queued 10s
created

Email::userVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Slides\Connector\Auth\Clients\Mandrill\Builders;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Slides\Connector\Auth\Clients\Mandrill\Mailer;
8
use Slides\Connector\Auth\Clients\Mandrill\VariableResolver;
9
10
/**
11
 * Class Email
12
 *
13
 * @package Slides\Connector\Auth\Clients\Mandrill\Builders
14
 */
15
class Email
16
{
17
    /**
18
     * @var Mailer
19
     */
20
    protected $mailer;
21
22
    /**
23
     * The variable resolver.
24
     *
25
     * @var string
26
     */
27
    protected $resolver;
28
29
    /**
30
     * @var VariableResolver
31
     */
32
    protected $resolverInstance;
33
34
    /**
35
     * The additional attributes that should be added to email.
36
     *
37
     * @var array
38
     */
39
    protected $attributes = [];
40
41
    /**
42
     * @var string
43
     */
44
    protected $template;
45
46
    /**
47
     * @var array
48
     */
49
    protected $variables;
50
51
    /**
52
     * @var Collection
53
     */
54
    protected $recipients;
55
56
    /**
57
     * Email constructor.
58
     *
59
     * @param Mailer $mailer
60
     * @param string $resolver
61
     */
62
    public function __construct(Mailer $mailer, string $resolver = null)
63
    {
64
        $this->mailer = $mailer;
65
        $this->recipients = new Collection();
66
67
        $this->resolver = $resolver ?: config('connector.credentials.clients.mandrill.resolver');
68
    }
69
70
    /**
71
     * Retrieve recipients.
72
     *
73
     * @return Collection
74
     */
75
    public function getRecipients(): Collection
76
    {
77
        return $this->recipients;
78
    }
79
80
    /**
81
     * Set the email template.
82
     *
83
     * @param string $template
84
     *
85
     * @return $this
86
     */
87
    public function template(string $template)
88
    {
89
        $this->template = $template;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set additional variables.
96
     *
97
     * @param array $variables
98
     *
99
     * @return $this
100
     */
101
    public function variables(array $variables)
102
    {
103
        $this->variables = $variables;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Set the message subject.
110
     *
111
     * @param string $subject
112
     *
113
     * @return static
114
     */
115
    public function subject(string $subject)
116
    {
117
        $this->attributes['subject'] = $subject;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Set the sender email address and name.
124
     *
125
     * @param string $email
126
     * @param string $name
127
     *
128
     * @return static
129
     */
130
    public function from(string $email, ?string $name = null)
131
    {
132
        $this->attributes['from'] = compact('email', 'name');
133
134
        return $this;
135
    }
136
137
    /**
138
     * Set the email recipients.
139
     *
140
     * @param mixed $recipients
141
     *
142
     * @return static
143
     */
144
    public function recipients($recipients)
145
    {
146
        $this->recipients = collect($recipients);
147
148
        return $this;
149
    }
150
151
    /**
152
     * Chunk the recipients.
153
     *
154
     * @param int $size
155
     *
156
     * @return array
157
     */
158
    public function chunk(int $size = 1000): array
159
    {
160
        $chunks = $this->recipients->chunk($size);
161
162
        return $chunks->map([$this, 'build'])->toArray();
163
    }
164
165
    /**
166
     * Send the email.
167
     *
168
     * @param Collection $recipients
169
     *
170
     * @return mixed
171
     */
172
    public function send(Collection $recipients = null)
173
    {
174
        if (is_null($recipients)) {
175
            $recipients = $this->recipients;
176
        }
177
178
        return $this->mailer->send($this->build($recipients));
179
    }
180
181
    /**
182
     * Chunk recipients before sending.
183
     *
184
     * @param int $size
185
     *
186
     * @return \Generator
187
     */
188
    public function sendChunk(int $size = 1000)
189
    {
190
        foreach ($this->recipients->chunk($size) as $chunk) {
191
            yield $this->send($chunk);
192
        }
193
    }
194
195
    /**
196
     * Build the email to be sent.
197
     *
198
     * @param Collection|null $recipients
199
     *
200
     * @return array
201
     */
202
    protected function build(Collection $recipients): array
203
    {
204
        return array_merge([
205
            'template' => $this->template,
206
            'recipients' => $this->buildRecipients($recipients),
207
            'variables' => $this->buildVariables($recipients),
208
        ], $this->attributes);
209
    }
210
211
    /**
212
     * Build the message recipients.
213
     *
214
     * @param Collection $recipients
215
     *
216
     * @return array
217
     */
218
    protected function buildRecipients(Collection $recipients): array
219
    {
220
        return $recipients->map(function (string $email) {
221
            return [
222
                'email' => $email,
223
                'type' => 'to'
224
            ];
225
        })->toArray();
226
    }
227
228
    /**
229
     * Build the variables for given collection of recipients.
230
     *
231
     * @param Collection $recipients
232
     *
233
     * @return array
234
     */
235
    protected function buildVariables(Collection $recipients): array
236
    {
237
        return $recipients->map(function (string $email) {
238
            return [
239
                'rcpt' => $email,
240
                'vars' => $this->userVariables($email)
241
            ];
242
        })->toArray();
243
    }
244
245
    /**
246
     * Build variable for a user.
247
     *
248
     * @param string $email
249
     *
250
     * @return array
251
     *
252
     * @throws Exception
253
     */
254
    protected function userVariables(string $email): array
255
    {
256
        if (!$this->resolverInstance) {
257
            $this->resolverInstance = new $this->resolver($this->recipients);
258
        }
259
260
        return array_map(function ($variable) use ($email) {
261
            return $this->resolverInstance->resolve($variable, $email);
262
        }, $this->variables);
263
    }
264
}