| Conditions | 14 |
| Paths | 134 |
| Total Lines | 103 |
| Code Lines | 66 |
| 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 declare(strict_types=1); |
||
| 122 | public function send(array $data, Context $context, array $templateData = []): ?Email |
||
| 123 | { |
||
| 124 | $mailBeforeValidateEvent = new MailBeforeValidateEvent($data, $context, $templateData); |
||
| 125 | $this->eventDispatcher->dispatch($mailBeforeValidateEvent); |
||
| 126 | |||
| 127 | if ($mailBeforeValidateEvent->isPropagationStopped()) { |
||
| 128 | return null; |
||
| 129 | } |
||
| 130 | |||
| 131 | $definition = $this->getValidationDefinition($context); |
||
| 132 | $this->dataValidator->validate($data, $definition); |
||
| 133 | |||
| 134 | $recipients = $data['recipients']; |
||
| 135 | $salesChannelId = $data['salesChannelId']; |
||
| 136 | $salesChannel = null; |
||
| 137 | |||
| 138 | if ($salesChannelId !== null && !isset($templateData['salesChannel'])) { |
||
| 139 | $criteria = $this->getSalesChannelDomainCriteria($salesChannelId, $context); |
||
| 140 | |||
| 141 | /** @var SalesChannelEntity|null $salesChannel */ |
||
| 142 | $salesChannel = $this->salesChannelRepository->search($criteria, $context)->get($salesChannelId); |
||
| 143 | |||
| 144 | if ($salesChannel === null) { |
||
| 145 | throw new SalesChannelNotFoundException($salesChannelId); |
||
| 146 | } |
||
| 147 | |||
| 148 | $templateData['salesChannel'] = $salesChannel; |
||
| 149 | } elseif (isset($templateData['salesChannel'])) { |
||
| 150 | $salesChannel = $templateData['salesChannel']; |
||
| 151 | } |
||
| 152 | |||
| 153 | $senderEmail = $this->getSender($data, $salesChannelId); |
||
| 154 | |||
| 155 | $contents = $this->buildContents($data, $salesChannel); |
||
| 156 | if (isset($data['testMode']) && (bool) $data['testMode'] === true) { |
||
| 157 | $this->templateRenderer->enableTestMode(); |
||
| 158 | } |
||
| 159 | |||
| 160 | $template = $data['subject']; |
||
| 161 | |||
| 162 | try { |
||
| 163 | $data['subject'] = $this->templateRenderer->render($template, $templateData, $context); |
||
| 164 | $template = $data['senderName']; |
||
| 165 | $data['senderName'] = $this->templateRenderer->render($template, $templateData, $context); |
||
| 166 | foreach ($contents as $index => $template) { |
||
| 167 | $contents[$index] = $this->templateRenderer->render($template, $templateData, $context); |
||
| 168 | } |
||
| 169 | } catch (\Throwable $e) { |
||
| 170 | $this->logger->error( |
||
| 171 | "Could not render Mail-Template with error message:\n" |
||
| 172 | . $e->getMessage() . "\n" |
||
| 173 | . 'Error Code:' . $e->getCode() . "\n" |
||
| 174 | . 'Template source:' |
||
| 175 | . $template . "\n" |
||
| 176 | . "Template data: \n" |
||
| 177 | . json_encode($templateData) . "\n" |
||
| 178 | ); |
||
| 179 | |||
| 180 | return null; |
||
| 181 | } |
||
| 182 | if (isset($data['testMode']) && (bool) $data['testMode'] === true) { |
||
| 183 | $this->templateRenderer->disableTestMode(); |
||
| 184 | } |
||
| 185 | |||
| 186 | $mediaUrls = $this->getMediaUrls($data, $context); |
||
| 187 | |||
| 188 | $binAttachments = $data['binAttachments'] ?? null; |
||
| 189 | |||
| 190 | $mail = $this->messageFactory->create( |
||
| 191 | $data['subject'], |
||
| 192 | [$senderEmail => $data['senderName']], |
||
| 193 | $recipients, |
||
| 194 | $contents, |
||
| 195 | $mediaUrls, |
||
| 196 | $data, |
||
| 197 | $binAttachments |
||
| 198 | ); |
||
| 199 | |||
| 200 | if ($mail->getBody()->toString() === '') { |
||
| 201 | $this->logger->error( |
||
| 202 | "message is null:\n" |
||
| 203 | . 'Data:' |
||
| 204 | . json_encode($data) . "\n" |
||
| 205 | . "Template data: \n" |
||
| 206 | . json_encode($templateData) . "\n" |
||
| 207 | ); |
||
| 208 | |||
| 209 | return null; |
||
| 210 | } |
||
| 211 | |||
| 212 | $mailBeforeSentEvent = new MailBeforeSentEvent($data, $mail, $context); |
||
| 213 | $this->eventDispatcher->dispatch($mailBeforeSentEvent); |
||
| 214 | |||
| 215 | if ($mailBeforeSentEvent->isPropagationStopped()) { |
||
| 216 | return null; |
||
| 217 | } |
||
| 218 | |||
| 219 | $this->mailSender->send($mail); |
||
| 220 | |||
| 221 | $mailSentEvent = new MailSentEvent($data['subject'], $recipients, $contents, $context); |
||
| 222 | $this->eventDispatcher->dispatch($mailSentEvent); |
||
| 223 | |||
| 224 | return $mail; |
||
| 225 | } |
||
| 319 |