Passed
Branch master (487060)
by Wanderson
03:41 queued 01:00
created

Mailer::sendTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Win\Services;
4
5
use Exception;
6
use PHPMailer\PHPMailer\PHPMailer;
7
use Win\Templates\Email;
8
use Win\Common\Server;
9
use Win\Services\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
		return $this;
45
	}
46
47
	/**
48
	 * Define o assunto
49
	 * @param string $subject
50
	 */
51
	public function setSubject($subject)
52
	{
53
		$this->mailer->Subject = $subject;
54
		return $this;
55
	}
56
57
	/**
58
	 * Define o remetente
59
	 * @param string $address
60
	 * @param string $name
61
	 */
62
	public function setFrom($address, $name = '')
63
	{
64
		$this->mailer->From = $address;
65
		$this->mailer->FromName = $name;
66
		return $this;
67
	}
68
69
	/**
70
	 * Add destinatário
71
	 * @param string $address
72
	 * @param string $name
73
	 */
74
	public function addTo($address, $name = '')
75
	{
76
		$this->mailer->addAddress($address, $name);
77
		return $this;
78
	}
79
80
	/**
81
	 * Add cópia
82
	 * @param string $address
83
	 * @param string $name
84
	 */
85
	public function addCC($address, $name = '')
86
	{
87
		$this->mailer->addCC($address, $name);
88
		return $this;
89
	}
90
91
	/**
92
	 * Add cópia oculta
93
	 * @param string $address
94
	 * @param string $name
95
	 */
96
	public function addBCC($address, $name = '')
97
	{
98
		$this->mailer->addBCC($address, $name);
99
		return $this;
100
	}
101
102
	/**
103
	 * Add responder para
104
	 * @param string $address
105
	 * @param string $name
106
	 */
107
	public function addReplyTo($address, $name = '')
108
	{
109
		$this->mailer->addReplyTo($address, $name);
110
		return $this;
111
	}
112
113
	/**
114
	 * Envia o E-mail
115
	 * @param string|Email $body
116
	 */
117
	public function send($body)
118
	{
119
		if ($body instanceof Email) {
120
			$body->mailer = $this;
121
		}
122
		$this->mailer->Body = (string) $body;
123
124
		if (!Server::isLocalHost() || static::$sendOnLocalHost) {
125
			$send = $this->mailer->Send();
126
			$this->flush();
127
128
			if (!$send) {
129
				throw new Exception('Houve um erro ao enviar o e-mail.');
130
			}
131
		} else {
132
			$this->flush();
133
			$this->saveOnDisk();
134
		}
135
	}
136
137
	/**
138
	 * Envia o email como template
139
	 * @param mixed $data
140
	 * @param string $template
141
	 * @param string $layout
142
	 */
143
	public function sendTemplate($data, $template, $layout = 'layout')
144
	{
145
		$template = new Email($template, $data);
146
		$template->mailer = $this;
147
		$layout = new Email($layout, ['content' => $template]);
148
		$this->send($layout);
149
	}
150
151
	/**
152
	 * Limpa dados
153
	 */
154
	private function flush()
155
	{
156
		$this->mailer->ClearAllRecipients();
157
		$this->mailer->ClearAttachments();
158
	}
159
160
	/**
161
	 * Salva o corpo do E-mail em um arquivo
162
	 * @return bool
163
	 */
164
	private function saveOnDisk()
165
	{
166
		$fs = new Filesystem();
167
		$name = date('Y.m.d-H.i.s-') . strtolower(md5(uniqid(time()))) . '.html';
168
		$body = $this->mailer->Body;
169
170
		return $fs->write(static::DIRECTORY . "/$name", $body);
171
	}
172
}
173