Passed
Pull Request — master (#21)
by Wanderson
03:27
created

Mailer::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Services;
4
5
use Exception;
6
use PHPMailer\PHPMailer\PHPMailer;
7
use Win\Common\Email;
8
use Win\Common\Server;
9
use Win\Repositories\Filesystem;
10
11
/**
12
 * Envio de Emails
13
 *
14
 * Responsável por enviar Emails
15
 */
16
class Mailer
17
{
18
	const DIRECTORY = 'data/emails';
19
20
	/** @var PHPMailer */
21
	private $mailer;
22
23
	/** @var bool */
24
	public static $sendOnLocalHost = false;
25
26
	/**
27
	 * Instancia o serviço de E-mail
28
	 */
29
	public function __construct()
30
	{
31
		$this->mailer = new PHPMailer();
32
		$this->mailer->CharSet = 'utf-8';
33
		$this->mailer->IsMail();
34
		$this->mailer->IsHTML(true);
35
	}
36
37
	/**
38
	 * Define o idioma
39
	 * @param string $language
40
	 */
41
	public function setLanguage($language)
42
	{
43
		$this->mailer->SetLanguage($language);
44
	}
45
46
	/**
47
	 * Define o assunto
48
	 * @param string $subject
49
	 */
50
	public function setSubject($subject)
51
	{
52
		$this->mailer->Subject = $subject;
53
	}
54
55
	/**
56
	 * Define o remetente
57
	 * @param string $address
58
	 * @param string $name
59
	 */
60
	public function setFrom($address, $name = '')
61
	{
62
		$this->mailer->From = $address;
63
		$this->mailer->FromName = $name;
64
	}
65
66
	/**
67
	 * Add destinatário
68
	 * @param string $address
69
	 * @param string $name
70
	 */
71
	public function addTo($address, $name = '')
72
	{
73
		$this->mailer->addAddress($address, $name);
74
	}
75
76
	/**
77
	 * Add cópia
78
	 * @param string $address
79
	 * @param string $name
80
	 */
81
	public function addCC($address, $name = '')
82
	{
83
		$this->mailer->addCC($address, $name);
84
	}
85
86
	/**
87
	 * Add cópia oculta
88
	 * @param string $address
89
	 * @param string $name
90
	 */
91
	public function addBCC($address, $name = '')
92
	{
93
		$this->mailer->addBCC($address, $name);
94
	}
95
96
	/**
97
	 * Add responder para
98
	 * @param string $address
99
	 * @param string $name
100
	 */
101
	public function addReplyTo($address, $name = '')
102
	{
103
		$this->mailer->addReplyTo($address, $name);
104
	}
105
106
	/**
107
	 * Envia o E-mail
108
	 * @param string|Email $body
109
	 */
110
	public function send($body)
111
	{
112
		$this->mailer->Body = (string) $body;
113
114
		if (!Server::isLocalHost() || static::$sendOnLocalHost) {
115
			$send = $this->mailer->Send();
116
			$this->flush();
117
118
			if (!$send) {
119
				throw new Exception('Houve um erro ao enviar o e-mail.');
120
			}
121
		} else {
122
			$this->saveOnDisk();
123
		}
124
	}
125
126
	/**
127
	 * Limpa dados
128
	 */
129
	private function flush()
130
	{
131
		$this->mailer->ClearAllRecipients();
132
		$this->mailer->ClearAttachments();
133
	}
134
135
	/**
136
	 * Salva o corpo do E-mail em um arquivo
137
	 * @return bool
138
	 */
139
	private function saveOnDisk()
140
	{
141
		$fs = new Filesystem();
142
		$name = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html';
143
		$body = $this->mailer->Body;
144
145
		return $fs->write(static::DIRECTORY . "/$name", $body);
146
	}
147
}
148