Passed
Branch master (242b35)
by Wanderson
02:31 queued 17s
created

Mailer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

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