1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Plugins\DebugSendPlugin |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 7 |
13
|
|
|
* |
14
|
|
|
* @author Marcus Döllerer <[email protected]> |
15
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-cli |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Plugins; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Console\Question\Question; |
24
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
25
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
26
|
|
|
use TechDivision\Import\Configuration\SwiftMailerConfigurationInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Plugin that creates and sends a debug report via email. |
30
|
|
|
* |
31
|
|
|
* @author Marcus Döllerer <[email protected]> |
32
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class DebugSendPlugin extends AbstractConsolePlugin |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Process the plugin functionality. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
* @throws \InvalidArgumentException Is thrown if either the directory nor a artefact for the given serial is available |
45
|
|
|
*/ |
46
|
|
|
public function process() |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
// load the actual status |
50
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
51
|
|
|
|
52
|
|
|
// query whether or not a custom debug serial is available |
53
|
|
|
$serial = $this->getSerial(); |
54
|
|
|
if (isset($status[RegistryKeys::DEBUG_SERIAL])) { |
55
|
|
|
$serial = $status[RegistryKeys::DEBUG_SERIAL]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// retrieve the SwiftMailer configuration |
59
|
|
|
$swiftMailerConfiguration = $this->getPluginConfiguration()->getSwiftMailer(); |
60
|
|
|
|
61
|
|
|
// retrieve the question helper |
62
|
|
|
$questionHelper = $this->getHelper('question'); |
63
|
|
|
|
64
|
|
|
// use the configured SwiftMail recipient address as default if possible |
65
|
|
|
if ($swiftMailerConfiguration instanceof SwiftMailerConfigurationInterface && $swiftMailerConfiguration->hasParam('to')) { |
66
|
|
|
$recipient = $swiftMailerConfiguration->getParam('to'); |
67
|
|
|
// ask the user for the recipient address to send the debug report to |
68
|
|
|
$recipientQuestion = new Question( |
69
|
|
|
"<question>Please enter the email address of the debug report recipient (Configured: " . $recipient . "):\n</question>", |
70
|
|
|
$recipient |
71
|
|
|
); |
72
|
|
|
} else { |
73
|
|
|
$recipientQuestion = new Question( |
74
|
|
|
"<question>Please enter the email address of the debug report recipient:\n</question>" |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// ask the user to confirm the configured recipient address or enter a new one |
79
|
|
|
$recipient = $questionHelper->ask($this->getInput(), $this->getOutput(), $recipientQuestion); |
|
|
|
|
80
|
|
|
|
81
|
|
|
// warn the user about the impact of submitting their report and ask for confirmation |
82
|
|
|
$confirmationQuestion = new ConfirmationQuestion( |
83
|
|
|
"<comment>The debug report may contain confidential information (depending on the data you were importing).\n</comment>" |
84
|
|
|
. "<question>Do you really want to send the report to " . $recipient . "? (Y/n)\n</question>" |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
// abort the operation if the user does not confirm with 'y' or enter |
88
|
|
|
if (!$questionHelper->ask($this->getInput(), $this->getOutput(), $confirmationQuestion)) { |
89
|
|
|
$this->getOutput()->writeln('<warning>Aborting operation - debug report has NOT been sent.</warning>'); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// try to load the mailer instance |
94
|
|
|
if ($mailer = $this->getSwiftMailer()) { |
95
|
|
|
// initialize the message body |
96
|
|
|
$body = sprintf('<html><head></head><body>This mail contains the debug dump for import with serial "%s"</body></html>', $serial); |
97
|
|
|
|
98
|
|
|
// initialize the message template |
99
|
|
|
/** @var \Swift_Message $message */ |
100
|
|
|
$message = $mailer->createMessage() |
101
|
|
|
->setSubject('Test') |
102
|
|
|
->setFrom($swiftMailerConfiguration->getParam('from')) |
103
|
|
|
->setTo($recipient) |
104
|
|
|
->setBody($body, 'text/html'); |
105
|
|
|
|
106
|
|
|
// initialize the archive file |
107
|
|
|
$archiveFile = null; |
|
|
|
|
108
|
|
|
|
109
|
|
|
// query whether or not the archive file is available |
110
|
|
|
if (!is_file($archiveFile = sprintf('%s/%s.zip', sys_get_temp_dir(), $serial))) { |
111
|
|
|
$this->getOutput()->writeln(sprintf('<error>Can\'t find either a directory or ZIP artefact for serial "%s"</error>', $serial)); |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// attach the CSV files with zipped artefacts |
116
|
|
|
$message->attach(\Swift_Attachment::fromPath($archiveFile)); |
117
|
|
|
|
118
|
|
|
// initialize the array with the failed recipients |
119
|
|
|
$failedRecipients = array(); |
120
|
|
|
|
121
|
|
|
// send the mail |
122
|
|
|
$recipientsAccepted = $mailer->send($message, $failedRecipients); |
123
|
|
|
|
124
|
|
|
// query whether or not all recipients have been accepted |
125
|
|
|
if (sizeof($failedRecipients) > 0) { |
126
|
|
|
$this->getSystemLogger()->error(sprintf('Can\'t send mail to %s', implode(', ', $failedRecipients))); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// if at least one recipient has been accepted |
130
|
|
|
if ($recipientsAccepted > 0) { |
131
|
|
|
// cast 'to' into an array if not already |
132
|
|
|
is_array($recipient) ?: $recipient = (array)$recipient; |
133
|
|
|
|
134
|
|
|
// remove the NOT accepted recipients |
135
|
|
|
$acceptedRecipients = array_diff($recipient, $failedRecipients); |
136
|
|
|
|
137
|
|
|
// log a message with the accepted receivers |
138
|
|
|
$this->getSystemLogger()->info( |
139
|
|
|
sprintf( |
140
|
|
|
'Mail successfully sent to %d recipient(s) (%s)', |
141
|
|
|
$recipientsAccepted, |
142
|
|
|
implode(', ', $acceptedRecipients) |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
// write a message to the console, that the mailer configuration has not been available |
148
|
|
|
$this->getOutput()->writeln('<warning>The mailer configuration is not available or mailer can not be loaded</warning>'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|