Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

OSSMailScanner_Bind_Cron::process()   C

Complexity

Conditions 15
Paths 114

Size

Total Lines 69
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 69
rs 5.8
cc 15
nc 114
nop 0

How to fix   Long Method    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
 * Cron for scheduled import.
4
 *
5
 * @package   App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Tomasz Kur <[email protected]>
10
 */
11
12
/**
13
 * OSSMailScanner_Bind_Cron class.
14
 */
15
class OSSMailScanner_Bind_Cron extends \App\CronHandler
16
{
17
	/** {@inheritdoc} */
18
	public function process()
19
	{
20
		$dbCommand = App\Db::getInstance()->createCommand();
21
		$scannerModel = Vtiger_Record_Model::getCleanInstance('OSSMailScanner');
22
		$dataReader = (new App\Db\Query())->select([
23
			'vtiger_ossmailview.*',
24
			'roundcube_users.actions',
25
		])->from('vtiger_ossmailview')
26
			->innerJoin('roundcube_users', 'roundcube_users.user_id = vtiger_ossmailview.rc_user')->where(['vtiger_ossmailview.verify' => 1])
27
			->createCommand()->query();
28
29
		while ($row = $dataReader->read()) {
30
			$scannerModel->bindMail($row);
0 ignored issues
show
Bug introduced by
The method bindMail() does not exist on Vtiger_Record_Model. It seems like you code against a sub-type of Vtiger_Record_Model such as OSSMailScanner_Record_Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
			$scannerModel->/** @scrutinizer ignore-call */ 
31
                  bindMail($row);
Loading history...
31
			$dbCommand->update('vtiger_ossmailview', ['verify' => 0], ['ossmailviewid' => $row['ossmailviewid']])->execute();
32
		}
33
		$dataReader->close();
34
		$dataReader = (new App\Db\Query())->from('s_#__mail_relation_updater')->createCommand()->query();
35
		$bindByEmail = ['Leads', 'Accounts', 'Partners', 'Vendors', 'Competition', 'Contacts', 'OSSEmployees'];
36
		$bindByPrefix = ['Campaigns', 'HelpDesk', 'Project', 'SSalesProcesses'];
37
		while ($relationRow = $dataReader->read()) {
38
			$dbCommand->delete('vtiger_ossmailview_relation', ['crmid' => $relationRow['crmid']])->execute();
39
			$moduleName = \App\Module::getModuleName($relationRow['tabid']);
40
			$bind = false;
41
			if (\in_array($moduleName, $bindByEmail)) {
42
				$bind = 'email';
43
			}
44
			if (\in_array($moduleName, $bindByPrefix)) {
45
				$bind = 'prefix';
46
			}
47
			if (false === $bind) {
48
				continue;
49
			}
50
			$recordModel = Vtiger_Record_Model::getInstanceById($relationRow['crmid'], $moduleName);
51
			$where = [];
52
			if ('prefix' == $bind) {
53
				$recordNumber = $recordModel->getRecordNumber();
54
				if (empty($recordNumber)) {
55
					continue;
56
				}
57
				$where = ['like', 'vtiger_ossmailview.subject', "[{$recordNumber}]"];
58
			} elseif ('email' == $bind) {
59
				$where = ['or'];
60
				$fieldModels = $recordModel->getModule()->getFieldsByType('email');
61
				foreach ($fieldModels as $fieldName => $fieldModel) {
62
					if (!$recordModel->isEmpty($fieldName)) {
63
						$email = $recordModel->get($fieldName);
64
						$where[] = ['from_email' => $email];
65
						$where[] = ['to_email' => $email];
66
						$where[] = ['cc_email' => $email];
67
						$where[] = ['bcc_email' => $email];
68
					}
69
				}
70
			}
71
			if (!empty($where) && $where !== ['or']) {
72
				$dataReaderMail = (new App\Db\Query())->select(['vtiger_ossmailview.*', 'roundcube_users.actions'])
73
					->from('vtiger_ossmailview')
74
					->innerJoin('roundcube_users', 'roundcube_users.user_id = vtiger_ossmailview.rc_user')
75
					->where($where)->createCommand()->query();
76
				while ($row = $dataReaderMail->read()) {
77
					$scannerModel->bindMail($row);
78
				}
79
				$dataReaderMail->close();
80
			}
81
			$dbCommand->delete('s_#__mail_relation_updater', ['crmid' => $relationRow['crmid']])->execute();
82
			if ($this->checkTimeout()) {
83
				break;
84
			}
85
		}
86
		$dataReader->close();
87
	}
88
}
89