| Total Complexity | 41 |
| Total Lines | 355 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MailService 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 MailService, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 55 | class MailService { |
||
| 56 | |||
| 57 | |||
| 58 | /** @var ConfigService */ |
||
| 59 | private $configService; |
||
| 60 | |||
| 61 | /** @var MiscService */ |
||
| 62 | private $miscService; |
||
| 63 | |||
| 64 | /** @var int */ |
||
| 65 | private $count = 0; |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * MailService constructor. |
||
| 70 | * |
||
| 71 | * @param ConfigService $configService |
||
| 72 | * @param MiscService $miscService |
||
| 73 | */ |
||
| 74 | function __construct(ConfigService $configService, MiscService $miscService) { |
||
| 75 | $this->configService = $configService; |
||
| 76 | $this->miscService = $miscService; |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * parse the mail content. |
||
| 82 | * |
||
| 83 | * will create a local text file containing the headers and the content of the mail for each one of |
||
| 84 | * the 'to' or 'cc' mail address correspond to a mail added using the |
||
| 85 | * "./occ files_frommail:address --add" |
||
| 86 | * |
||
| 87 | * Attachments will also be saved on the cloud in the path: |
||
| 88 | * "Mails sent to [email protected]/From [email protected]/" |
||
| 89 | * |
||
| 90 | * @param string $content |
||
| 91 | * @param string $userId |
||
| 92 | */ |
||
| 93 | public function parseMail(string $content, string $userId): void { |
||
| 94 | $mail = new Parser(); |
||
| 95 | $mail->setText($content); |
||
| 96 | |||
| 97 | $data = $this->parseMailHeaders($mail); |
||
| 98 | $data['id'] = date($this->configService->getAppValue(ConfigService::FROMMAIL_FILENAMEID)); |
||
| 99 | $data['userId'] = $userId; |
||
| 100 | |||
| 101 | $done = []; |
||
| 102 | $toAddresses = array_merge($mail->getAddresses('to'), $mail->getAddresses('cc')); |
||
| 103 | foreach ($toAddresses as $toAddress) { |
||
| 104 | $to = $toAddress['address']; |
||
| 105 | if (in_array($to, $done)) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | try { |
||
| 110 | $this->generateLocalContentFromMail($mail, $to, $data); |
||
| 111 | } catch (Exception $e) { |
||
| 112 | $this->miscService->log('could not generate LocalContent from Mail - ' . $e->getMessage()); |
||
| 113 | } |
||
| 114 | |||
| 115 | $done[] = $to; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param Parser $mail |
||
| 122 | * @param string $to |
||
| 123 | * @param array $data |
||
| 124 | * |
||
| 125 | * @throws AddressInfoException |
||
| 126 | * @throws GenericFileException |
||
| 127 | * @throws NotAFolderException |
||
| 128 | * @throws NotFoundException |
||
| 129 | * @throws NotPermittedException |
||
| 130 | * @throws LockedException |
||
| 131 | */ |
||
| 132 | private function generateLocalContentFromMail(Parser $mail, string $to, array $data): void { |
||
| 133 | $toInfo = $this->getMailAddressInfo($to); |
||
| 134 | $this->miscService->log($to . ' ' . json_encode($toInfo)); |
||
| 135 | if (empty($toInfo)) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | $text = $data['text']; |
||
| 140 | $subject = $data['subject']; |
||
| 141 | $from = $data['from']; |
||
| 142 | $userId = $data['userId']; |
||
| 143 | $id = $data['id']; |
||
| 144 | |||
| 145 | $this->verifyInfoAndPassword($text, $toInfo); |
||
| 146 | |||
| 147 | $this->count = 0; |
||
| 148 | $folder = $this->getMailFolder($userId, $to, $from); |
||
| 149 | $this->createLocalFile($folder, $id, 'mail-' . $subject . '.txt', $text); |
||
| 150 | $this->createLocalFileFromAttachments($id, $folder, $mail->getAttachments()); |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $content |
||
| 156 | * @param array $toInfo |
||
| 157 | * |
||
| 158 | * @throws AddressInfoException |
||
| 159 | */ |
||
| 160 | private function verifyInfoAndPassword(string $content, array $toInfo): void { |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $userId |
||
| 179 | * @param string $to |
||
| 180 | * @param string $from |
||
| 181 | * |
||
| 182 | * @return Folder |
||
| 183 | * @throws NotAFolderException |
||
| 184 | * @throws NotFoundException |
||
| 185 | * @throws NotPermittedException |
||
| 186 | */ |
||
| 187 | private function getMailFolder(string $userId, string $to, string $from): Folder { |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * @param Parser $mail |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | private function parseMailHeaders(Parser $mail): array { |
||
| 214 | $from = $mail->getAddresses('from')[0]['address']; |
||
| 215 | $subject = $mail->getHeader('subject'); |
||
| 216 | $text = $mail->getHeadersRaw() . $mail->getMessageBody('text'); |
||
| 217 | |||
| 218 | //TODO: check that data are enough |
||
| 219 | return [ |
||
| 220 | 'from' => $from, |
||
| 221 | 'subject' => $subject, |
||
| 222 | 'text' => $text |
||
| 223 | ]; |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $id |
||
| 229 | * @param Folder $folder |
||
| 230 | * @param Attachment[] $attachments |
||
| 231 | * |
||
| 232 | * @throws GenericFileException |
||
| 233 | * @throws NotPermittedException |
||
| 234 | * @throws LockedException |
||
| 235 | */ |
||
| 236 | private function createLocalFileFromAttachments(string $id, Folder $folder, array $attachments): void { |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param Folder $folder |
||
| 248 | * @param string $id |
||
| 249 | * @param string $filename |
||
| 250 | * @param string $content |
||
| 251 | * |
||
| 252 | * @throws NotPermittedException |
||
| 253 | * @throws GenericFileException |
||
| 254 | * @throws LockedException |
||
| 255 | */ |
||
| 256 | private function createLocalFile(Folder $folder, string $id, string $filename, string $content): void { |
||
| 257 | $new = $folder->newFile($id . '-' . $this->count . '_' . $filename); |
||
| 258 | $new->putContent($content); |
||
| 259 | |||
| 260 | $this->count++; |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $address |
||
| 266 | * @param string $password |
||
| 267 | * |
||
| 268 | * @throws UnknownAddressException |
||
| 269 | */ |
||
| 270 | public function setMailPassword(string $address, string $password): void { |
||
| 271 | if (!$this->mailAddressExist($address)) { |
||
| 272 | throw new UnknownAddressException('address is not known'); |
||
| 273 | } |
||
| 274 | |||
| 275 | $addresses = $this->getMailAddresses(); |
||
| 276 | $new = []; |
||
| 277 | foreach ($addresses as $entry) { |
||
| 278 | if ($entry['address'] === $address) { |
||
| 279 | $entry['password'] = $password; |
||
| 280 | } |
||
| 281 | $new[] = $entry; |
||
| 282 | } |
||
| 283 | |||
| 284 | $this->saveMailAddresses($new); |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $address |
||
| 290 | * |
||
| 291 | * @throws UnknownAddressException |
||
| 292 | */ |
||
| 293 | public function removeMailAddress(string $address): void { |
||
| 294 | $addresses = $this->getMailAddresses(); |
||
| 295 | if (!$this->mailAddressExist($address)) { |
||
| 296 | throw new UnknownAddressException('address is not known'); |
||
| 297 | } |
||
| 298 | |||
| 299 | $new = []; |
||
| 300 | foreach ($addresses as $entry) { |
||
| 301 | if ($entry['address'] !== $address) { |
||
| 302 | $new[] = $entry; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | $this->saveMailAddresses($new); |
||
| 307 | } |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $address |
||
| 312 | * @param string $password |
||
| 313 | * |
||
| 314 | * @throws AddressAlreadyExistException |
||
| 315 | * @throws InvalidAddressException |
||
| 316 | */ |
||
| 317 | public function addMailAddress(string $address, string $password = ''): void { |
||
| 318 | $this->hasToBeAValidMailAddress($address); |
||
| 319 | if ($this->mailAddressExist($address)) { |
||
| 320 | throw new AddressAlreadyExistException('address already exist'); |
||
| 321 | } |
||
| 322 | |||
| 323 | $addresses = $this->getMailAddresses(); |
||
| 324 | array_push($addresses, ['address' => $address, 'password' => $password]); |
||
| 325 | $this->saveMailAddresses($addresses); |
||
| 326 | } |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $address |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | private function mailAddressExist(string $address): bool { |
||
| 335 | return !empty($this->getMailAddressInfo($address)); |
||
| 336 | } |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $address |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | private function getMailAddressInfo(string $address): array { |
||
| 345 | $addresses = $this->getMailAddresses(); |
||
| 346 | foreach ($addresses as $entry) { |
||
| 347 | if ($entry['address'] === $address) { |
||
| 348 | return $entry; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | return []; |
||
| 353 | } |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $address |
||
| 358 | * |
||
| 359 | * @throws InvalidAddressException |
||
| 360 | */ |
||
| 361 | private function hasToBeAValidMailAddress(string $address): void { |
||
| 362 | if (filter_var($address, FILTER_VALIDATE_EMAIL)) { |
||
| 363 | return; |
||
| 364 | } |
||
| 365 | |||
| 366 | throw new InvalidAddressException('this mail address is not valid'); |
||
| 367 | } |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function getMailAddresses(): array { |
||
| 374 | $curr = json_decode($this->configService->getAppValue(ConfigService::FROMMAIL_ADDRESSES), true); |
||
| 375 | if ($curr === null) { |
||
| 376 | return []; |
||
| 377 | } |
||
| 378 | |||
| 379 | return $curr; |
||
| 380 | } |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * @param array $addresses |
||
| 385 | */ |
||
| 386 | private function saveMailAddresses(array $addresses): void { |
||
| 387 | $this->configService->setAppValue(ConfigService::FROMMAIL_ADDRESSES, json_encode($addresses)); |
||
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $address |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | private function parseMailAddress(string $address): string { |
||
| 410 | } |
||
| 411 | |||
| 412 | } |
||
| 413 | |||
| 414 |
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