Find::Handle()   F
last analyzed

Complexity

Conditions 37
Paths > 20000

Size

Total Lines 200
Code Lines 126

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 37
eloc 126
c 3
b 0
f 0
nc 44661
nop 1
dl 0
loc 200
rs 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
/*
4
 * SPDX-License-Identifier: AGPL-3.0-only
5
 * SPDX-FileCopyrightText: Copyright 2022-2024 grommunio GmbH
6
 *
7
 * Provides the FIND command
8
 */
9
10
class Find extends RequestProcessor {
11
	/**
12
	 * Handles the Find command.
13
	 *
14
	 * @param int $commandCode
15
	 *
16
	 * @return bool
17
	 */
18
	public function Handle($commandCode) {
19
		$cpo = new ContentParameters();
20
		if (!self::$decoder->getElementStartTag(SYNC_FIND_FIND)) {
21
			return false;
22
		}
23
24
		if (!self::$decoder->getElementStartTag(SYNC_FIND_SEARCHID)) {
25
			return false;
26
		}
27
		$searchId = self::$decoder->getElementContent();
28
		$cpo->SetFindSearchId($searchId);
0 ignored issues
show
Bug introduced by
The method SetFindSearchId() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

28
		$cpo->/** @scrutinizer ignore-call */ 
29
        SetFindSearchId($searchId);
Loading history...
29
		if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_SEARCHID
30
			return false;
31
		}
32
33
		if (!self::$decoder->getElementStartTag(SYNC_FIND_EXECUTESEARCH)) {
34
			return false;
35
		}
36
37
		if (self::$decoder->getElementStartTag(SYNC_FIND_MAILBOXSEARCHCRITERION)) {
38
			$searchname = ISearchProvider::SEARCH_MAILBOX;
39
			if (!self::$decoder->getElementStartTag(SYNC_FIND_QUERY)) {
40
				return false;
41
			}
42
43
			if (self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) {
44
				$folderType = self::$decoder->getElementContent();
45
				$cpo->SetFindFolderType($folderType);
0 ignored issues
show
Bug introduced by
The method SetFindFolderType() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

45
				$cpo->/** @scrutinizer ignore-call */ 
46
          SetFindFolderType($folderType);
Loading history...
46
				if (!self::$decoder->getElementEndTag()) { // SYNC_FOLDERTYPE
47
					return false;
48
				}
49
			}
50
51
			if (self::$decoder->getElementStartTag(SYNC_FOLDERID)) {
52
				$folderId = self::$decoder->getElementContent();
53
				$cpo->SetFindFolderId($folderId);
0 ignored issues
show
Bug introduced by
The method SetFindFolderId() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

53
				$cpo->/** @scrutinizer ignore-call */ 
54
          SetFindFolderId($folderId);
Loading history...
54
				$cpo->SetRawFindFolderId($folderId);
0 ignored issues
show
Bug introduced by
The method SetRawFindFolderId() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

54
				$cpo->/** @scrutinizer ignore-call */ 
55
          SetRawFindFolderId($folderId);
Loading history...
55
				if (!self::$decoder->getElementEndTag()) { // SYNC_FOLDERID
56
					return false;
57
				}
58
			}
59
60
			if (self::$decoder->getElementStartTag(SYNC_FIND_FREETEXT)) {
61
				$freeText = self::$decoder->getElementContent();
62
				$cpo->SetFindFreeText($freeText);
0 ignored issues
show
Bug introduced by
The method SetFindFreeText() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

62
				$cpo->/** @scrutinizer ignore-call */ 
63
          SetFindFreeText($freeText);
Loading history...
63
				if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_FREETEXT
64
					return false;
65
				}
66
			}
67
68
			if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_QUERY
69
				return false;
70
			}
71
72
			$deeptraversal = false;
73
			if (self::$decoder->getElementStartTag(SYNC_FIND_OPTIONS)) {
74
				WBXMLDecoder::ResetInWhile("findOptions");
75
				while (WBXMLDecoder::InWhile("findOptions")) {
76
					if (self::$decoder->getElementStartTag(SYNC_FIND_RANGE)) {
77
						$findrange = self::$decoder->getElementContent();
78
						$cpo->SetFindRange($findrange);
0 ignored issues
show
Bug introduced by
The method SetFindRange() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

78
						$cpo->/** @scrutinizer ignore-call */ 
79
            SetFindRange($findrange);
Loading history...
79
						if (!self::$decoder->getElementEndTag()) {
80
							return false;
81
						}
82
					}
83
84
					if (self::$decoder->getElementStartTag(SYNC_FIND_DEEPTRAVERSAL)) {
85
						$deeptraversal = true;
86
						if (($dam = self::$decoder->getElementContent()) !== false) {
0 ignored issues
show
Unused Code introduced by
The assignment to $dam is dead and can be removed.
Loading history...
87
							$deeptraversal = true;
88
							if (!self::$decoder->getElementEndTag()) {
89
								return false;
90
							}
91
						}
92
					}
93
					$e = self::$decoder->peek();
94
					if ($e[EN_TYPE] == EN_TYPE_ENDTAG) {
95
						self::$decoder->getElementEndTag();
96
97
						break;
98
					}
99
				}
100
			}
101
			$cpo->SetFindDeepTraversal($deeptraversal);
0 ignored issues
show
Bug introduced by
The method SetFindDeepTraversal() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

101
			$cpo->/** @scrutinizer ignore-call */ 
102
         SetFindDeepTraversal($deeptraversal);
Loading history...
102
103
			if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_MAILBOXSEARCHCRITERION
104
				return false;
105
			}
106
		}
107
108
		if (self::$decoder->getElementStartTag(SYNC_FIND_GALSEARCHCRITERION)) {
109
			$searchname = ISearchProvider::SEARCH_GAL;
110
			$galSearchCriterion = self::$decoder->getElementContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $galSearchCriterion is dead and can be removed.
Loading history...
111
			if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_GALSEARCHCRITERION
112
				return false;
113
			}
114
		}
115
116
		if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_EXECUTESEARCH
117
			return false;
118
		}
119
120
		if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_FIND
121
			return false;
122
		}
123
124
		// get SearchProvider
125
		$searchprovider = GSync::GetBackend()->GetSearchProvider();
126
		$findstatus = SYNC_FINDSTATUS_SUCCESS;
127
128
		if (!isset($searchname)) {
129
			$findstatus = SYNC_FINDSTATUS_INVALIDREQUEST;
130
		}
131
132
		self::$encoder->startWBXML();
133
		self::$encoder->startTag(SYNC_FIND_FIND);
134
135
		self::$encoder->startTag(SYNC_FIND_STATUS);
136
		self::$encoder->content($findstatus);
137
		self::$encoder->endTag();
138
139
		if ($findstatus == SYNC_FINDSTATUS_SUCCESS) {
140
			$status = SYNC_FINDSTATUS_SUCCESS;
141
142
			try {
143
				if ($searchname == ISearchProvider::SEARCH_GAL) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $searchname does not seem to be defined for all execution paths leading up to this point.
Loading history...
144
					// TODO: Fix GAL search in Find command
145
					// $rows = $searchprovider->GetGALSearchResults($cpo->GetFindFreeText(), "0-100", null);
146
					throw new StatusException("GAL search in FIND command is not implemented. Please report this including the 'WBXML debug data' logged. Be aware that the debug data could contain confidential information.", SYNC_FINDSTATUS_INVALIDREQUEST, null, LOGLEVEL_FATAL);
147
				}
148
				if ($searchname == ISearchProvider::SEARCH_MAILBOX) {
149
					$backendFolderId = self::$deviceManager->GetBackendIdForFolderId($cpo->GetFindFolderid());
0 ignored issues
show
Bug introduced by
The method GetFindFolderid() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

149
					$backendFolderId = self::$deviceManager->GetBackendIdForFolderId($cpo->/** @scrutinizer ignore-call */ GetFindFolderid());
Loading history...
150
					$cpo->SetFindFolderid($backendFolderId);
0 ignored issues
show
Bug introduced by
The method SetFindFolderid() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

150
					$cpo->/** @scrutinizer ignore-call */ 
151
           SetFindFolderid($backendFolderId);
Loading history...
151
					$rows = $searchprovider->GetMailboxSearchResults($cpo);
152
				}
153
			}
154
			catch (StatusException $stex) {
155
				$status = $stex->getCode();
156
			}
157
158
			self::$encoder->startTag(SYNC_FIND_RESPONSE);
159
160
			self::$encoder->startTag(SYNC_ITEMOPERATIONS_STORE);
161
			self::$encoder->content("Mailbox");
162
			self::$encoder->endTag();
163
164
			self::$encoder->startTag(SYNC_FIND_STATUS);
165
			self::$encoder->content($status);
166
			self::$encoder->endTag();
167
168
			if (isset($rows['range'])) {
169
				$searchrange = $rows['range'];
0 ignored issues
show
Unused Code introduced by
The assignment to $searchrange is dead and can be removed.
Loading history...
170
				unset($rows['range']);
171
			}
172
			if (isset($rows['searchtotal'])) {
173
				$searchtotal = $rows['searchtotal'];
174
				unset($rows['searchtotal']);
175
			}
176
177
			if ($searchtotal > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $searchtotal does not seem to be defined for all execution paths leading up to this point.
Loading history...
178
				foreach ($rows as $u) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rows does not seem to be defined for all execution paths leading up to this point.
Loading history...
179
					// fetch the SyncObject for this result
180
					$message = self::$backend->Fetch(false, $u['longid'], $cpo);
181
					$mfolderid = self::$deviceManager->GetFolderIdForBackendId(bin2hex((string) $message->ParentSourceKey));
182
183
					self::$encoder->startTag(SYNC_FIND_RESULT);
184
					self::$encoder->startTag(SYNC_FOLDERTYPE);
185
					self::$encoder->content($u['class']);
186
					self::$encoder->endTag();
187
188
					self::$encoder->startTag(SYNC_SERVERENTRYID);
189
					self::$encoder->content($mfolderid . ":" . $u['serverid']);
190
					self::$encoder->endTag();
191
					self::$encoder->startTag(SYNC_FOLDERID);
192
					self::$encoder->content($cpo->GetRawFindFolderId());
0 ignored issues
show
Bug introduced by
The method GetRawFindFolderId() does not exist on ContentParameters. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

192
					self::$encoder->content($cpo->/** @scrutinizer ignore-call */ GetRawFindFolderId());
Loading history...
193
					self::$encoder->endTag();
194
195
					self::$encoder->startTag(SYNC_FIND_PROPERTIES);
196
					$fpmessage = SyncFindProperties::GetObjectFromSyncMail($message);
197
					$fpmessage->Encode(self::$encoder);
198
199
					self::$encoder->endTag(); // properties
200
					self::$encoder->endTag(); // result
201
				}
202
			}
203
204
			self::$encoder->startTag(SYNC_FIND_RANGE);
205
			self::$encoder->content("0-" . $searchtotal);
206
			self::$encoder->endTag();
207
			if ($searchtotal > 0) {
208
				self::$encoder->startTag(SYNC_FIND_TOTAL);
209
				self::$encoder->content($searchtotal); // $searchtotal);
210
				self::$encoder->endTag();
211
			}
212
213
			self::$encoder->endTag(); // SYNC_FIND_RESPONSE
214
		}
215
		self::$encoder->endTag(); // SYNC_FIND_FIND
216
217
		return true;
218
	}
219
}
220