| Total Complexity | 44 |
| Total Lines | 178 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Outlook 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 Outlook, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Outlook extends Base |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Scanner engine name. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $name = 'Outlook'; |
||
| 26 | |||
| 27 | /** {@inheritdoc} */ |
||
| 28 | public function getActions(): array |
||
| 29 | { |
||
| 30 | return array_filter(explode(',', \App\User::getCurrentUserModel()->getDetail('mail_scanner_actions'))); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** {@inheritdoc} */ |
||
| 34 | public function getMailCrmId() |
||
| 35 | { |
||
| 36 | if ($this->has('mailCrmId')) { |
||
| 37 | return $this->get('mailCrmId'); |
||
| 38 | } |
||
| 39 | $mailCrmId = \App\Mail\Message::findByCid($this->getCid()); |
||
| 40 | $this->set('mailCrmId', $mailCrmId); |
||
| 41 | return $mailCrmId; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** {@inheritdoc} */ |
||
| 45 | public function getMailType(): int |
||
| 46 | { |
||
| 47 | if ($this->has('mailType')) { |
||
| 48 | return $this->get('mailType'); |
||
|
|
|||
| 49 | } |
||
| 50 | $to = false; |
||
| 51 | $from = (bool) \App\Mail\RecordFinder::findUserEmail([$this->get('from_email')]); |
||
| 52 | if ($this->has('to_email')) { |
||
| 53 | $to = (bool) \App\Mail\RecordFinder::findUserEmail($this->get('to_email')); |
||
| 54 | } elseif ($this->has('cc_email')) { |
||
| 55 | $to = (bool) \App\Mail\RecordFinder::findUserEmail($this->get('cc_email')); |
||
| 56 | } elseif ($this->has('bcc_email')) { |
||
| 57 | $to = (bool) \App\Mail\RecordFinder::findUserEmail($this->get('bcc_email')); |
||
| 58 | } |
||
| 59 | $key = self::MAIL_TYPE_RECEIVED; |
||
| 60 | if ($from && $to) { |
||
| 61 | $key = self::MAIL_TYPE_INTERNAL; |
||
| 62 | } elseif ($from) { |
||
| 63 | $key = self::MAIL_TYPE_SENT; |
||
| 64 | } |
||
| 65 | $this->set('mailType', $key); |
||
| 66 | return $key; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** {@inheritdoc} */ |
||
| 70 | public function getUserId(): int |
||
| 71 | { |
||
| 72 | return \App\User::getCurrentUserRealId(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initialize with request data. |
||
| 77 | * |
||
| 78 | * @param \App\Request $request |
||
| 79 | * |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function initFromRequest(\App\Request $request) |
||
| 83 | { |
||
| 84 | $this->set('subject', $request->isEmpty('mailSubject') ? '-' : \App\TextUtils::textTruncate($request->getByType('mailSubject', 'Text'), 65535, false)); |
||
| 85 | $this->set('headers', $request->isEmpty('mailHeaders') ? '' : \App\TextUtils::textTruncate($request->getRaw('mailHeaders'), 16777215, false)); |
||
| 86 | $this->set('from_email', $request->getByType('mailFrom', 'Email')); |
||
| 87 | $this->set('date', $request->getByType('mailDateTimeCreated', 'DateTimeInIsoFormat')); |
||
| 88 | $this->set('message_id', $request->getByType('mailMessageId', 'MailId')); |
||
| 89 | if (!$request->isEmpty('mailTo')) { |
||
| 90 | $this->set('to_email', $request->getArray('mailTo', 'Email')); |
||
| 91 | } |
||
| 92 | if (!$request->isEmpty('mailCc', true)) { |
||
| 93 | $this->set('cc_email', $request->getArray('mailCc', 'Email')); |
||
| 94 | } |
||
| 95 | if (!$request->isEmpty('mailBcc', true)) { |
||
| 96 | $this->set('bcc_email', $request->getArray('mailBcc', 'Email')); |
||
| 97 | } |
||
| 98 | if (!$request->isEmpty('mailBody', true)) { |
||
| 99 | $this->set('body', $request->getForHtml('mailBody')); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** {@inheritdoc} */ |
||
| 104 | public function findRelatedRecords(bool $onlyId = false): array |
||
| 120 | } |
||
| 121 | |||
| 122 | /** {@inheritdoc} */ |
||
| 123 | public function findRelatedRecordsByEmail(): array |
||
| 124 | { |
||
| 125 | if (isset($this->processData['findByEmail'])) { |
||
| 126 | return $this->processData['findByEmail']; |
||
| 127 | } |
||
| 128 | $emails = $this->get('to_email'); |
||
| 129 | $emails[] = $this->get('from_email'); |
||
| 130 | if ($this->has('cc_email')) { |
||
| 131 | $emails = array_merge($emails, $this->get('cc_email')); |
||
| 132 | } |
||
| 133 | if ($this->has('bcc_email')) { |
||
| 134 | $emails = array_merge($emails, $this->get('bcc_email')); |
||
| 135 | } |
||
| 136 | return $this->processData['findByEmail'] = \App\Utils::flatten(\App\Mail\RecordFinder::findByEmail($emails, $this->getEmailsFields())); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** {@inheritdoc} */ |
||
| 140 | public function findRelatedRecordsBySubject(): array |
||
| 146 | } |
||
| 147 | |||
| 148 | /** {@inheritdoc} */ |
||
| 149 | public function getEmailsFields(?string $searchModuleName = null): array |
||
| 150 | { |
||
| 151 | $cacheKey = $searchModuleName ?? '-'; |
||
| 152 | if (isset($this->emailsFieldsCache[$cacheKey])) { |
||
| 153 | return $this->emailsFieldsCache[$cacheKey]; |
||
| 154 | } |
||
| 155 | $user = \App\User::getCurrentUserModel(); |
||
| 156 | $fields = []; |
||
| 157 | if ($mailScannerFields = $user->getDetail('mail_scanner_fields')) { |
||
| 158 | foreach (explode(',', trim($mailScannerFields, ',')) as $field) { |
||
| 159 | $field = explode('|', $field); |
||
| 160 | if (($searchModuleName && $searchModuleName !== $field[1]) || !\in_array($field[3], [13, 319])) { |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | $fields[$field[1]][$field[3]][] = $field[2]; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | $this->emailsFieldsCache[$cacheKey] = $fields; |
||
| 167 | return $fields; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** {@inheritdoc} */ |
||
| 171 | public function getNumberFields(?string $searchModuleName = null): array |
||
| 190 | } |
||
| 191 | |||
| 192 | /** {@inheritdoc} */ |
||
| 193 | public function getExceptions(): array |
||
| 194 | { |
||
| 196 | } |
||
| 197 | } |
||
| 198 |