Completed
Push — master ( dd5c07...b4647f )
by Wanderson
02:12
created

Email::saveOnDisk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Mailer;
4
5
use PHPMailer;
6
use Win\File\Directory;
7
use Win\File\File;
8
use Win\Mvc\Application;
9
use Win\Mvc\Block;
10
use const BASE_PATH;
11
12
/**
13
 * Envios de E-mails
14
 *
15
 * Responsável por enviar emails, simplificando a forma de envio
16
 */
17
class Email {
18
19
	/** @var Block */
20
	private $layout;
21
22
	/** @var Block|string */
23
	private $content;
24
25
	/** @var object Classe responsável pelo envio real */
26
	private $mailer;
27
28
	/**
29
	 * Cria uma mensagem de E-mail
30
	 */
31
	public function __construct() {
32
		$this->setLayout('main');
33
34
		spl_autoload_register('\Win\Mailer\Email::autoload');
35
36
		$this->mailer = new PHPMailer();
37
		$this->mailer->CharSet = 'utf-8';
38
		$this->mailer->SetLanguage('br');
39
		$this->mailer->IsMail();
40
		$this->mailer->IsHTML(true);
41
	}
42
43
	/**
44
	 * Inclui bibliotecas necessarias
45
	 * @param string $className
46
	 */
47
	public static function autoload($className) {
48
		$file = BASE_PATH . '/lib/vendor/phpmailer/class.' . strtolower($className) . '.php';
49
		if (file_exists($file)):
50
			return require $file;
51
		endif;
52
	}
53
54
	/**
55
	 * Adiciona um Destinatário
56
	 * @param string $address E-mail destinatário
57
	 * @param string $name Nome destinatário
58
	 */
59
	public function addAddress($address, $name = '') {
60
		$this->mailer->AddAddress($address, $name);
61
	}
62
63
	/**
64
	 * Define o Remetente
65
	 * @param string $address E-mail remetente
66
	 * @param string $name Nome remetente
67
	 */
68
	public function setFrom($address, $name = '') {
69
		$this->mailer->SetFrom($address, $name);
70
	}
71
72
	/**
73
	 * Define qual será o arquivo de layout
74
	 *
75
	 * @param string $layout Nome do arquivo de layout
76
	 */
77
	public function setLayout($layout) {
78
		$file = 'email/' . $layout;
79
		$this->layout = new Block($file, ['email' => $this]);
80
	}
81
82
	/**
83
	 * Define o conteudo do E-mail
84
	 * que pode ser uma string ou um bloco
85
	 * @param string|Block $content
86
	 */
87
	public function setContent($content) {
88
		$this->content = $content;
89
	}
90
91
	/**
92
	 * Define o Assunto
93
	 * @param string $subject
94
	 */
95
	public function setSubject($subject) {
96
		$this->mailer->Subject = $subject;
97
	}
98
99
	/**
100
	 * Define o idioma
101
	 * @param string $lang
102
	 */
103
	public function setLanguage($lang) {
104
		$this->mailer->SetLanguage($lang);
105
	}
106
107
	/**
108
	 * Retorna o E-mail do Destinatário
109
	 * @return string
110
	 */
111
	public function getFrom() {
112
		return $this->mailer->From;
113
	}
114
115
	/**
116
	 * Retorna o Nome do destinatário
117
	 * @return string
118
	 */
119
	public function getFromName() {
120
		return $this->mailer->FromName;
121
	}
122
123
	/**
124
	 * Retorna o conteudo do E-mail
125
	 * @return string
126
	 */
127
	public function getContent() {
128
		return $this->content;
129
	}
130
131
	/**
132
	 * Retorna o Assunto
133
	 * @return string
134
	 */
135
	public function getSubject() {
136
		return $this->mailer->Subject;
137
	}
138
139
	/**
140
	 * Envia o email
141
	 *
142
	 * No localhost será mostrado o conteudo do E-mail
143
	 * @return null|string Retorna null ou string de erro
144
	 */
145
	public function send() {
146
		if (!Application::app()->isLocalHost()) {
147
			$this->mailer->Body = $this->layout->toString();
148
			$send = $this->mailer->Send();
149
			$this->mailer->ClearAllRecipients();
150
			$this->mailer->ClearAttachments();
151
			if (!$send) {
152
				return 'Houve um erro ao enviar o e-mail.<br /><span style="display:none">' . $this->mailer->ErrorInfo . '</span>';
153
			}
154
			return null;
155
		} else {
156
			$this->saveOnDisk();
157
		}
158
		return null;
159
	}
160
161
	/**
162
	 * Salva o email em um arquivo
163
	 */
164
	private function saveOnDisk() {
165
		$file = new File();
166
		$file->setDirectory('data/email');
167
168
		$fileName = date('Y-m-d H:i:s') . ' ' . strtolower(md5(uniqid(time()))) . '.html';
169
		$file->setName($fileName);
170
		$file->write($this->layout->toString());
171
	}
172
173
}
174