Conditions | 19 |
Paths | 2 |
Total Lines | 143 |
Code Lines | 83 |
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 |
||
80 | public function init() |
||
81 | { |
||
82 | parent::init(); |
||
83 | self::$plugin = $this; |
||
84 | |||
85 | if (!Craft::$app->plugins->isPluginInstalled('contact-form') && !Craft::$app->request->getIsConsoleRequest()) { |
||
86 | Craft::$app->session->setNotice(Craft::t('contact-form-extensions', 'The Contact Form plugin is not installed or activated, Contact Form Extensions does not work without it.')); |
||
|
|||
87 | } |
||
88 | |||
89 | Event::on(View::class, View::EVENT_BEFORE_RENDER_TEMPLATE, function (TemplateEvent $e) { |
||
90 | if ( |
||
91 | $e->template === 'settings/plugins/_settings' && |
||
92 | $e->variables['plugin'] === $this |
||
93 | ) { |
||
94 | // Add the tabs |
||
95 | $e->variables['tabs'] = [ |
||
96 | ['label' => 'Settings', 'url' => '#settings-tab-settings'], |
||
97 | ['label' => 'reCAPTCHA', 'url' => '#settings-tab-recaptcha'], |
||
98 | ]; |
||
99 | } |
||
100 | }); |
||
101 | |||
102 | Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) { |
||
103 | $event->rules = array_merge($event->rules, [ |
||
104 | 'contact-form-extensions/submissions/<submissionId:\d+>' => 'contact-form-extensions/submissions/show-submission', |
||
105 | 'contact-form-extensions/submissions/<submissionId:\d+>/<siteHandle:{handle}>' => 'contact-form-extensions/submissions/show-submission', |
||
106 | ]); |
||
107 | }); |
||
108 | |||
109 | Event::on(Mailer::class, Mailer::EVENT_BEFORE_SEND, function (SendEvent $e) { |
||
110 | if ($e->isSpam) { |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | if ($this->settings->recaptcha) { |
||
115 | $recaptcha = $this->contactFormExtensionsService->getRecaptcha(); |
||
116 | $captchaResponse = Craft::$app->request->getParam('g-recaptcha-response'); |
||
117 | |||
118 | if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) { |
||
119 | $e->isSpam = true; |
||
120 | $e->handled = true; |
||
121 | |||
122 | return; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $submission = $e->submission; |
||
127 | |||
128 | $attNames = []; |
||
129 | if ($this->settings->attachmentVolumeHandle && $submission->attachment) { |
||
130 | $volume = Craft::$app->getVolumes()->getVolumeByHandle($this->settings->attachmentVolumeHandle); |
||
131 | if ($volume) { |
||
132 | foreach ($submission->attachment as $attachment) { |
||
133 | $date = date_create(); |
||
134 | $asset = new Asset(); |
||
135 | $asset->tempFilePath = $attachment->tempName; |
||
136 | $asset->filename = $date->getTimestamp().'_'.$attachment->name; |
||
137 | $asset->newFolderId = Craft::$app->assets->getRootFolderByVolumeId($volume->id)->id; |
||
138 | $asset->volumeId = $volume->id; |
||
139 | $asset->avoidFilenameConflicts = true; |
||
140 | $asset->setScenario(Asset::SCENARIO_CREATE); |
||
141 | $result = Craft::$app->getElements()->saveElement($asset); |
||
142 | array_push($attNames, $asset->filename); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if ($this->settings->enableDatabase) { |
||
148 | $this->contactFormExtensionsService->saveSubmission($submission, implode(', ', $attNames)); |
||
149 | } |
||
150 | |||
151 | // Set the overridden "toEmail" setting |
||
152 | if (array_key_exists('toEmail', $e->submission->message)) { |
||
153 | $email = Craft::$app->security->validateData($e->submission->message['toEmail']); |
||
154 | $e->toEmails = explode(',', $email); |
||
155 | } |
||
156 | |||
157 | if ($this->settings->enableTemplateOverwrite) { |
||
158 | // First set the template mode to the Site templates |
||
159 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
||
160 | |||
161 | // Render the set template |
||
162 | $html = Craft::$app->view->renderTemplate( |
||
163 | $this->settings->notificationTemplate, |
||
164 | ['submission' => $e->submission] |
||
165 | ); |
||
166 | |||
167 | // Update the message body |
||
168 | $e->message->setHtmlBody($html); |
||
169 | |||
170 | // Set the template mode back to Control Panel |
||
171 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
||
172 | } |
||
173 | }); |
||
174 | |||
175 | Event::on(Mailer::class, Mailer::EVENT_AFTER_SEND, function (SendEvent $e) { |
||
176 | if ($this->settings->enableConfirmationEmail) { |
||
177 | // First set the template mode to the Site templates |
||
178 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
||
179 | |||
180 | // Check if template is overridden in form |
||
181 | $template = null; |
||
182 | if (array_key_exists('template', $e->submission->message)) { |
||
183 | $template = '_emails\\'.Craft::$app->security->validateData($e->submission->message['template']); |
||
184 | } else { |
||
185 | // Render the set template |
||
186 | $template = $this->settings->confirmationTemplate; |
||
187 | } |
||
188 | $html = Craft::$app->view->renderTemplate( |
||
189 | $template, |
||
190 | ['submission' => $e->submission] |
||
191 | ); |
||
192 | |||
193 | // Create the confirmation email |
||
194 | $message = new Message(); |
||
195 | $message->setTo($e->submission->fromEmail); |
||
196 | if (isset(App::mailSettings()->fromEmail)) { |
||
197 | $message->setFrom(App::mailSettings()->fromEmail); |
||
198 | } else { |
||
199 | $message->setFrom($e->message->getTo()); |
||
200 | } |
||
201 | $message->setHtmlBody($html); |
||
202 | if (array_key_exists('subject', $e->submission)) { |
||
203 | $message->setSubject($e->submission->subject); |
||
204 | } else { |
||
205 | $message->setSubject($this->settings->getConfirmationSubject()); |
||
206 | } |
||
207 | |||
208 | // Send the mail |
||
209 | Craft::$app->mailer->send($message); |
||
210 | |||
211 | // Set the template mode back to Control Panel |
||
212 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
||
213 | } |
||
214 | }); |
||
215 | |||
216 | Event::on( |
||
217 | CraftVariable::class, |
||
218 | CraftVariable::EVENT_INIT, |
||
219 | function (Event $event) { |
||
220 | /** @var CraftVariable $variable */ |
||
221 | $variable = $event->sender; |
||
222 | $variable->set('contactFormExtensions', ContactFormExtensionsVariable::class); |
||
223 | } |
||
283 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.