| Total Complexity | 51 |
| Total Lines | 408 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PHPMailer 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 PHPMailer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class PHPMailer extends AbstractAdapter |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * {@inheritDoc} |
||
| 13 | */ |
||
| 14 | protected array $dependancies = [ |
||
| 15 | ['class' => Mailer::class, 'package' => 'phpmailer/phpmailer'] |
||
| 16 | ]; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Mailer |
||
| 20 | */ |
||
| 21 | protected $mailer; |
||
| 22 | |||
| 23 | public function __construct(bool $debug = false) |
||
| 24 | { |
||
| 25 | $this->mailer = new Mailer(); |
||
| 26 | $this->mailer->Debugoutput = function($str, $level) { |
||
| 27 | Services::logger()->info('[Mail][' . $level . ']: ' . $str); |
||
| 28 | }; |
||
| 29 | |||
| 30 | parent::__construct($debug); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritDoc} |
||
| 35 | */ |
||
| 36 | public function init(array $config): self |
||
| 37 | { |
||
| 38 | if (!empty($config['username']) && !empty($config['password'])) { |
||
| 39 | $this->mailer->SMTPAuth = true; |
||
| 40 | } |
||
| 41 | |||
| 42 | return parent::init($config); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritDoc} |
||
| 47 | */ |
||
| 48 | public function setPort(int $port): self |
||
| 49 | { |
||
| 50 | $this->mailer->Port = $port; |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritDoc} |
||
| 57 | */ |
||
| 58 | public function setHost(string $host): self |
||
| 59 | { |
||
| 60 | $this->mailer->Host = $host; |
||
| 61 | |||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritDoc} |
||
| 67 | */ |
||
| 68 | public function setUsername(string $username): self |
||
| 69 | { |
||
| 70 | $this->mailer->Username = $username; |
||
| 71 | |||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritDoc} |
||
| 77 | */ |
||
| 78 | public function setPassword(string $password): self |
||
| 79 | { |
||
| 80 | $this->mailer->Password = $password; |
||
| 81 | |||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritDoc} |
||
| 87 | */ |
||
| 88 | public function setDebug(int $debug = SMTP::DEBUG_SERVER): self |
||
| 89 | { |
||
| 90 | $this->mailer->SMTPDebug = $debug; |
||
| 91 | |||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritDoc} |
||
| 97 | */ |
||
| 98 | public function setProtocol(string $protocol): self |
||
| 99 | { |
||
| 100 | switch (strtolower($protocol)) { |
||
| 101 | case static::PROTOCOL_MAIL: |
||
| 102 | $this->mailer->isMail(); |
||
| 103 | break; |
||
| 104 | case static::PROTOCOL_QMAIL: |
||
| 105 | $this->mailer->isQmail(); |
||
| 106 | break; |
||
| 107 | case static::PROTOCOL_SENDMAIL: |
||
| 108 | $this->mailer->isSendmail(); |
||
| 109 | break; |
||
| 110 | default: |
||
| 111 | $this->mailer->isSMTP(); |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | |||
| 115 | return $this; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritDoc} |
||
| 120 | */ |
||
| 121 | public function setTimeout(int $timeout): self |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritDoc} |
||
| 130 | */ |
||
| 131 | public function setCharset(string $charset): self |
||
| 132 | { |
||
| 133 | $this->mailer->CharSet = $charset; |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritDoc} |
||
| 140 | */ |
||
| 141 | public function setPriority(int $priority): self |
||
| 142 | { |
||
| 143 | if (in_array($priority, static::PRIORITY_MAP, true)) { |
||
| 144 | $this->mailer->Priority = $priority; |
||
| 145 | } |
||
| 146 | |||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritDoc} |
||
| 152 | */ |
||
| 153 | public function setEncryption(?string $encryption): self |
||
| 154 | { |
||
| 155 | if (in_array($encryption, [null, static::ENCRYPTION_SSL, static::ENCRYPTION_TLS], true)) { |
||
| 156 | $this->mailer->SMTPSecure = $encryption; |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritDoc} |
||
| 165 | */ |
||
| 166 | public function alt(string $content) : self |
||
| 167 | { |
||
| 168 | $this->mailer->AltBody = $content; |
||
| 169 | |||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritDoc} |
||
| 175 | * |
||
| 176 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 177 | */ |
||
| 178 | public function attach(array|string $path, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): self |
||
| 179 | { |
||
| 180 | if (is_string($path)) { |
||
| 181 | $path = [$path => $name]; |
||
| 182 | } |
||
| 183 | |||
| 184 | foreach ($path As $key => $value) { |
||
| 185 | $this->mailer->addAttachment($key, $value, $encoding, $type, $disposition); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritDoc} |
||
| 193 | * |
||
| 194 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 195 | */ |
||
| 196 | public function attachBinary($binary, string $name, string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): self |
||
| 197 | { |
||
| 198 | $this->mailer->addStringAttachment($binary, $name, $encoding, $type, $disposition); |
||
| 199 | |||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritDoc} |
||
| 205 | * |
||
| 206 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 207 | */ |
||
| 208 | public function bcc(array|string $address, bool|string $name = '', bool $set = false): self |
||
| 209 | { |
||
| 210 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
| 211 | |||
| 212 | if ($set) { |
||
| 213 | $this->mailer->clearBCCs(); |
||
| 214 | } |
||
| 215 | |||
| 216 | foreach ($addresses as $address) { |
||
| 217 | $this->mailer->addBCC(...$address); |
||
| 218 | } |
||
| 219 | |||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritDoc} |
||
| 225 | * |
||
| 226 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 227 | */ |
||
| 228 | public function cc(array|string $address, bool|string $name = '', bool $set = false): self |
||
| 229 | { |
||
| 230 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
| 231 | |||
| 232 | if ($set) { |
||
| 233 | $this->mailer->clearCCs(); |
||
| 234 | } |
||
| 235 | |||
| 236 | foreach ($addresses as $address) { |
||
| 237 | $this->mailer->addCC(...$address); |
||
| 238 | } |
||
| 239 | |||
| 240 | return $this; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritDoc} |
||
| 245 | */ |
||
| 246 | public function dkim(string $pk, string $passphrase = '', string $selector = '', string $domain = ''): self |
||
| 247 | { |
||
| 248 | $this->mailer->DKIM_domain = $domain; |
||
| 249 | $this->mailer->DKIM_private = $pk; |
||
| 250 | $this->mailer->DKIM_selector = $selector ?: 'blitz'; |
||
| 251 | $this->mailer->DKIM_passphrase = $passphrase; |
||
| 252 | $this->mailer->DKIM_identity = $this->mailer->From; |
||
| 253 | |||
| 254 | return $this; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritDoc} |
||
| 259 | * |
||
| 260 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 261 | */ |
||
| 262 | public function embedded(string $path, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): self |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritDoc} |
||
| 271 | * |
||
| 272 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 273 | */ |
||
| 274 | public function embeddedBinary($binary, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): self |
||
| 275 | { |
||
| 276 | $this->mailer->addStringEmbeddedImage($binary, $cid, $name, $encoding, $type, $disposition); |
||
| 277 | |||
| 278 | return $this; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritDoc} |
||
| 283 | * |
||
| 284 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 285 | */ |
||
| 286 | public function from(string $address, string $name = '') : self |
||
| 287 | { |
||
| 288 | $this->mailer->setFrom($address, $name); |
||
| 289 | |||
| 290 | return $this; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritDoc} |
||
| 295 | * |
||
| 296 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 297 | */ |
||
| 298 | public function header(array|string $name, ?string $value = null) : self |
||
| 299 | { |
||
| 300 | if (is_string($name)) { |
||
| 301 | $name = [$name => $value]; |
||
| 302 | } |
||
| 303 | |||
| 304 | foreach ($name As $key => $value) { |
||
| 305 | $this->mailer->addCustomHeader($key, $value); |
||
| 306 | } |
||
| 307 | |||
| 308 | return $this; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritDoc} |
||
| 313 | */ |
||
| 314 | public function html(string $content): self |
||
| 315 | { |
||
| 316 | $this->mailer->isHTML(true); |
||
| 317 | |||
| 318 | return $this->message($content); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritDoc} |
||
| 323 | */ |
||
| 324 | public function lastId(): string |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritDoc} |
||
| 331 | */ |
||
| 332 | public function message(string $message): self |
||
| 333 | { |
||
| 334 | $this->mailer->Body = $message; |
||
| 335 | |||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritDoc} |
||
| 341 | * |
||
| 342 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 343 | */ |
||
| 344 | public function replyTo(array|string $address, bool|string $name = '', bool $set = false): self |
||
| 345 | { |
||
| 346 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
| 347 | |||
| 348 | if ($set) { |
||
| 349 | $this->mailer->clearReplyTos(); |
||
| 350 | } |
||
| 351 | |||
| 352 | foreach ($addresses as $address) { |
||
| 353 | $this->mailer->addReplyTo(...$address); |
||
| 354 | } |
||
| 355 | |||
| 356 | return $this; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * {@inheritDoc} |
||
| 361 | * |
||
| 362 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 363 | */ |
||
| 364 | public function send(): bool |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * {@inheritDoc} |
||
| 371 | */ |
||
| 372 | public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): self |
||
| 373 | { |
||
| 374 | $this->mailer->sign($cert_filename, $key_filename, $key_pass, $extracerts_filename); |
||
| 375 | |||
| 376 | return $this; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritDoc} |
||
| 381 | */ |
||
| 382 | public function subject(string $subject): self |
||
| 383 | { |
||
| 384 | $this->mailer->Subject = $subject; |
||
| 385 | |||
| 386 | return $this; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritDoc} |
||
| 391 | */ |
||
| 392 | public function text(string $content): self |
||
| 393 | { |
||
| 394 | $this->mailer->isHTML(false); |
||
| 395 | |||
| 396 | return $this->message($content); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritDoc} |
||
| 401 | * |
||
| 402 | * @throws \PHPMailer\PHPMailer\Exception |
||
| 403 | */ |
||
| 404 | public function to(array|string $address, bool|string $name = '', bool $set = false): self |
||
| 417 | } |
||
| 418 | } |
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