| Conditions | 22 |
| Paths | 3096 |
| Total Lines | 108 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 77 | protected function executeInternal() |
||
| 78 | { |
||
| 79 | $languageBackup = null; |
||
| 80 | // Flexform overrides write strings instead of integers so |
||
| 81 | // we need to cast the string '0' to false. |
||
| 82 | if ( |
||
| 83 | isset($this->options['addHtmlPart']) |
||
| 84 | && $this->options['addHtmlPart'] === '0' |
||
| 85 | ) { |
||
| 86 | $this->options['addHtmlPart'] = false; |
||
| 87 | } |
||
| 88 | |||
| 89 | $subject = $this->parseOption('subject'); |
||
| 90 | $recipients = $this->getRecipients('recipients', 'recipientAddress', 'recipientName'); |
||
| 91 | $senderAddress = $this->parseOption('senderAddress'); |
||
| 92 | $senderAddress = is_string($senderAddress) ? $senderAddress : ''; |
||
| 93 | $senderName = $this->parseOption('senderName'); |
||
| 94 | $senderName = is_string($senderName) ? $senderName : ''; |
||
| 95 | $replyToRecipients = $this->getRecipients('replyToRecipients', 'replyToAddress'); |
||
| 96 | $carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients', 'carbonCopyAddress'); |
||
| 97 | $blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients', 'blindCarbonCopyAddress'); |
||
| 98 | $addHtmlPart = $this->isHtmlPartAdded(); |
||
| 99 | $attachUploads = $this->parseOption('attachUploads'); |
||
| 100 | |||
| 101 | if (empty($subject)) { |
||
| 102 | throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320); |
||
| 103 | } |
||
| 104 | if (empty($recipients)) { |
||
| 105 | throw new FinisherException('The option "recipients" must be set for the EmailFinisher.', 1327060200); |
||
| 106 | } |
||
| 107 | if (empty($senderAddress)) { |
||
| 108 | throw new FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210); |
||
| 109 | } |
||
| 110 | |||
| 111 | $mail = $this->objectManager->get(MailMessage::class); |
||
| 112 | |||
| 113 | $mail->from(new Address($senderAddress, $senderName)) |
||
| 114 | ->to(...$recipients) |
||
| 115 | ->subject($subject); |
||
| 116 | |||
| 117 | if (!empty($replyToRecipients)) { |
||
| 118 | $mail->replyTo(...$replyToRecipients); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!empty($carbonCopyRecipients)) { |
||
| 122 | $mail->cc(...$carbonCopyRecipients); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!empty($blindCarbonCopyRecipients)) { |
||
| 126 | $mail->bcc(...$blindCarbonCopyRecipients); |
||
| 127 | } |
||
| 128 | |||
| 129 | $formRuntime = $this->finisherContext->getFormRuntime(); |
||
| 130 | |||
| 131 | $translationService = TranslationService::getInstance(); |
||
| 132 | if (isset($this->options['translation']['language']) && !empty($this->options['translation']['language'])) { |
||
| 133 | $languageBackup = $translationService->getLanguage(); |
||
| 134 | $translationService->setLanguage($this->options['translation']['language']); |
||
| 135 | } |
||
| 136 | |||
| 137 | $parts = [ |
||
| 138 | [ |
||
| 139 | 'format' => 'Plaintext', |
||
| 140 | 'contentType' => 'text/plain', |
||
| 141 | ], |
||
| 142 | ]; |
||
| 143 | |||
| 144 | if ($addHtmlPart) { |
||
| 145 | $parts[] = [ |
||
| 146 | 'format' => 'Html', |
||
| 147 | 'contentType' => 'text/html', |
||
| 148 | ]; |
||
| 149 | } |
||
| 150 | |||
| 151 | foreach ($parts as $i => $part) { |
||
| 152 | $standaloneView = $this->initializeStandaloneView($formRuntime, $part['format']); |
||
| 153 | $message = $standaloneView->render(); |
||
| 154 | |||
| 155 | if ($part['contentType'] === 'text/plain') { |
||
| 156 | $mail->text($message); |
||
| 157 | } else { |
||
| 158 | $mail->html($message); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!empty($languageBackup)) { |
||
| 163 | $translationService->setLanguage($languageBackup); |
||
| 164 | } |
||
| 165 | |||
| 166 | $elements = $formRuntime->getFormDefinition()->getRenderablesRecursively(); |
||
| 167 | |||
| 168 | if ($attachUploads) { |
||
| 169 | foreach ($elements as $element) { |
||
| 170 | if (!$element instanceof FileUpload) { |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | $file = $formRuntime[$element->getIdentifier()]; |
||
| 174 | if ($file) { |
||
| 175 | if ($file instanceof FileReference) { |
||
| 176 | $file = $file->getOriginalResource(); |
||
| 177 | } |
||
| 178 | |||
| 179 | $mail->attach($file->getContents(), $file->getName(), $file->getMimeType()); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | $mail->send(); |
||
| 185 | } |
||
| 322 |