Conditions | 13 |
Paths | 104 |
Total Lines | 87 |
Code Lines | 55 |
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); |
||
104 | public function sendMail(BusinessEvent $event): void |
||
105 | { |
||
106 | $mailEvent = $event->getEvent(); |
||
107 | |||
108 | $extension = $event->getContext()->getExtension(self::MAIL_CONFIG_EXTENSION); |
||
109 | if (!$extension instanceof MailSendSubscriberConfig) { |
||
110 | $extension = new MailSendSubscriberConfig(false, [], []); |
||
111 | } |
||
112 | |||
113 | if ($extension->skip()) { |
||
114 | return; |
||
115 | } |
||
116 | |||
117 | if (!$mailEvent instanceof MailActionInterface) { |
||
118 | throw new MailEventConfigurationException('Not a instance of MailActionInterface', \get_class($mailEvent)); |
||
119 | } |
||
120 | |||
121 | $config = $event->getConfig(); |
||
122 | |||
123 | if (!isset($config['mail_template_id'])) { |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $mailTemplate = $this->getMailTemplate($config['mail_template_id'], $event->getContext()); |
||
128 | |||
129 | if ($mailTemplate === null) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | $data = new DataBag(); |
||
134 | |||
135 | $recipients = $mailEvent->getMailStruct()->getRecipients(); |
||
136 | if (isset($config['recipients'])) { |
||
137 | $recipients = $config['recipients']; |
||
138 | } |
||
139 | |||
140 | $data->set('recipients', $recipients); |
||
141 | $data->set('senderName', $mailTemplate->getTranslation('senderName')); |
||
142 | $data->set('salesChannelId', $mailEvent->getSalesChannelId()); |
||
143 | |||
144 | $data->set('templateId', $mailTemplate->getId()); |
||
145 | $data->set('customFields', $mailTemplate->getCustomFields()); |
||
146 | $data->set('contentHtml', $mailTemplate->getTranslation('contentHtml')); |
||
147 | $data->set('contentPlain', $mailTemplate->getTranslation('contentPlain')); |
||
148 | $data->set('subject', $mailTemplate->getTranslation('subject')); |
||
149 | $data->set('mediaIds', []); |
||
150 | |||
151 | $attachments = $this->buildAttachments($event, $mailTemplate, $extension); |
||
152 | |||
153 | if (!empty($attachments)) { |
||
154 | $data->set('binAttachments', $attachments); |
||
155 | } |
||
156 | |||
157 | try { |
||
158 | //@deprecated tag:v6.4.0 (flag:FEATURE_NEXT_12246) remove complete if-else-branch |
||
159 | // and only keep the if-content |
||
160 | if (Feature::isActive('FEATURE_NEXT_12246') && $this->emailService !== null) { |
||
161 | $this->emailService->send( |
||
162 | $data->all(), |
||
163 | $event->getContext(), |
||
164 | $this->getTemplateData($mailEvent) |
||
165 | ); |
||
166 | } elseif ($this->mailService !== null) { |
||
167 | $this->mailService->send( |
||
168 | $data->all(), |
||
169 | $event->getContext(), |
||
170 | $this->getTemplateData($mailEvent) |
||
171 | ); |
||
172 | } else { |
||
173 | //@deprecated tag:v6.4.0 (flag:FEATURE_NEXT_12246) remove check for null and Exception on Feature Release |
||
174 | throw new \Exception('Either `emailService` or `mailService` needs to be defined. Neither is.'); |
||
175 | } |
||
176 | |||
177 | $writes = array_map(static function ($id) { |
||
178 | return ['id' => $id, 'sent' => true]; |
||
179 | }, $extension->getDocumentIds()); |
||
180 | |||
181 | if (!empty($writes)) { |
||
182 | $this->documentRepository->update($writes, $event->getContext()); |
||
183 | } |
||
184 | } catch (\Exception $e) { |
||
185 | $this->logger->error( |
||
186 | "Could not send mail:\n" |
||
187 | . $e->getMessage() . "\n" |
||
188 | . 'Error Code:' . $e->getCode() . "\n" |
||
189 | . "Template data: \n" |
||
190 | . json_encode($data->all()) . "\n" |
||
191 | ); |
||
274 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.