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

CreatedHelpDesk::process()   B

Complexity

Conditions 11
Paths 50

Size

Total Lines 52
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 52
rs 7.3166
c 0
b 0
f 0
cc 11
nc 50
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
 * Base mail scanner action file.
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    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App\Mail\ScannerAction;
13
14
use App\Mail\RecordFinder;
15
16
/**
17
 * Base mail scanner action class.
18
 */
19
class CreatedHelpDesk extends Base
20
{
21
	/** {@inheritdoc} */
22
	public static $priority = 5;
23
24
	/** {@inheritdoc} */
25
	public function process(): void
26
	{
27
		if ($this->checkExceptions('CreatedHelpDesk')) {
28
			return;
29
		}
30
		$scanner = $this->scannerEngine;
31
		if (($prefix = RecordFinder::getRecordNumberFromString($scanner->get('subject'), 'HelpDesk')) && \App\Record::getIdByRecordNumber($prefix, 'HelpDesk')) {
32
			return;
33
		}
34
		$fromEmail = [$scanner->get('from_email')];
35
		$contactId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $scanner->getEmailsFields('Contacts'))));
36
		$parentId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $scanner->getEmailsFields('Accounts'))));
37
		if (!$parentId) {
38
			$parentId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $scanner->getEmailsFields('Vendors'))));
39
		}
40
		if (!$parentId && $contactId) {
41
			$parentId = \App\Record::getParentRecord($contactId, 'Contacts');
42
		}
43
		$recordModel = \Vtiger_Record_Model::getCleanInstance('HelpDesk');
44
		$this->loadServiceContracts($recordModel, $parentId);
45
		$recordModel->set('assigned_user_id', $scanner->getUserId());
46
		$recordModel->set('created_user_id', $scanner->getUserId());
47
		$recordModel->set('createdtime', $scanner->get('date'));
48
		$recordModel->setFromUserValue('ticket_title', \App\TextUtils::textTruncate($scanner->get('subject'), $recordModel->getField('ticket_title')->getMaxValue(), false));
49
		$recordModel->set('description', \App\TextUtils::htmlTruncate($scanner->get('body'), $recordModel->getField('description')->getMaxValue()));
50
		$recordModel->set('ticketstatus', \Config\Modules\OSSMailScanner::$helpdeskCreateDefaultStatus);
0 ignored issues
show
Bug introduced by
The type Config\Modules\OSSMailScanner was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
		if ($contactId) {
52
			$recordModel->ext['relations'][] = [
53
				'relatedModule' => 'Contacts',
54
				'relatedRecords' => [$contactId],
55
			];
56
		}
57
		if ($mailId = $scanner->getMailCrmId()) {
58
			$recordModel->ext['relations'][] = [
59
				'reverse' => true,
60
				'relatedModule' => 'OSSMailView',
61
				'relatedRecords' => [$mailId],
62
				'params' => $scanner->get('date'),
63
			];
64
		}
65
		$recordModel->save();
66
		$id = $recordModel->getId();
67
		$scanner->processData['CreatedHelpDesk'] = $id;
68
		if ($mailId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mailId of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69
			$dbCommand = \App\Db::getInstance()->createCommand();
70
			$query = (new \App\Db\Query())->select(['documentsid'])->from('vtiger_ossmailview_files')->where(['ossmailviewid' => $mailId]);
71
			$dataReader = $query->createCommand()->query();
72
			while ($documentId = $dataReader->readColumn(0)) {
73
				$dbCommand->insert('vtiger_senotesrel', ['crmid' => $id, 'notesid' => $documentId])->execute();
74
			}
75
			$dataReader->close();
76
			unset($dataReader,$query, $dbCommand, $recordModel);
77
		}
78
	}
79
80
	/**
81
	 * Find service contracts and init data.
82
	 *
83
	 * @param Vtiger_Record_Model $recordModel
0 ignored issues
show
Bug introduced by
The type App\Mail\ScannerAction\Vtiger_Record_Model was not found. Did you mean Vtiger_Record_Model? If so, make sure to prefix the type with \.
Loading history...
84
	 * @param int|bool            $parentId
85
	 *
86
	 * @return void
87
	 */
88
	private function loadServiceContracts(\Vtiger_Record_Model $recordModel, $parentId)
89
	{
90
		if (!$parentId) {
91
			return;
92
		}
93
		$recordModel->set('parent_id', $parentId);
94
		$queryGenerator = new \App\QueryGenerator('ServiceContracts');
95
		$queryGenerator->setFields(['id', 'contract_priority']);
96
		$queryGenerator->addNativeCondition(['vtiger_servicecontracts.sc_related_to' => $parentId]);
97
		$queryGenerator->permissions = false;
98
		$queryGenerator->addCondition('contract_status', 'In Progress', 'e');
99
		$dataReader = $queryGenerator->createQuery()->createCommand()->query();
100
		if (1 === $dataReader->count()) {
101
			$serviceContracts = $dataReader->read();
102
			$recordModel->set('servicecontractsid', $serviceContracts['id']);
103
			if (\App\Fields\Picklist::isExists('ticketpriorities', $serviceContracts['contract_priority'])) {
104
				$recordModel->set('ticketpriorities', $serviceContracts['contract_priority']);
105
			}
106
		}
107
		$dataReader->close();
108
		unset($dataReader, $queryGenerator);
109
	}
110
}
111