| Total Complexity | 73 |
| Total Lines | 420 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 1 | Features | 2 |
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 MailerInterface |
||
| 40 | */ |
||
| 41 | public static function getMailer() |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param MailerInterface $mailer |
||
| 48 | * @return AbstractTransport|SparkpostApiTransport |
||
|
|
|||
| 49 | */ |
||
| 50 | public static function getTransportFromMailer($mailer) |
||
| 51 | { |
||
| 52 | $r = new ReflectionObject($mailer); |
||
| 53 | $p = $r->getProperty('transport'); |
||
| 54 | $p->setAccessible(true); |
||
| 55 | return $p->getValue($mailer); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public static function getApiKey() |
||
| 62 | { |
||
| 63 | return self::config()->api_key; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the api client instance |
||
| 68 | * @return SparkPostApiClient |
||
| 69 | * @throws Exception |
||
| 70 | */ |
||
| 71 | public static function getClient() |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the api client instance |
||
| 95 | * @return LeKoala\SparkPost\Api\SparkPostApiClient |
||
| 96 | * |
||
| 97 | * @throws Exception |
||
| 98 | */ |
||
| 99 | public static function getMasterClient() |
||
| 100 | { |
||
| 101 | $masterKey = self::config()->master_api_key; |
||
| 102 | if (!$masterKey) { |
||
| 103 | return self::getClient(); |
||
| 104 | } |
||
| 105 | $client = new SparkPostApiClient($masterKey); |
||
| 106 | return $client; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the log folder and create it if necessary |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public static function getLogFolder() |
||
| 115 | { |
||
| 116 | $logFolder = BASE_PATH . '/' . self::config()->log_folder; |
||
| 117 | if (!is_dir($logFolder)) { |
||
| 118 | mkdir($logFolder, 0755, true); |
||
| 119 | } |
||
| 120 | return $logFolder; |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Process environment variable to configure this module |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public static function init() |
||
| 130 | { |
||
| 131 | // Regular api key used for sending emails (including subaccount support) |
||
| 132 | $api_key = self::getEnvApiKey(); |
||
| 133 | if ($api_key) { |
||
| 134 | self::config()->api_key = $api_key; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Master api key that is used to configure the account. If no api key is defined, the master api key is used |
||
| 138 | $master_api_key = self::getEnvMasterApiKey(); |
||
| 139 | if ($master_api_key) { |
||
| 140 | self::config()->master_api_key = $master_api_key; |
||
| 141 | if (!self::config()->api_key) { |
||
| 142 | self::config()->api_key = $master_api_key; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $sending_disabled = self::getEnvSendingDisabled(); |
||
| 147 | if ($sending_disabled) { |
||
| 148 | self::config()->disable_sending = $sending_disabled; |
||
| 149 | } |
||
| 150 | $enable_logging = self::getEnvEnableLogging(); |
||
| 151 | if ($enable_logging) { |
||
| 152 | self::config()->enable_logging = $enable_logging; |
||
| 153 | } |
||
| 154 | $subaccount_id = self::getEnvSubaccountId(); |
||
| 155 | if ($subaccount_id) { |
||
| 156 | self::config()->subaccount_id = $subaccount_id; |
||
| 157 | } |
||
| 158 | |||
| 159 | // We have a key, we can register the transport |
||
| 160 | if (self::config()->api_key) { |
||
| 161 | self::registerTransport(); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function getEnvApiKey() |
||
| 166 | { |
||
| 167 | return Environment::getEnv('SPARKPOST_API_KEY'); |
||
| 168 | } |
||
| 169 | |||
| 170 | public static function getEnvMasterApiKey() |
||
| 171 | { |
||
| 172 | return Environment::getEnv('SPARKPOST_MASTER_API_KEY'); |
||
| 173 | } |
||
| 174 | |||
| 175 | public static function getEnvSendingDisabled() |
||
| 176 | { |
||
| 177 | return Environment::getEnv('SPARKPOST_SENDING_DISABLED'); |
||
| 178 | } |
||
| 179 | |||
| 180 | public static function getEnvEnableLogging() |
||
| 181 | { |
||
| 182 | return Environment::getEnv('SPARKPOST_ENABLE_LOGGING'); |
||
| 183 | } |
||
| 184 | |||
| 185 | public static function getEnvSubaccountId() |
||
| 186 | { |
||
| 187 | return Environment::getEnv('SPARKPOST_SUBACCOUNT_ID'); |
||
| 188 | } |
||
| 189 | |||
| 190 | public static function getSubaccountId() |
||
| 191 | { |
||
| 192 | return self::config()->subaccount_id; |
||
| 193 | } |
||
| 194 | |||
| 195 | public static function getEnvForceSender() |
||
| 196 | { |
||
| 197 | return Environment::getEnv('SPARKPOST_FORCE_SENDER'); |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function getWebhookUsername() |
||
| 201 | { |
||
| 202 | return self::config()->webhook_username; |
||
| 203 | } |
||
| 204 | |||
| 205 | public static function getWebhookPassword() |
||
| 206 | { |
||
| 207 | return self::config()->webhook_password; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Register the transport with the client |
||
| 212 | * |
||
| 213 | * @return SparkPostApiTransport The updated mailer |
||
| 214 | * @throws Exception |
||
| 215 | */ |
||
| 216 | public static function registerTransport() |
||
| 217 | { |
||
| 218 | $client = self::getClient(); |
||
| 219 | $mailer = self::getMailer(); |
||
| 220 | $transport = new SparkPostApiTransport($client); |
||
| 221 | $mailer = new Mailer($transport); |
||
| 222 | Injector::inst()->registerService($mailer, MailerInterface::class); |
||
| 223 | return $mailer; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Update admin email so that we use our config email |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public static function forceAdminEmailOverride() |
||
| 232 | { |
||
| 233 | Config::modify()->set(Email::class, 'admin_email', self::resolveDefaultFromEmailType()); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if email is ready to send emails |
||
| 238 | * |
||
| 239 | * @param string $email |
||
| 240 | * @return boolean |
||
| 241 | */ |
||
| 242 | public static function isEmailDomainReady($email) |
||
| 243 | { |
||
| 244 | if (!$email) { |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | $parts = explode("@", $email); |
||
| 248 | if (count($parts) != 2) { |
||
| 249 | return false; |
||
| 250 | } |
||
| 251 | $client = SparkPostHelper::getClient(); |
||
| 252 | try { |
||
| 253 | $domain = $client->getSendingDomain(strtolower($parts[1])); |
||
| 254 | } catch (Exception $ex) { |
||
| 255 | return false; |
||
| 256 | } |
||
| 257 | if (!$domain) { |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | if ($domain['status']['dkim_status'] != 'valid') { |
||
| 261 | return false; |
||
| 262 | } |
||
| 263 | if ($domain['status']['compliance_status'] != 'valid') { |
||
| 264 | return false; |
||
| 265 | } |
||
| 266 | if ($domain['status']['ownership_verified'] != true) { |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | return true; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Resolve default send from address |
||
| 274 | * |
||
| 275 | * Keep in mind that an email using send() without a from |
||
| 276 | * will inject the admin_email. Therefore, SiteConfig |
||
| 277 | * will not be used |
||
| 278 | * See forceAdminEmailOverride() or use override_admin_email config |
||
| 279 | * |
||
| 280 | * @param string $from |
||
| 281 | * @param bool $createDefault |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public static function resolveDefaultFromEmail($from = null, $createDefault = true) |
||
| 285 | { |
||
| 286 | $configEmail = self::getSenderFromSiteConfig(); |
||
| 287 | $original_from = $from; |
||
| 288 | if (!empty($from)) { |
||
| 289 | // We have a set email but sending from admin => override if flag is set |
||
| 290 | if (self::isAdminEmail($from) && $configEmail && self::config()->override_admin_email) { |
||
| 291 | return $configEmail; |
||
| 292 | } |
||
| 293 | // If we have a sender, validate its email |
||
| 294 | $from = EmailUtils::get_email_from_rfc_email($from); |
||
| 295 | if (filter_var($from, FILTER_VALIDATE_EMAIL)) { |
||
| 296 | return $original_from; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | // Look in siteconfig for default sender |
||
| 300 | if ($configEmail) { |
||
| 301 | return $configEmail; |
||
| 302 | } |
||
| 303 | // Use admin email if set |
||
| 304 | if ($admin = Email::config()->admin_email) { |
||
| 305 | return $admin; |
||
| 306 | } |
||
| 307 | // If we still don't have anything, create something based on the domain |
||
| 308 | if ($createDefault) { |
||
| 309 | return self::createDefaultEmail(); |
||
| 310 | } |
||
| 311 | return false; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns what type of default email is used |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public static function resolveDefaultFromEmailType() |
||
| 320 | { |
||
| 321 | // Look in siteconfig for default sender |
||
| 322 | if (self::getSenderFromSiteConfig()) { |
||
| 323 | return self::FROM_SITECONFIG; |
||
| 324 | } |
||
| 325 | // Is admin email set ? |
||
| 326 | if (Email::config()->admin_email) { |
||
| 327 | return self::FROM_ADMIN; |
||
| 328 | } |
||
| 329 | return self::FROM_DEFAULT; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return string|false |
||
| 334 | */ |
||
| 335 | public static function getSenderFromSiteConfig() |
||
| 336 | { |
||
| 337 | $config = SiteConfig::current_site_config(); |
||
| 338 | $config_field = self::config()->siteconfig_from; |
||
| 339 | if ($config_field && !empty($config->$config_field)) { |
||
| 340 | return $config->$config_field; |
||
| 341 | } |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $email |
||
| 347 | * @return boolean |
||
| 348 | */ |
||
| 349 | public static function isAdminEmail($email) |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string $email |
||
| 362 | * @return boolean |
||
| 363 | */ |
||
| 364 | public static function isDefaultEmail($email) |
||
| 365 | { |
||
| 366 | $rfc_email = EmailUtils::get_email_from_rfc_email($email); |
||
| 367 | return $rfc_email == self::createDefaultEmail(); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Resolve default send to address |
||
| 372 | * |
||
| 373 | * @param string $to |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public static function resolveDefaultToEmail($to = null) |
||
| 377 | { |
||
| 378 | // In case of multiple recipients, do not validate anything |
||
| 379 | if (is_array($to) || strpos($to, ',') !== false) { |
||
| 380 | return $to; |
||
| 381 | } |
||
| 382 | $original_to = $to; |
||
| 383 | if (!empty($to)) { |
||
| 384 | $to = EmailUtils::get_email_from_rfc_email($to); |
||
| 385 | if (filter_var($to, FILTER_VALIDATE_EMAIL)) { |
||
| 386 | return $original_to; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | $config = SiteConfig::current_site_config(); |
||
| 390 | $config_field = self::config()->siteconfig_to; |
||
| 391 | if ($config_field && !empty($config->$config_field)) { |
||
| 392 | return $config->$config_field; |
||
| 393 | } |
||
| 394 | if ($admin = Email::config()->admin_email) { |
||
| 395 | return $admin; |
||
| 396 | } |
||
| 397 | return false; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Create a sensible default address based on domain name |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public static function createDefaultEmail() |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Is logging enabled? |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public static function getLoggingEnabled() |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Is sending enabled? |
||
| 432 | * |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | public static function getSendingEnabled() |
||
| 441 | } |
||
| 442 | } |
||
| 443 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths