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

Outlook::getActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Mail outlook message 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\Message;
14
15
/**
16
 * Mail outlook message class.
17
 */
18
class Outlook extends Base
19
{
20
	/**
21
	 * Scanner engine name.
22
	 *
23
	 * @var string
24
	 */
25
	public $name = 'Outlook';
26
27
	/** {@inheritdoc} */
28
	public function getActions(): array
29
	{
30
		return array_filter(explode(',', \App\User::getCurrentUserModel()->getDetail('mail_scanner_actions')));
31
	}
32
33
	/** {@inheritdoc} */
34
	public function getMailCrmId()
35
	{
36
		if ($this->has('mailCrmId')) {
37
			return $this->get('mailCrmId');
38
		}
39
		$mailCrmId = \App\Mail\Message::findByCid($this->getCid());
40
		$this->set('mailCrmId', $mailCrmId);
41
		return $mailCrmId;
42
	}
43
44
	/** {@inheritdoc} */
45
	public function getMailType(): int
46
	{
47
		if ($this->has('mailType')) {
48
			return $this->get('mailType');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('mailType') could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
49
		}
50
		$to = false;
51
		$from = (bool) \App\Mail\RecordFinder::findUserEmail([$this->get('from_email')]);
52
		if ($this->has('to_email')) {
53
			$to = (bool) \App\Mail\RecordFinder::findUserEmail($this->get('to_email'));
54
		} elseif ($this->has('cc_email')) {
55
			$to = (bool) \App\Mail\RecordFinder::findUserEmail($this->get('cc_email'));
56
		} elseif ($this->has('bcc_email')) {
57
			$to = (bool) \App\Mail\RecordFinder::findUserEmail($this->get('bcc_email'));
58
		}
59
		$key = self::MAIL_TYPE_RECEIVED;
60
		if ($from && $to) {
61
			$key = self::MAIL_TYPE_INTERNAL;
62
		} elseif ($from) {
63
			$key = self::MAIL_TYPE_SENT;
64
		}
65
		$this->set('mailType', $key);
66
		return $key;
67
	}
68
69
	/** {@inheritdoc} */
70
	public function getUserId(): int
71
	{
72
		return \App\User::getCurrentUserRealId();
73
	}
74
75
	/**
76
	 * Initialize with request data.
77
	 *
78
	 * @param \App\Request $request
79
	 *
80
	 * @return void
81
	 */
82
	public function initFromRequest(\App\Request $request)
83
	{
84
		$this->set('subject', $request->isEmpty('mailSubject') ? '-' : \App\TextUtils::textTruncate($request->getByType('mailSubject', 'Text'), 65535, false));
85
		$this->set('headers', $request->isEmpty('mailHeaders') ? '' : \App\TextUtils::textTruncate($request->getRaw('mailHeaders'), 16777215, false));
86
		$this->set('from_email', $request->getByType('mailFrom', 'Email'));
87
		$this->set('date', $request->getByType('mailDateTimeCreated', 'DateTimeInIsoFormat'));
88
		$this->set('message_id', $request->getByType('mailMessageId', 'MailId'));
89
		if (!$request->isEmpty('mailTo')) {
90
			$this->set('to_email', $request->getArray('mailTo', 'Email'));
91
		}
92
		if (!$request->isEmpty('mailCc', true)) {
93
			$this->set('cc_email', $request->getArray('mailCc', 'Email'));
94
		}
95
		if (!$request->isEmpty('mailBcc', true)) {
96
			$this->set('bcc_email', $request->getArray('mailBcc', 'Email'));
97
		}
98
		if (!$request->isEmpty('mailBody', true)) {
99
			$this->set('body', $request->getForHtml('mailBody'));
100
		}
101
	}
102
103
	/** {@inheritdoc} */
104
	public function findRelatedRecords(bool $onlyId = false): array
105
	{
106
		$ids = $this->findRelatedRecordsByEmail();
107
		if ($idsBySubject = $this->findRelatedRecordsBySubject()) {
108
			$ids[] = current($idsBySubject);
109
		}
110
		if (!$onlyId) {
111
			foreach ($ids as &$id) {
112
				$id = [
113
					'id' => $id,
114
					'module' => \App\Record::getType($id),
115
					'label' => \App\Record::getLabel($id),
116
				];
117
			}
118
		}
119
		return $ids;
120
	}
121
122
	/** {@inheritdoc} */
123
	public function findRelatedRecordsByEmail(): array
124
	{
125
		if (isset($this->processData['findByEmail'])) {
126
			return $this->processData['findByEmail'];
127
		}
128
		$emails = $this->get('to_email');
129
		$emails[] = $this->get('from_email');
130
		if ($this->has('cc_email')) {
131
			$emails = array_merge($emails, $this->get('cc_email'));
132
		}
133
		if ($this->has('bcc_email')) {
134
			$emails = array_merge($emails, $this->get('bcc_email'));
135
		}
136
		return $this->processData['findByEmail'] = \App\Utils::flatten(\App\Mail\RecordFinder::findByEmail($emails, $this->getEmailsFields()));
137
	}
138
139
	/** {@inheritdoc} */
140
	public function findRelatedRecordsBySubject(): array
141
	{
142
		if (isset($this->processData['findBySubject'])) {
143
			return $this->processData['findBySubject'];
144
		}
145
		return $this->processData['findBySubject'] = \App\Mail\RecordFinder::findBySubject($this->get('subject'), $this->getNumberFields());
146
	}
147
148
	/** {@inheritdoc} */
149
	public function getEmailsFields(?string $searchModuleName = null): array
150
	{
151
		$cacheKey = $searchModuleName ?? '-';
152
		if (isset($this->emailsFieldsCache[$cacheKey])) {
153
			return $this->emailsFieldsCache[$cacheKey];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->emailsFieldsCache[$cacheKey] returns the type string which is incompatible with the type-hinted return array.
Loading history...
154
		}
155
		$user = \App\User::getCurrentUserModel();
156
		$fields = [];
157
		if ($mailScannerFields = $user->getDetail('mail_scanner_fields')) {
158
			foreach (explode(',', trim($mailScannerFields, ',')) as $field) {
159
				$field = explode('|', $field);
160
				if (($searchModuleName && $searchModuleName !== $field[1]) || !\in_array($field[3], [13, 319])) {
161
					continue;
162
				}
163
				$fields[$field[1]][$field[3]][] = $field[2];
164
			}
165
		}
166
		$this->emailsFieldsCache[$cacheKey] = $fields;
167
		return $fields;
168
	}
169
170
	/** {@inheritdoc} */
171
	public function getNumberFields(?string $searchModuleName = null): array
172
	{
173
		$cacheKey = $searchModuleName ?? '-';
174
		if (isset($this->numberFieldsCache[$cacheKey])) {
175
			return $this->numberFieldsCache[$cacheKey];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->numberFieldsCache[$cacheKey] returns the type string which is incompatible with the type-hinted return array.
Loading history...
176
		}
177
		$user = \App\User::getCurrentUserModel();
178
		$fields = [];
179
		if ($mailScannerFields = $user->getDetail('mail_scanner_fields')) {
180
			foreach (explode(',', trim($mailScannerFields, ',')) as $field) {
181
				$field = explode('|', $field);
182
				if (($searchModuleName && $searchModuleName !== $field[1]) || 4 !== (int) $field[3]) {
183
					continue;
184
				}
185
				$fields[$field[1]][$field[3]][] = $field[2];
186
			}
187
		}
188
		$this->numberFieldsCache[$cacheKey] = $fields;
189
		return $fields;
190
	}
191
192
	/** {@inheritdoc} */
193
	public function getExceptions(): array
194
	{
195
		return [];
196
	}
197
}
198