Conditions | 10 |
Paths | 28 |
Total Lines | 107 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | $recipientsAccepted = 0; |
||
121 | |||
122 | // send the mail |
||
123 | $recipientsAccepted = $mailer->send($message, $failedRecipients); |
||
124 | |||
125 | // query whether or not all recipients have been accepted |
||
126 | if (sizeof($failedRecipients) > 0) { |
||
127 | $this->getSystemLogger()->error(sprintf('Can\'t send mail to %s', implode(', ', $failedRecipients))); |
||
128 | } |
||
129 | |||
130 | // if at least one recipient has been accepted |
||
131 | if ($recipientsAccepted > 0) { |
||
132 | // cast 'to' into an array if not already |
||
133 | is_array($recipient) ? : $recipient = (array) $recipient; |
||
134 | // remove the NOT accepted recipients |
||
135 | $acceptedRecipients = array_diff($recipient, $failedRecipients); |
||
136 | |||
137 | // log a message with the successfull 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 | |||
147 | // write a message to the console, that the debug report has successfully been submitted |
||
148 | $this->getOutput()->writeln('<info>The debug report has successfully been submitted to ' . implode(', ', $recipient) . '</info>'); |
||
149 | |||
150 | } else { |
||
151 | // write a message to the console, that the mailer configuration has not been available |
||
152 | $this->getOutput()->writeln('<warning>The mailer configuration is not available or mailer can not be loaded</warning>'); |
||
153 | } |
||
156 |