Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

MailAccount_Verification_Cron::process()   B

Complexity

Conditions 10
Paths 68

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 35
rs 7.6666
c 1
b 0
f 0
cc 10
nc 68
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Mail account verification cron file.
4
 *
5
 * @package   Cron
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
12
/**
13
 * Mail account verification cron class.
14
 */
15
class MailAccount_Verification_Cron extends \App\CronHandler
16
{
17
	/** {@inheritdoc} */
18
	public function process()
19
	{
20
		$pauser = \App\Pauser::getInstance('MailAccountVerification');
21
		$lastId = (int) $pauser->getValue();
22
		$deactivationTime = \App\Mail::getConfig('scanner', 'deactivation_time');
23
24
		$queryGenerator = (new \App\QueryGenerator('MailAccount'))
25
			->setFields(['id'])
26
			->addCondition('mailaccount_status', \App\Mail\Account::STATUS_LOCKED, 'e');
27
		if ($lastId) {
28
			$queryGenerator->addCondition('id', $lastId, 'a');
29
		}
30
		$dataReader = $queryGenerator->createQuery()->createCommand()->query();
31
		$count = $dataReader->count();
32
		while ($recordId = $dataReader->readColumn(0)) {
33
			$pauser->setValue((string) $recordId);
34
			$mailAccount = \App\Mail\Account::getInstanceById($recordId);
35
			try {
36
				$mailbox = $mailAccount->openImap();
37
				if ($mailbox->isConnected()) {
38
					$mailAccount->unlock();
39
				}
40
			} catch (\Throwable $th) {
41
				$lastLogin = $mailAccount->getSource()->get('last_login') ?: $mailAccount->getSource()->get('createdtime');
42
				$hours = \App\Fields\DateTime::getDiff($lastLogin, date('Y-m-d H:i:s'), 'hours');
43
				if ((int) $hours >= (int) $deactivationTime) {
44
					$mailAccount->deactivate($th->getMessage());
45
				}
46
			}
47
			if ($this->checkTimeout()) {
48
				break;
49
			}
50
		}
51
		if (!$lastId || !$count) {
52
			$pauser->destroy();
53
		}
54
	}
55
}
56