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

CreatedHelpDesk   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 59
dl 0
loc 95
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadServiceContracts() 0 21 4
C process() 0 58 12
1
<?php
2
/**
3
 * Base mail scanner action file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace App\Mail\ScannerAction;
14
15
use App\Mail\RecordFinder;
16
17
/**
18
 * Base mail scanner action class.
19
 */
20
class CreatedHelpDesk extends CreatedMail
21
{
22
	/** {@inheritdoc} */
23
	public static $priority = 5;
24
25
	/** {@inheritdoc} */
26
	public function process(): void
27
	{
28
		if ($this->checkExceptions() || ($prefix = RecordFinder::getRecordNumberFromString($this->message->getSubject(), 'HelpDesk')) && \App\Record::getIdByRecordNumber($prefix, 'HelpDesk')) {
29
			return;
30
		}
31
		$owner = $this->account->getSource()->get('assigned_user_id');
32
		$fromEmail = $this->message->getEmail('from');
33
		$contactId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $this->getEmailsFields('Contacts'))));
34
		$parentId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $this->getEmailsFields('Accounts'))));
35
		if (!$parentId) {
36
			$parentId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $this->getEmailsFields('Vendors'))));
37
		}
38
		if (!$parentId && $contactId) {
39
			$parentId = \App\Record::getParentRecord($contactId, 'Contacts');
40
		}
41
		$recordModel = \Vtiger_Record_Model::getCleanInstance('HelpDesk');
42
		$this->loadServiceContracts($recordModel, $parentId);
43
		$recordModel->set('assigned_user_id', $owner);
44
		$recordModel->set('created_user_id', \App\User::getCurrentUserRealId());
45
		$recordModel->setFromUserValue('ticket_title', \App\TextUtils::textTruncate($this->message->getSubject(), $recordModel->getField('ticket_title')->getMaxValue(), false));
46
47
		$mailId = $this->message->getMailCrmId($this->account->getSource()->getId());
48
		$this->message->getBody();
49
		if ($mailId) {
50
			$this->attachments = $this->message->processData['CreatedMail']['attachments'] ?? (new \App\Db\Query())->select(['crmid' => 'documentsid'])->from('vtiger_ossmailview_files')->where(['ossmailviewid' => $mailId])->all();
0 ignored issues
show
Bug Best Practice introduced by
The property attachments does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
		} elseif ($this->message->hasAttachments()) {
52
			$this->message->saveAttachments([
53
				'assigned_user_id' => $owner,
54
				'modifiedby' => $owner,
55
			]);
56
		}
57
		$recordModel->set('description', \App\TextUtils::htmlTruncate($this->message->getBody(true), $recordModel->getField('description')->getMaxValue()));
58
		$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...
59
60
		if ($contactId) {
61
			$recordModel->ext['relations'][] = [
62
				'relatedModule' => 'Contacts',
63
				'relatedRecords' => [$contactId],
64
			];
65
		}
66
		if ($mailId) {
67
			$recordModel->ext['relations'][] = [
68
				'reverse' => true,
69
				'relatedModule' => 'OSSMailView',
70
				'relatedRecords' => [$mailId],
71
				'params' => $this->message->getDate(),
72
			];
73
		}
74
75
		foreach ($this->attachments as $file) {
76
			$recordModel->ext['relations'][] = [
77
				'relatedModule' => 'Documents',
78
				'relatedRecords' => [$file['crmid']],
79
			];
80
		}
81
82
		$recordModel->save();
83
		$this->message->setProcessData($this->getName(), ['crmid' => $recordModel->getId()]);
84
	}
85
86
	/**
87
	 * Find service contracts and init data.
88
	 *
89
	 * @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...
90
	 * @param int|bool            $parentId
91
	 *
92
	 * @return void
93
	 */
94
	private function loadServiceContracts(\Vtiger_Record_Model $recordModel, $parentId)
95
	{
96
		if (!$parentId) {
97
			return;
98
		}
99
		$recordModel->set('parent_id', $parentId);
100
		$queryGenerator = new \App\QueryGenerator('ServiceContracts');
101
		$queryGenerator->setFields(['id', 'contract_priority']);
102
		$queryGenerator->addNativeCondition(['vtiger_servicecontracts.sc_related_to' => $parentId]);
103
		$queryGenerator->permissions = false;
104
		$queryGenerator->addCondition('contract_status', 'In Progress', 'e');
105
		$dataReader = $queryGenerator->createQuery()->createCommand()->query();
106
		if (1 === $dataReader->count()) {
107
			$serviceContracts = $dataReader->read();
108
			$recordModel->set('servicecontractsid', $serviceContracts['id']);
109
			if (\App\Fields\Picklist::isExists('ticketpriorities', $serviceContracts['contract_priority'])) {
110
				$recordModel->set('ticketpriorities', $serviceContracts['contract_priority']);
111
			}
112
		}
113
		$dataReader->close();
114
		unset($dataReader, $queryGenerator);
115
	}
116
}
117