Conditions | 18 |
Paths | 398 |
Total Lines | 139 |
Code Lines | 93 |
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 declare(strict_types=1); |
||
100 | public function send(array $data, Context $context, array $templateData = []): ?Email |
||
101 | { |
||
102 | $event = new MailBeforeValidateEvent($data, $context, $templateData); |
||
103 | $this->eventDispatcher->dispatch($event); |
||
104 | $data = $event->getData(); |
||
105 | $templateData = $event->getTemplateData(); |
||
106 | |||
107 | if ($event->isPropagationStopped()) { |
||
108 | return null; |
||
109 | } |
||
110 | |||
111 | $definition = $this->getValidationDefinition($context); |
||
112 | $this->dataValidator->validate($data, $definition); |
||
113 | |||
114 | $recipients = $data['recipients']; |
||
115 | $salesChannelId = $data['salesChannelId']; |
||
116 | $salesChannel = null; |
||
117 | |||
118 | if (($salesChannelId !== null && !isset($templateData['salesChannel'])) || $this->isTestMode($data)) { |
||
119 | $criteria = $this->getSalesChannelDomainCriteria($salesChannelId, $context); |
||
120 | |||
121 | /** @var SalesChannelEntity|null $salesChannel */ |
||
122 | $salesChannel = $this->salesChannelRepository->search($criteria, $context)->get($salesChannelId); |
||
123 | |||
124 | if ($salesChannel === null) { |
||
125 | throw new SalesChannelNotFoundException($salesChannelId); |
||
126 | } |
||
127 | |||
128 | $templateData['salesChannel'] = $salesChannel; |
||
129 | } elseif ($this->templateDataContainsSalesChannel($templateData)) { |
||
130 | $salesChannel = $templateData['salesChannel']; |
||
131 | } |
||
132 | |||
133 | $senderEmail = $data['senderMail'] ?? $this->getSender($data, $salesChannelId, $context); |
||
134 | |||
135 | if ($senderEmail === null) { |
||
136 | $event = new MailErrorEvent( |
||
137 | $context, |
||
138 | Logger::ERROR, |
||
139 | null, |
||
140 | 'senderMail not configured for salesChannel: ' . $salesChannelId . '. Please check system_config \'core.basicInformation.email\'', |
||
141 | null, |
||
142 | $templateData |
||
143 | ); |
||
144 | |||
145 | $this->eventDispatcher->dispatch($event); |
||
146 | $this->logger->error( |
||
147 | 'senderMail not configured for salesChannel: ' . $salesChannelId . '. Please check system_config \'core.basicInformation.email\'', |
||
148 | $templateData |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | $contents = $this->buildContents($data, $salesChannel); |
||
153 | if ($this->isTestMode($data)) { |
||
154 | $this->templateRenderer->enableTestMode(); |
||
155 | if (!isset($templateData['order']) && !isset($templateData['order']['deepLinkCode']) || $templateData['order']['deepLinkCode'] === '') { |
||
|
|||
156 | $templateData['order']['deepLinkCode'] = 'home'; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $template = $data['subject']; |
||
161 | |||
162 | try { |
||
163 | $data['subject'] = $this->templateRenderer->render($template, $templateData, $context, false); |
||
164 | $template = $data['senderName']; |
||
165 | $data['senderName'] = $this->templateRenderer->render($template, $templateData, $context, false); |
||
166 | foreach ($contents as $index => $template) { |
||
167 | $contents[$index] = $this->templateRenderer->render($template, $templateData, $context, $index !== 'text/plain'); |
||
168 | } |
||
169 | } catch (\Throwable $e) { |
||
170 | $event = new MailErrorEvent( |
||
171 | $context, |
||
172 | Logger::ERROR, |
||
173 | $e, |
||
174 | 'Could not render Mail-Template with error message: ' . $e->getMessage(), |
||
175 | $template, |
||
176 | $templateData |
||
177 | ); |
||
178 | $this->eventDispatcher->dispatch($event); |
||
179 | $this->logger->error( |
||
180 | 'Could not render Mail-Template with error message: ' . $e->getMessage(), |
||
181 | array_merge([ |
||
182 | 'template' => $template, |
||
183 | 'exception' => (string) $e, |
||
184 | ], $templateData) |
||
185 | ); |
||
186 | |||
187 | return null; |
||
188 | } |
||
189 | if (isset($data['testMode']) && (bool) $data['testMode'] === true) { |
||
190 | $this->templateRenderer->disableTestMode(); |
||
191 | } |
||
192 | |||
193 | $mediaUrls = $this->getMediaUrls($data, $context); |
||
194 | |||
195 | $binAttachments = $data['binAttachments'] ?? null; |
||
196 | |||
197 | $mail = $this->mailFactory->create( |
||
198 | $data['subject'], |
||
199 | [$senderEmail => $data['senderName']], |
||
200 | $recipients, |
||
201 | $contents, |
||
202 | $mediaUrls, |
||
203 | $data, |
||
204 | $binAttachments |
||
205 | ); |
||
206 | |||
207 | if ($mail->getBody()->toString() === '') { |
||
208 | $event = new MailErrorEvent( |
||
209 | $context, |
||
210 | Logger::ERROR, |
||
211 | null, |
||
212 | 'mail body is null', |
||
213 | null, |
||
214 | $templateData |
||
215 | ); |
||
216 | |||
217 | $this->eventDispatcher->dispatch($event); |
||
218 | $this->logger->error( |
||
219 | 'mail body is null', |
||
220 | $templateData |
||
221 | ); |
||
222 | |||
223 | return null; |
||
224 | } |
||
225 | |||
226 | $event = new MailBeforeSentEvent($data, $mail, $context, $templateData['eventName'] ?? null); |
||
227 | $this->eventDispatcher->dispatch($event); |
||
228 | |||
229 | if ($event->isPropagationStopped()) { |
||
230 | return null; |
||
231 | } |
||
232 | |||
233 | $this->mailSender->send($mail); |
||
234 | |||
235 | $event = new MailSentEvent($data['subject'], $recipients, $contents, $context, $templateData['eventName'] ?? null); |
||
236 | $this->eventDispatcher->dispatch($event); |
||
237 | |||
238 | return $mail; |
||
239 | } |
||
364 |