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

OSSMail_MailActionBar_View::process()   C

Complexity

Conditions 14
Paths 176

Size

Total Lines 41
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 41
rs 5.6333
c 0
b 0
f 0
cc 14
nc 176
nop 1

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
/**
4
 * Mail cction bar class.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
8
 * @author    Mariusz Krzaczkowski <[email protected]>
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
class OSSMail_MailActionBar_View extends Vtiger_Index_View
12
{
13
	use App\Controller\ClearProcess;
14
15
	/** {@inheritdoc} */
16
	public function process(App\Request $request)
17
	{
18
		$moduleName = $request->getModule();
19
		$uid = $request->getInteger('uid');
20
		$account = OSSMail_Record_Model::getAccountByHash($request->getForSql('rcId'));
21
		if (!$account || !\App\Record::isExists($account['crm_ma_id'], 'MailAccount') || !\App\Privilege::isPermitted('MailAccount', 'DetailView', $account['crm_ma_id'])) {
22
			throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED', 406);
23
		}
24
		$mailAccount = \App\Mail\Account::getInstanceById($account['crm_ma_id']);
25
		try {
26
			$mailViewModel = OSSMailView_Record_Model::getCleanInstance('OSSMailView');
27
			$folderDecode = \App\Utils::convertCharacterEncoding($request->getRaw('folder'), 'UTF7-IMAP', 'UTF-8');
28
			$folderDecode = \App\Purifier::purifyByType($folderDecode, 'Text');
29
			$folderDecode = \App\Purifier::decodeHtml($folderDecode);
30
			$imap = $mailAccount->openImap();
31
			$message = $imap->getMessageByUid($folderDecode, $uid);
32
			$record = $message ? $message->getMailCrmIdByCid() : 0;
33
			if (!$record && \in_array('CreatedMail', $mailAccount->getActions()) && \in_array($folderDecode, $mailAccount->getFolders())) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
34
				$scanner = (new \App\Mail\Scanner())->setLimit(1);
35
				foreach ($mailAccount->getActions() as $action) {
36
					$scanner->getAction($action)->setAccount($mailAccount)->setMessage($message)->process();
37
				}
38
				$record = (int) $message->getProcessData('CreatedMail')['mailViewId'] ?? 0;
39
			} elseif ($record && !\App\Privilege::isPermitted('OSSMailView', 'DetailView', $record)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
40
				$recordModel = Vtiger_Record_Model::getInstanceById($record, $mailViewModel->getModule());
0 ignored issues
show
Bug introduced by
$mailViewModel->getModule() of type Vtiger_Module_Model is incompatible with the type string expected by parameter $module of Vtiger_Record_Model::getInstanceById(). ( Ignorable by Annotation )

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

40
				$recordModel = Vtiger_Record_Model::getInstanceById($record, /** @scrutinizer ignore-type */ $mailViewModel->getModule());
Loading history...
41
				$sharedOwner = $recordModel->isEmpty('shownerid') ? [] : explode(',', $recordModel->get('shownerid'));
42
				$sharedOwner[] = \App\User::getCurrentUserId();
43
				$recordModel->set('shownerid', implode(',', $sharedOwner))->save();
44
			}
45
			$viewer = $this->getViewer($request);
46
			$viewer->assign('RECORD', $record);
47
			if ($record) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
48
				$relatedRecords = $mailViewModel->getRelatedRecords($record);
49
				$viewer->assign('RELATED_RECORDS', $relatedRecords);
50
			}
51
			\App\ModuleHierarchy::getModulesByLevel(0);
52
			$viewer->assign('MODULE_NAME', $moduleName);
53
			$viewer->assign('URL', App\Config::main('site_URL'));
54
			$viewer->view('MailActionBar.tpl', $moduleName);
55
		} catch (\Throwable $e) {
56
			\App\Log::error($e->getMessage() . PHP_EOL . $e->__toString(), 'OSSMail');
57
		}
58
	}
59
}
60