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