Total Complexity | 66 |
Total Lines | 370 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 1 |
Complex classes like SparkPostHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SparkPostHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class SparkPostHelper |
||
22 | { |
||
23 | use Configurable; |
||
24 | |||
25 | const FROM_SITECONFIG = "SiteConfig"; |
||
26 | const FROM_ADMIN = "Admin"; |
||
27 | const FROM_DEFAULT = "Default"; |
||
28 | |||
29 | /** |
||
30 | * Client instance |
||
31 | * |
||
32 | * @var SparkPostApiClient |
||
33 | */ |
||
34 | protected static $client; |
||
35 | |||
36 | /** |
||
37 | * Get the mailer instance |
||
38 | * |
||
39 | * @return SilverStripe\Control\Email\SwiftMailer |
||
|
|||
40 | */ |
||
41 | public static function getMailer() |
||
42 | { |
||
43 | return Injector::inst()->get(Mailer::class); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | public static function getApiKey() |
||
50 | { |
||
51 | return self::config()->api_key; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get the api client instance |
||
56 | * @return SparkPostApiClient |
||
57 | * @throws Exception |
||
58 | */ |
||
59 | public static function getClient() |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get the api client instance |
||
83 | * @return LeKoala\SparkPost\Api\SparkPostApiClient |
||
84 | * |
||
85 | * @throws Exception |
||
86 | */ |
||
87 | public static function getMasterClient() |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get the log folder and create it if necessary |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public static function getLogFolder() |
||
103 | { |
||
104 | $logFolder = BASE_PATH . '/' . self::config()->log_folder; |
||
105 | if (!is_dir($logFolder)) { |
||
106 | mkdir($logFolder, 0755, true); |
||
107 | } |
||
108 | return $logFolder; |
||
109 | } |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Process environment variable to configure this module |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public static function init() |
||
118 | { |
||
119 | // Regular api key used for sending emails (including subaccount support) |
||
120 | $api_key = self::getEnvApiKey(); |
||
121 | if ($api_key) { |
||
122 | self::config()->api_key = $api_key; |
||
123 | } |
||
124 | |||
125 | // Master api key that is used to configure the account. If no api key is defined, the master api key is used |
||
126 | $master_api_key = self::getEnvMasterApiKey(); |
||
127 | if ($master_api_key) { |
||
128 | self::config()->master_api_key = $master_api_key; |
||
129 | if (!self::config()->api_key) { |
||
130 | self::config()->api_key = $master_api_key; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $sending_disabled = self::getEnvSendingDisabled(); |
||
135 | if ($sending_disabled) { |
||
136 | self::config()->disable_sending = $sending_disabled; |
||
137 | } |
||
138 | $enable_logging = self::getEnvEnableLogging(); |
||
139 | if ($enable_logging) { |
||
140 | self::config()->enable_logging = $enable_logging; |
||
141 | } |
||
142 | $subaccount_id = self::getEnvSubaccountId(); |
||
143 | if ($subaccount_id) { |
||
144 | self::config()->subaccount_id = $subaccount_id; |
||
145 | } |
||
146 | |||
147 | // We have a key, we can register the transport |
||
148 | if (self::config()->api_key) { |
||
149 | self::registerTransport(); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | public static function getEnvApiKey() |
||
154 | { |
||
155 | return Environment::getEnv('SPARKPOST_API_KEY'); |
||
156 | } |
||
157 | |||
158 | public static function getEnvMasterApiKey() |
||
159 | { |
||
160 | return Environment::getEnv('SPARKPOST_MASTER_API_KEY'); |
||
161 | } |
||
162 | |||
163 | public static function getEnvSendingDisabled() |
||
166 | } |
||
167 | |||
168 | public static function getEnvEnableLogging() |
||
169 | { |
||
170 | return Environment::getEnv('SPARKPOST_ENABLE_LOGGING'); |
||
171 | } |
||
172 | |||
173 | public static function getEnvSubaccountId() |
||
176 | } |
||
177 | |||
178 | public static function getEnvForceSender() |
||
179 | { |
||
180 | return Environment::getEnv('SPARKPOST_FORCE_SENDER'); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Register the transport with the client |
||
185 | * |
||
186 | * @return SilverStripe\Control\Email\SwiftMailer The updated swift mailer |
||
187 | * @throws Exception |
||
188 | */ |
||
189 | public static function registerTransport() |
||
190 | { |
||
191 | $client = self::getClient(); |
||
192 | $mailer = self::getMailer(); |
||
193 | if (!$mailer instanceof SwiftMailer) { |
||
194 | throw new Exception("Mailer must be an instance of " . SwiftMailer::class . " instead of " . get_class($mailer)); |
||
195 | } |
||
196 | $transport = new SparkPostSwiftTransport($client); |
||
197 | $newSwiftMailer = $mailer->getSwiftMailer()->newInstance($transport); |
||
198 | $mailer->setSwiftMailer($newSwiftMailer); |
||
199 | return $mailer; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Update admin email so that we use our config email |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | public static function forceAdminEmailOverride() |
||
208 | { |
||
209 | Config::modify()->set(Email::class, 'admin_email', self::resolveDefaultFromEmailType()); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Check if email is ready to send emails |
||
214 | * |
||
215 | * @param string $email |
||
216 | * @return boolean |
||
217 | */ |
||
218 | public static function isEmailDomainReady($email) |
||
219 | { |
||
220 | if (!$email) { |
||
221 | return false; |
||
222 | } |
||
223 | $parts = explode("@", $email); |
||
224 | if (count($parts) != 2) { |
||
225 | return false; |
||
226 | } |
||
227 | $client = SparkPostHelper::getClient(); |
||
228 | try { |
||
229 | $domain = $client->getSendingDomain(strtolower($parts[1])); |
||
230 | } catch (Exception $ex) { |
||
231 | return false; |
||
232 | } |
||
233 | if (!$domain) { |
||
234 | return false; |
||
235 | } |
||
236 | if ($domain['status']['dkim_status'] != 'valid') { |
||
237 | return false; |
||
238 | } |
||
239 | if ($domain['status']['compliance_status'] != 'valid') { |
||
240 | return false; |
||
241 | } |
||
242 | if ($domain['status']['ownership_verified'] != true) { |
||
243 | return false; |
||
244 | } |
||
245 | return true; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Resolve default send from address |
||
250 | * |
||
251 | * Keep in mind that an email using send() without a from |
||
252 | * will inject the admin_email. Therefore, SiteConfig |
||
253 | * will not be used |
||
254 | * See forceAdminEmailOverride() or use override_admin_email config |
||
255 | * |
||
256 | * @param string $from |
||
257 | * @param bool $createDefault |
||
258 | * @return string |
||
259 | */ |
||
260 | public static function resolveDefaultFromEmail($from = null, $createDefault = true) |
||
261 | { |
||
262 | $configEmail = self::getSenderFromSiteConfig(); |
||
263 | $original_from = $from; |
||
264 | if (!empty($from)) { |
||
265 | // We have a set email but sending from admin => override if flag is set |
||
266 | if (self::isAdminEmail($from) && $configEmail && self::config()->override_admin_email) { |
||
267 | return $configEmail; |
||
268 | } |
||
269 | // If we have a sender, validate its email |
||
270 | $from = EmailUtils::get_email_from_rfc_email($from); |
||
271 | if (filter_var($from, FILTER_VALIDATE_EMAIL)) { |
||
272 | return $original_from; |
||
273 | } |
||
274 | } |
||
275 | // Look in siteconfig for default sender |
||
276 | if ($configEmail) { |
||
277 | return $configEmail; |
||
278 | } |
||
279 | // Use admin email if set |
||
280 | if ($admin = Email::config()->admin_email) { |
||
281 | return $admin; |
||
282 | } |
||
283 | // If we still don't have anything, create something based on the domain |
||
284 | if ($createDefault) { |
||
285 | return self::createDefaultEmail(); |
||
286 | } |
||
287 | return false; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns what type of default email is used |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public static function resolveDefaultFromEmailType() |
||
296 | { |
||
297 | // Look in siteconfig for default sender |
||
298 | if (self::getSenderFromSiteConfig()) { |
||
299 | return self::FROM_SITECONFIG; |
||
300 | } |
||
301 | // Is admin email set ? |
||
302 | if (Email::config()->admin_email) { |
||
303 | return self::FROM_ADMIN; |
||
304 | } |
||
305 | return self::FROM_DEFAULT; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @return string|false |
||
310 | */ |
||
311 | public static function getSenderFromSiteConfig() |
||
312 | { |
||
313 | $config = SiteConfig::current_site_config(); |
||
314 | $config_field = self::config()->siteconfig_from; |
||
315 | if ($config_field && !empty($config->$config_field)) { |
||
316 | return $config->$config_field; |
||
317 | } |
||
318 | return false; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param string $email |
||
323 | * @return boolean |
||
324 | */ |
||
325 | public static function isAdminEmail($email) |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param string $email |
||
338 | * @return boolean |
||
339 | */ |
||
340 | public static function isDefaultEmail($email) |
||
341 | { |
||
342 | $rfc_email = EmailUtils::get_email_from_rfc_email($email); |
||
343 | return $rfc_email == self::createDefaultEmail(); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Resolve default send to address |
||
348 | * |
||
349 | * @param string $to |
||
350 | * @return string |
||
351 | */ |
||
352 | public static function resolveDefaultToEmail($to = null) |
||
353 | { |
||
354 | // In case of multiple recipients, do not validate anything |
||
355 | if (is_array($to) || strpos($to, ',') !== false) { |
||
356 | return $to; |
||
357 | } |
||
358 | $original_to = $to; |
||
359 | if (!empty($to)) { |
||
360 | $to = EmailUtils::get_email_from_rfc_email($to); |
||
361 | if (filter_var($to, FILTER_VALIDATE_EMAIL)) { |
||
362 | return $original_to; |
||
363 | } |
||
364 | } |
||
365 | $config = SiteConfig::current_site_config(); |
||
366 | $config_field = self::config()->siteconfig_to; |
||
367 | if ($config_field && !empty($config->$config_field)) { |
||
368 | return $config->$config_field; |
||
369 | } |
||
370 | if ($admin = Email::config()->admin_email) { |
||
371 | return $admin; |
||
372 | } |
||
373 | return false; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Create a sensible default address based on domain name |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | public static function createDefaultEmail() |
||
391 | } |
||
392 | } |
||
393 |