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

OSSMailScanner_Bind_Cron   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 55
c 1
b 0
f 0
dl 0
loc 72
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 69 15
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