Passed
Push — master ( 3bea0d...2ba6bf )
by Daniel
06:18 queued 12s
created

Replyable   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 425
Duplicated Lines 0 %

Importance

Changes 12
Bugs 6 Features 2
Metric Value
wmc 39
eloc 109
c 12
b 6
f 2
dl 0
loc 425
rs 9.28

24 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 5 1
A convertEmailList() 0 11 3
A to() 0 6 1
A optionalParameters() 0 5 1
A bcc() 0 6 1
A priority() 0 5 1
A cc() 0 6 1
A __construct() 0 3 1
A attach() 0 13 3
A from() 0 6 1
A markdown() 0 11 2
A reply() 0 14 2
A message() 0 5 1
A emailList() 0 6 2
A subject() 0 5 1
A setReplyFrom() 0 6 3
A base64_encode() 0 3 1
A setReplyTo() 0 7 2
A setReplyThread() 0 7 2
A setHeader() 0 5 1
A setReplySubject() 0 4 2
A send() 0 7 1
A getMessageBody() 0 21 2
A getMessageIdHeader() 0 8 3
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Dacastro4\LaravelGmail\Services\Message\Mail;
6
use Google_Service_Gmail;
7
use Google_Service_Gmail_Message;
8
use Illuminate\Container\Container;
9
use Illuminate\Mail\Markdown;
10
use Swift_Attachment;
11
use Swift_Message;
12
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
13
14
/**
15
 * @property Google_Service_Gmail $service
16
 */
17
trait Replyable
18
{
19
	use HasHeaders;
20
21
	private $swiftMessage;
22
23
	/**
24
	 * Gmail optional parameters
25
	 *
26
	 * @var array
27
	 */
28
	private $parameters = [];
29
30
	/**
31
	 * Text or html message to send
32
	 *
33
	 * @var string
34
	 */
35
	private $message;
36
37
	/**
38
	 * Subject of the email
39
	 *
40
	 * @var string
41
	 */
42
	private $subject;
43
44
	/**
45
	 * Sender's email
46
	 *
47
	 * @var string
48
	 */
49
	private $from;
50
51
	/**
52
	 * Sender's name
53
	 *
54
	 * @var  string
55
	 */
56
	private $nameFrom;
57
58
	/**
59
	 * Email of the recipient
60
	 *
61
	 * @var string|array
62
	 */
63
	private $to;
64
65
	/**
66
	 * Name of the recipient
67
	 *
68
	 * @var string
69
	 */
70
	private $nameTo;
71
72
	/**
73
	 * Single email or array of email for a carbon copy
74
	 *
75
	 * @var array|string
76
	 */
77
	private $cc;
78
79
	/**
80
	 * Name of the recipient
81
	 *
82
	 * @var string
83
	 */
84
	private $nameCc;
85
86
	/**
87
	 * Single email or array of email for a blind carbon copy
88
	 *
89
	 * @var array|string
90
	 */
91
	private $bcc;
92
93
	/**
94
	 * Name of the recipient
95
	 *
96
	 * @var string
97
	 */
98
	private $nameBcc;
99
100
	/**
101
	 * List of attachments
102
	 *
103
	 * @var array
104
	 */
105
	private $attachments = [];
106
107
	private $priority = 2;
108
109
	public function __construct()
110
	{
111
		$this->swiftMessage = new Swift_Message();
112
	}
113
114
	/**
115
	 * Receives the recipient's
116
	 * If multiple recipients will receive the message an array should be used.
117
	 * Example: array('[email protected]', '[email protected]' => 'A name')
118
	 *
119
	 * If $name is passed and the first parameter is a string, this name will be
120
	 * associated with the address.
121
	 *
122
	 * @param  string|array  $to
123
	 *
124
	 * @param  string|null  $name
125
	 *
126
	 * @return Replyable
127
	 */
128
	public function to($to, $name = null)
129
	{
130
		$this->to = $to;
131
		$this->nameTo = $name;
132
133
		return $this;
134
	}
135
136
	public function from($from, $name = null)
137
	{
138
		$this->from = $from;
139
		$this->nameFrom = $name;
140
141
		return $this;
142
	}
143
144
	/**
145
	 * @param  array|string  $cc
146
	 *
147
	 * @param  string|null  $name
148
	 *
149
	 * @return Replyable
150
	 */
151
	public function cc($cc, $name = null)
152
	{
153
		$this->cc = $this->emailList($cc, $name);
154
		$this->nameCc = $name;
155
156
		return $this;
157
	}
158
159
	private function emailList($list, $name = null)
160
	{
161
		if (is_array($list)) {
162
			return $this->convertEmailList($list, $name);
163
		} else {
164
			return $list;
165
		}
166
	}
167
168
	private function convertEmailList($emails, $name = null)
169
	{
170
		$newList = [];
171
		$count = 0;
172
		foreach ($emails as $key => $email) {
173
			$emailName = isset($name[$count]) ? $name[$count] : explode('@', $email)[0];
174
			$newList[$email] = $emailName;
175
			$count = $count + 1;
176
		}
177
178
		return $newList;
179
	}
180
181
	/**
182
	 * @param  array|string  $bcc
183
	 *
184
	 * @param  string|null  $name
185
	 *
186
	 * @return Replyable
187
	 */
188
	public function bcc($bcc, $name = null)
189
	{
190
		$this->bcc = $this->emailList($bcc, $name);
191
		$this->nameBcc = $name;
192
193
		return $this;
194
	}
195
196
	/**
197
	 * @param  string  $subject
198
	 *
199
	 * @return Replyable
200
	 */
201
	public function subject($subject)
202
	{
203
		$this->subject = $subject;
204
205
		return $this;
206
	}
207
208
	/**
209
	 * @param  string  $view
210
	 * @param  array  $data
211
	 * @param  array  $mergeData
212
	 *
213
	 * @return Replyable
214
	 * @throws \Throwable
215
	 */
216
	public function view($view, $data = [], $mergeData = [])
217
	{
218
		$this->message = view($view, $data, $mergeData)->render();
219
220
		return $this;
221
	}
222
223
    /**
224
     * loads markdown file for message body
225
     *
226
     * @throws \Throwable
227
     * @return Replyable
228
     */
229
    public function markdown(string $markdown_view, array $data = [])
230
    {
231
        $markdown = Container::getInstance()->make(Markdown::class);
232
233
        if (config('mail.markdown.theme')) {
234
            $markdown->theme(config('mail.markdown.theme'));
235
        }
236
237
        $this->message = $markdown->render($markdown_view, $data);
238
239
        return $this;
240
    }
241
242
	/**
243
	 * @param  string  $message
244
	 *
245
	 * @return Replyable
246
	 */
247
	public function message($message)
248
	{
249
		$this->message = $message;
250
251
		return $this;
252
	}
253
254
	/**
255
	 * Attaches new file to the email from the Storage folder
256
	 *
257
	 * @param  array  $files  comma separated of files
258
	 *
259
	 * @return Replyable
260
	 * @throws \Exception
261
	 */
262
	public function attach(...$files)
263
	{
264
265
		foreach ($files as $file) {
266
267
			if (!file_exists($file)) {
268
				throw new FileNotFoundException($file);
269
			}
270
271
			array_push($this->attachments, $file);
272
		}
273
274
		return $this;
275
	}
276
277
	/**
278
	 * The value is an integer where 1 is the highest priority and 5 is the lowest.
279
	 *
280
	 * @param  int  $priority
281
	 *
282
	 * @return Replyable
283
	 */
284
	public function priority($priority)
285
	{
286
		$this->priority = $priority;
287
288
		return $this;
289
	}
290
291
	/**
292
	 * @param  array  $parameters
293
	 *
294
	 * @return Replyable
295
	 */
296
	public function optionalParameters(array $parameters)
297
	{
298
		$this->parameters = $parameters;
299
300
		return $this;
301
	}
302
303
	/**
304
	 * Reply to a specific email
305
	 *
306
	 * @return Mail
307
	 * @throws \Exception
308
	 */
309
	public function reply()
310
	{
311
		if (!$this->getId()) {
312
			throw new \Exception('This is a new email. Use send().');
313
		}
314
315
		$this->setReplyThread();
316
		$this->setReplySubject();
317
		$this->setReplyTo();
318
		$this->setReplyFrom();
319
		$body = $this->getMessageBody();
320
		$body->setThreadId($this->getThreadId());
321
322
		return new Mail($this->service->users_messages->send('me', $body, $this->parameters));
323
	}
324
325
	public abstract function getId();
326
327
	private function setReplyThread()
328
	{
329
		$threadId = $this->getThreadId();
330
		if ($threadId) {
331
			$this->setHeader('In-Reply-To', $this->getMessageIdHeader());
332
			$this->setHeader('References', $this->getHeader('References'));
333
			$this->setHeader('Message-ID', $this->getMessageIdHeader());
334
		}
335
	}
336
	
337
	private function getMessageIdHeader() {
338
		if($this->getHeader('Message-ID')) {
339
			return $this->getHeader('Message-ID');
340
		}
341
		if($this->getHeader('Message-Id')) {
342
			return $this->getHeader('Message-Id');
343
		}
344
		return null;
345
	}
346
347
	public abstract function getThreadId();
348
349
	/**
350
	 * Add a header to the email
351
	 *
352
	 * @param  string  $header
353
	 * @param  string  $value
354
	 */
355
	public function setHeader($header, $value)
356
	{
357
		$headers = $this->swiftMessage->getHeaders();
358
359
		$headers->addTextHeader($header, $value);
360
361
	}
362
363
	private function setReplySubject()
364
	{
365
		if (!$this->subject) {
366
			$this->subject = $this->getSubject();
367
		}
368
	}
369
370
	private function setReplyTo()
371
	{
372
		if (!$this->to) {
373
			$replyTo = $this->getReplyTo();
374
375
			$this->to = $replyTo['email'];
376
			$this->nameTo = $replyTo['name'];
377
		}
378
	}
379
380
	private function setReplyFrom()
381
	{
382
		if (!$this->from) {
383
			$this->from = $this->getUser();
384
			if(!$this->from) {
385
				throw new \Exception('Reply from is not defined');
386
			}
387
		}
388
	}
389
390
	public abstract function getSubject();
391
392
	public abstract function getReplyTo();
393
394
	public abstract function getUser();
395
396
	/**
397
	 * @return Google_Service_Gmail_Message
398
	 */
399
	private function getMessageBody()
400
	{
401
		$body = new Google_Service_Gmail_Message();
402
403
		$this->swiftMessage
404
			->setSubject($this->subject)
405
			->setFrom($this->from, $this->nameFrom)
406
			->setTo($this->to, $this->nameTo)
407
			->setCc($this->cc, $this->nameCc)
408
			->setBcc($this->bcc, $this->nameBcc)
409
			->setBody($this->message, 'text/html')
410
			->setPriority($this->priority);
411
412
		foreach ($this->attachments as $file) {
413
			$this->swiftMessage
414
				->attach(Swift_Attachment::fromPath($file));
415
		}
416
417
		$body->setRaw($this->base64_encode($this->swiftMessage->toString()));
418
419
		return $body;
420
	}
421
422
	private function base64_encode($data)
423
	{
424
		return rtrim(strtr(base64_encode($data), ['+' => '-', '/' => '_']), '=');
425
	}
426
427
	/**
428
	 * Sends a new email
429
	 *
430
	 * @return self|Mail
431
	 */
432
	public function send()
433
	{
434
		$body = $this->getMessageBody();
435
436
		$this->setMessage($this->service->users_messages->send('me', $body, $this->parameters));
437
438
		return $this;
439
	}
440
441
	protected abstract function setMessage(\Google_Service_Gmail_Message $message);
442
}
443