| Total Complexity | 42 |
| Total Lines | 316 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Account 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 Account, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Account extends \App\Base |
||
| 18 | { |
||
| 19 | /** @var string Mailbox Status: Active */ |
||
| 20 | public const STATUS_ACTIVE = 'PLL_ACTIVE'; |
||
| 21 | /** @var string Mailbox Status: Inactive */ |
||
| 22 | public const STATUS_INACTIVE = 'PL_INACTIVE'; |
||
| 23 | /** @var string Mailbox Status: Locked */ |
||
| 24 | public const STATUS_LOCKED = 'PLL_LOCKED'; |
||
| 25 | |||
| 26 | /** @var string Base module name */ |
||
| 27 | public const MODULE_NAME = 'MailAccount'; |
||
| 28 | /** @var \App\Mail\Server */ |
||
| 29 | private $server; |
||
| 30 | /** @var \Vtiger_Record_Model */ |
||
| 31 | private $source; |
||
| 32 | /** @var \App\Integrations\OAuth\AbstractProvider OAuth2 provider */ |
||
| 33 | private $provider; |
||
| 34 | /** @var string */ |
||
| 35 | private $password; |
||
| 36 | /** @var string */ |
||
| 37 | private $userName; |
||
| 38 | /** @var string */ |
||
| 39 | private $refreshToken; |
||
| 40 | /** @var string Date time */ |
||
| 41 | private $expireTime; |
||
| 42 | /** @var string */ |
||
| 43 | private $redirectUri; |
||
| 44 | /** |
||
| 45 | * List of scopes that will be used for authentication. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $scopes; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | private $attempt = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get instance by ID. |
||
| 56 | * |
||
| 57 | * @param int $id |
||
| 58 | */ |
||
| 59 | public static function getInstanceById(int $id): ?self |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getSource() |
||
| 76 | { |
||
| 77 | return $this->source; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getPassword() |
||
| 81 | { |
||
| 82 | return $this->password; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getLogin() |
||
| 86 | { |
||
| 87 | return $this->userName; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Mail server instance. |
||
| 92 | * |
||
| 93 | * @return \App\Mail\Server |
||
| 94 | */ |
||
| 95 | public function getServer(): Server |
||
| 96 | { |
||
| 97 | return $this->server; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getRefreshToken() |
||
| 101 | { |
||
| 102 | return $this->refreshToken; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function update(array $fields = []) |
||
| 106 | { |
||
| 107 | if (!$fields) { |
||
| 108 | $fields = ['password', 'refresh_token', 'expire_time']; |
||
| 109 | } |
||
| 110 | foreach ($fields as $fieldName) { |
||
| 111 | $fieldModel = $this->source->getField($fieldName); |
||
| 112 | $value = null; |
||
| 113 | switch ($fieldName) { |
||
| 114 | case 'password': |
||
| 115 | $value = $fieldModel->getDBValue($this->password); |
||
| 116 | break; |
||
| 117 | case 'refresh_token': |
||
| 118 | if (!$this->refreshToken) { |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | $value = $fieldModel->getDBValue($this->refreshToken); |
||
| 122 | break; |
||
| 123 | case 'expire_time': |
||
| 124 | if (!$this->expireTime) { |
||
| 125 | break; |
||
| 126 | } |
||
| 127 | $value = $this->expireTime; |
||
| 128 | break; |
||
| 129 | case 'last_login': |
||
| 130 | $value = date('Y-m-d H:i:s'); |
||
| 131 | break; |
||
| 132 | default: |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | if (null !== $value) { |
||
| 136 | $this->source->set($fieldModel->getName(), $value)->setDataForSave([$fieldModel->getTableName() => [$fieldModel->getColumnName() => $value]]); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | if ($this->source->getPreviousValue()) { |
||
| 140 | $this->source->save(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Requests an access token using a specified option set. |
||
| 146 | * |
||
| 147 | * @param array $options |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getAccessToken(array $options = []) |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get OAuth provider. |
||
| 174 | * |
||
| 175 | * @return \App\Integrations\OAuth\AbstractProvider |
||
| 176 | */ |
||
| 177 | public function getOAuthProvider(): \App\Integrations\OAuth\AbstractProvider |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check if mail account is active. |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function isActive(): bool |
||
| 198 | { |
||
| 199 | return 'PLL_ACTIVE' === $this->getSource()->get('mailaccount_status'); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Open imap connection. |
||
| 204 | * |
||
| 205 | * @return Connections\Imap |
||
| 206 | */ |
||
| 207 | public function openImap(): Connections\Imap |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Lock mail account. |
||
| 238 | * |
||
| 239 | * @param string $messages |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function lock(string $messages) |
||
| 244 | { |
||
| 245 | $fieldModel = $this->source->getField('logs'); |
||
| 246 | |||
| 247 | $this->source->set('mailaccount_status', self::STATUS_LOCKED); |
||
| 248 | $messages = \App\Purifier::decodeHtml(\App\Purifier::encodeHtml($messages)); |
||
| 249 | $this->source->set('logs', \App\TextUtils::textTruncate($messages, $fieldModel->getMaxValue(), true, true))->save(); |
||
| 250 | |||
| 251 | return $this; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Unlock mail account. |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function unlock() |
||
| 260 | { |
||
| 261 | $this->source->set('mailaccount_status', self::STATUS_ACTIVE); |
||
| 262 | $this->source->set('logs', '')->save(); |
||
| 263 | |||
| 264 | return $this; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Deactiveate mail account. |
||
| 269 | * |
||
| 270 | * @param string|null $messages |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function deactivate(?string $messages = null) |
||
| 275 | { |
||
| 276 | if (null !== $messages) { |
||
| 277 | $fieldModel = $this->source->getField('logs'); |
||
| 278 | $messages = \App\Purifier::decodeHtml(\App\Purifier::encodeHtml($messages)); |
||
| 279 | $this->source->set('logs', \App\TextUtils::textTruncate($messages, $fieldModel->getMaxValue(), true, true)); |
||
| 280 | } |
||
| 281 | $this->source->set('mailaccount_status', self::STATUS_INACTIVE)->save(); |
||
| 282 | |||
| 283 | return $this; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get last UID. |
||
| 288 | * |
||
| 289 | * @param string $folderName |
||
| 290 | * |
||
| 291 | * @return int|null |
||
| 292 | */ |
||
| 293 | public function getLastUid(string $folderName) |
||
| 294 | { |
||
| 295 | return (new \App\Db\Query())->select(['uid'])->from(Scanner::FOLDER_TABLE) |
||
| 296 | ->where(['user_id' => $this->source->getId(), 'name' => $folderName])->scalar(); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set UID to folder. |
||
| 301 | * |
||
| 302 | * @param int $uid |
||
| 303 | * @param string $folderName |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function setLastUid(int $uid, string $folderName): bool |
||
| 308 | { |
||
| 309 | return (bool) \App\Db::getInstance()->createCommand() |
||
| 310 | ->update(Scanner::FOLDER_TABLE, ['uid' => $uid], ['user_id' => $this->source->getId(), 'name' => $folderName])->execute(); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get actions. |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getActions(): array |
||
| 319 | { |
||
| 320 | $actions = $this->getSource()->get('scanner_actions'); |
||
| 321 | return $actions ? explode(',', $actions) : []; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get folders. |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function getFolders(): array |
||
| 333 | } |
||
| 334 | } |
||
| 335 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.