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
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2022-2024 grommunio GmbH
5
 *
6
 * Provides the FIND command
7
 */
8
9
class Find extends RequestProcessor {
10
	/**
11
	 * Handles the Find command.
12
	 *
13
	 * @param int $commandCode
14
	 *
15
	 * @return bool
16
	 */
17
	public function Handle($commandCode) {
18
		$cpo = new ContentParameters();
19
		if (!self::$decoder->getElementStartTag(SYNC_FIND_FIND)) {
20
			return false;
21
		}
22
23
		if (!self::$decoder->getElementStartTag(SYNC_FIND_SEARCHID)) {
24
			return false;
25
		}
26
		$searchId = self::$decoder->getElementContent();
27
		$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

27
		$cpo->/** @scrutinizer ignore-call */ 
28
        SetFindSearchId($searchId);
Loading history...
28
		if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_SEARCHID
29
			return false;
30
		}
31
32
		if (!self::$decoder->getElementStartTag(SYNC_FIND_EXECUTESEARCH)) {
33
			return false;
34
		}
35
36
		if (self::$decoder->getElementStartTag(SYNC_FIND_MAILBOXSEARCHCRITERION)) {
37
			$searchname = ISearchProvider::SEARCH_MAILBOX;
38
			if (!self::$decoder->getElementStartTag(SYNC_FIND_QUERY)) {
39
				return false;
40
			}
41
42
			if (self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) {
43
				$folderType = self::$decoder->getElementContent();
44
				$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

44
				$cpo->/** @scrutinizer ignore-call */ 
45
          SetFindFolderType($folderType);
Loading history...
45
				if (!self::$decoder->getElementEndTag()) { // SYNC_FOLDERTYPE
46
					return false;
47
				}
48
			}
49
50
			if (self::$decoder->getElementStartTag(SYNC_FOLDERID)) {
51
				$folderId = self::$decoder->getElementContent();
52
				$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

52
				$cpo->/** @scrutinizer ignore-call */ 
53
          SetFindFolderId($folderId);
Loading history...
53
				$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

53
				$cpo->/** @scrutinizer ignore-call */ 
54
          SetRawFindFolderId($folderId);
Loading history...
54
				if (!self::$decoder->getElementEndTag()) { // SYNC_FOLDERID
55
					return false;
56
				}
57
			}
58
59
			if (self::$decoder->getElementStartTag(SYNC_FIND_FREETEXT)) {
60
				$freeText = self::$decoder->getElementContent();
61
				$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

61
				$cpo->/** @scrutinizer ignore-call */ 
62
          SetFindFreeText($freeText);
Loading history...
62
				if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_FREETEXT
63
					return false;
64
				}
65
			}
66
67
			if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_QUERY
68
				return false;
69
			}
70
71
			$deeptraversal = false;
72
			if (self::$decoder->getElementStartTag(SYNC_FIND_OPTIONS)) {
73
				WBXMLDecoder::ResetInWhile("findOptions");
74
				while (WBXMLDecoder::InWhile("findOptions")) {
75
					if (self::$decoder->getElementStartTag(SYNC_FIND_RANGE)) {
76
						$findrange = self::$decoder->getElementContent();
77
						$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

77
						$cpo->/** @scrutinizer ignore-call */ 
78
            SetFindRange($findrange);
Loading history...
78
						if (!self::$decoder->getElementEndTag()) {
79
							return false;
80
						}
81
					}
82
83
					if (self::$decoder->getElementStartTag(SYNC_FIND_DEEPTRAVERSAL)) {
84
						$deeptraversal = true;
85
						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...
86
							$deeptraversal = true;
87
							if (!self::$decoder->getElementEndTag()) {
88
								return false;
89
							}
90
						}
91
					}
92
					$e = self::$decoder->peek();
93
					if ($e[EN_TYPE] == EN_TYPE_ENDTAG) {
94
						self::$decoder->getElementEndTag();
95
96
						break;
97
					}
98
				}
99
			}
100
			$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

100
			$cpo->/** @scrutinizer ignore-call */ 
101
         SetFindDeepTraversal($deeptraversal);
Loading history...
101
102
			if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_MAILBOXSEARCHCRITERION
103
				return false;
104
			}
105
		}
106
107
		if (self::$decoder->getElementStartTag(SYNC_FIND_GALSEARCHCRITERION)) {
108
			$searchname = ISearchProvider::SEARCH_GAL;
109
			$galSearchCriterion = self::$decoder->getElementContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $galSearchCriterion is dead and can be removed.
Loading history...
110
			if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_GALSEARCHCRITERION
111
				return false;
112
			}
113
		}
114
115
		if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_EXECUTESEARCH
116
			return false;
117
		}
118
119
		if (!self::$decoder->getElementEndTag()) { // SYNC_FIND_FIND
120
			return false;
121
		}
122
123
		// get SearchProvider
124
		$searchprovider = GSync::GetBackend()->GetSearchProvider();
125
		$findstatus = SYNC_FINDSTATUS_SUCCESS;
126
127
		if (!isset($searchname)) {
128
			$findstatus = SYNC_FINDSTATUS_INVALIDREQUEST;
129
		}
130
131
		self::$encoder->startWBXML();
132
		self::$encoder->startTag(SYNC_FIND_FIND);
133
134
		self::$encoder->startTag(SYNC_FIND_STATUS);
135
		self::$encoder->content($findstatus);
136
		self::$encoder->endTag();
137
138
		if ($findstatus == SYNC_FINDSTATUS_SUCCESS) {
139
			$status = SYNC_FINDSTATUS_SUCCESS;
140
141
			try {
142
				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...
143
					// TODO: Fix GAL search in Find command
144
					// $rows = $searchprovider->GetGALSearchResults($cpo->GetFindFreeText(), "0-100", null);
145
					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);
146
				}
147
				if ($searchname == ISearchProvider::SEARCH_MAILBOX) {
148
					$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

148
					$backendFolderId = self::$deviceManager->GetBackendIdForFolderId($cpo->/** @scrutinizer ignore-call */ GetFindFolderid());
Loading history...
149
					$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

149
					$cpo->/** @scrutinizer ignore-call */ 
150
           SetFindFolderid($backendFolderId);
Loading history...
150
					$rows = $searchprovider->GetMailboxSearchResults($cpo);
151
				}
152
			}
153
			catch (StatusException $stex) {
154
				$status = $stex->getCode();
155
			}
156
157
			self::$encoder->startTag(SYNC_FIND_RESPONSE);
158
159
			self::$encoder->startTag(SYNC_ITEMOPERATIONS_STORE);
160
			self::$encoder->content("Mailbox");
161
			self::$encoder->endTag();
162
163
			self::$encoder->startTag(SYNC_FIND_STATUS);
164
			self::$encoder->content($status);
165
			self::$encoder->endTag();
166
167
			if (isset($rows['range'])) {
168
				$searchrange = $rows['range'];
0 ignored issues
show
Unused Code introduced by
The assignment to $searchrange is dead and can be removed.
Loading history...
169
				unset($rows['range']);
170
			}
171
			if (isset($rows['searchtotal'])) {
172
				$searchtotal = $rows['searchtotal'];
173
				unset($rows['searchtotal']);
174
			}
175
176
			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...
177
				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...
178
					// fetch the SyncObject for this result
179
					$message = self::$backend->Fetch(false, $u['longid'], $cpo);
180
					$mfolderid = self::$deviceManager->GetFolderIdForBackendId(bin2hex($message->ParentSourceKey));
181
182
					self::$encoder->startTag(SYNC_FIND_RESULT);
183
					self::$encoder->startTag(SYNC_FOLDERTYPE);
184
					self::$encoder->content($u['class']);
185
					self::$encoder->endTag();
186
187
					self::$encoder->startTag(SYNC_SERVERENTRYID);
188
					self::$encoder->content($mfolderid . ":" . $u['serverid']);
189
					self::$encoder->endTag();
190
					self::$encoder->startTag(SYNC_FOLDERID);
191
					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

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