-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailNotify.php
More file actions
239 lines (195 loc) · 6.71 KB
/
Copy pathEmailNotify.php
File metadata and controls
239 lines (195 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Exception\Exception;
use PHPCensor\Common\Plugin\Plugin;
use PHPCensor\Common\Email\Email;
use PHPCensor\Common\Email\EmailSenderInterface;
use PHPCensor\Common\View\ViewFactoryInterface;
use PHPCensor\Common\View\ViewInterface;
/**
* EmailNotify Plugin - Provides simple email capability.
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Steve Brazier <meadsteve@gmail.com>
*/
class EmailNotify extends Plugin
{
private ViewFactoryInterface $viewFactory;
private bool $committer = false;
private array $addresses = [];
private string $defaultMailtoAddress = '';
private array $cc = [];
private string $template = '';
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'email_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$addresses = $this->getEmailAddresses();
// Without some email addresses in the yml file then we can't do anything.
if (\count($addresses) === 0) {
return false;
}
$buildStatus = $this->build->isSuccessful() ? "Passing Build" : "Failing Build";
$projectName = $this->project->getTitle();
try {
$view = $this->getMailTemplate();
} catch (Exception) {
$this->buildLogger->logWarning(
\sprintf('Unknown mail template "%s", falling back to default.', $this->options->get('template'))
);
$view = $this->getDefaultMailTemplate();
}
$layout = $this->viewFactory->createView('EmailNotify/layout');
$layout->setVariables([
'build' => $this->build,
'project' => $this->project,
'content' => $view->render(),
]);
$body = $layout->render();
$layout->setVariables([
'body' => $body,
]);
$sendFailures = $this->sendSeparateEmails(
$addresses,
\sprintf("PHP Censor - %s - %s", $projectName, $buildStatus),
$body
);
// This is a success if we've not failed to send anything.
$this->buildLogger->logNormal(\sprintf('%d emails sent.', (\count($addresses) - $sendFailures)));
$this->buildLogger->logNormal(\sprintf('%d emails failed to send.', $sendFailures));
return ($sendFailures === 0);
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
$this->viewFactory = $this->container->get(ViewFactoryInterface::class);
$this->committer = (bool)$this->options->get('committer', $this->committer);
$this->defaultMailtoAddress = (string)$this->options->get('default_mailto_address', $this->defaultMailtoAddress);
$this->template = (string)$this->options->get('template', $this->template);
$this->addresses = (array)$this->options->get('addresses', $this->addresses);
$this->cc = (array)$this->options->get('cc', $this->cc);
}
/**
* @param string $toAddress Single address to send to
* @param string[] $ccList
* @param string $subject EmailNotify subject
* @param string $body EmailNotify body
*/
private function sendEmail(string $toAddress, array $ccList, string $subject, string $body): bool
{
$email = new Email();
$email->addEmailTo($toAddress);
$email->setSubject($subject);
$email->setBody($body);
$email->setIsHtml(true);
if ($ccList) {
foreach ($ccList as $address) {
$email->addCarbonCopyEmail($address);
}
}
/** @var EmailSenderInterface $sender */
$sender = $this->container->get(EmailSenderInterface::class);
return $sender->send($email);
}
/**
* Send an email to a list of specified subjects.
*
* @param array $toAddresses List of destination addresses for message.
* @param string $subject Mail subject
* @param string $body Mail body
*
* @return int number of failed messages
*/
private function sendSeparateEmails(array $toAddresses, string $subject, string $body): int
{
$failures = 0;
foreach ($toAddresses as $address) {
if (!$this->sendEmail($address, $this->cc, $subject, $body)) {
$failures++;
}
}
return $failures;
}
private function getEmailAddresses(): array
{
$addresses = [];
$committer = $this->build->getCommitterEmail();
$this->buildLogger->logDebug(\sprintf("Committer email: '%s'", $committer));
$this->buildLogger->logDebug(\sprintf("Committer option: '%s'", $this->committer ? 'true' : 'false'));
if ($this->committer && $committer) {
$addresses[] = $committer;
}
$this->buildLogger->logDebug(
\sprintf(
"Addresses option: '%s'",
($this->addresses ? \implode(', ', $this->addresses) : 'false')
)
);
if ($this->addresses) {
foreach ($this->addresses as $address) {
$addresses[] = $address;
}
}
$this->buildLogger->logDebug(\sprintf(
"Default mailTo option: '%s'",
$this->defaultMailtoAddress ?: 'false'
));
if (empty($addresses) && $this->defaultMailtoAddress) {
$addresses[] = $this->defaultMailtoAddress;
}
return \array_unique($addresses);
}
/**
* Get the mail template used to sent the mail.
*
* @throws Exception
*/
private function getMailTemplate(): ViewInterface
{
if ($this->template) {
return $this->viewFactory->createView('EmailNotify/' . $this->template);
}
return $this->getDefaultMailTemplate();
}
/**
* Get the default mail template.
*
* @throws Exception
*/
private function getDefaultMailTemplate(): ViewInterface
{
$template = $this->build->isSuccessful() ? 'short' : 'long';
return $this->viewFactory->createView('EmailNotify/' . $template);
}
}