SendMail::Handle()   F
last analyzed

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 115
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 62
nc 46290
nop 1
dl 0
loc 115
rs 0
c 0
b 0
f 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 2007-2013,2016 Zarafa Deutschland GmbH
5
 * SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH
6
 *
7
 * Provides the SENDMAIL, SMARTREPLY and SMARTFORWARD command
8
 */
9
10
class SendMail extends RequestProcessor {
11
	/**
12
	 * Handles the SendMail, SmartReply and SmartForward command.
13
	 *
14
	 * @param int $commandCode
15
	 *
16
	 * @return bool
17
	 */
18
	public function Handle($commandCode) {
19
		$sm = new SyncSendMail();
20
21
		$reply = $forward = $parent = $sendmail = $smartreply = $smartforward = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $parent is dead and can be removed.
Loading history...
22
		if (Request::GetGETCollectionId()) {
23
			$parent = Request::GetGETCollectionId();
24
		}
25
		if ($commandCode == GSync::COMMAND_SMARTFORWARD) {
26
			$forward = Request::GetGETItemId();
27
		}
28
		elseif ($commandCode == GSync::COMMAND_SMARTREPLY) {
29
			$reply = Request::GetGETItemId();
30
		}
31
32
		if (self::$decoder->IsWBXML()) {
33
			$el = self::$decoder->getElement();
34
35
			if ($el[EN_TYPE] != EN_TYPE_STARTTAG) {
36
				return false;
37
			}
38
39
			if ($el[EN_TAG] == SYNC_COMPOSEMAIL_SENDMAIL) {
40
				$sendmail = true;
41
			}
42
			elseif ($el[EN_TAG] == SYNC_COMPOSEMAIL_SMARTREPLY) {
43
				$smartreply = true;
44
			}
45
			elseif ($el[EN_TAG] == SYNC_COMPOSEMAIL_SMARTFORWARD) {
46
				$smartforward = true;
47
			}
48
49
			if (!$sendmail && !$smartreply && !$smartforward) {
50
				return false;
51
			}
52
53
			$sm->Decode(self::$decoder);
54
		}
55
		else {
56
			$sm->mime = self::$decoder->GetPlainInputStream();
57
			// no wbxml output is provided, only a http OK
58
			$sm->saveinsent = Request::GetGETSaveInSent();
59
		}
60
61
		// Check if it is a reply or forward. Two cases are possible:
62
		// 1. Either $smartreply or $smartforward are set after reading WBXML
63
		// 2. Either $reply or $forward are set after getting the request parameters
64
		if ($reply || $smartreply || $forward || $smartforward) {
65
			// If the mobile sends an email in WBXML data the variables below
66
			// should be set. If it is a RFC822 message, get the reply/forward message id
67
			// from the request as they are always available there
68
			if (!isset($sm->source)) {
69
				$sm->source = new SyncSendMailSource();
70
			}
71
			if (!isset($sm->source->itemid)) {
72
				$sm->source->itemid = Request::GetGETItemId();
73
			}
74
			if (!isset($sm->source->folderid)) {
75
				$sm->source->folderid = Request::GetGETCollectionId();
76
			}
77
78
			// split long-id if it's set - it overwrites folderid and itemid
79
			if (isset($sm->source->longid) && $sm->source->longid) {
80
				list($sm->source->folderid, $sm->source->itemid) = Utils::SplitMessageId($sm->source->longid);
81
			}
82
83
			// Rewrite the AS folderid into a backend folderid
84
			if (isset($sm->source->folderid)) {
85
				$sm->source->folderid = self::$deviceManager->GetBackendIdForFolderId($sm->source->folderid);
86
			}
87
			if (isset($sm->source->itemid)) {
88
				list(, $sk) = Utils::SplitMessageId($sm->source->itemid);
89
				$sm->source->itemid = $sk;
90
			}
91
			// replyflag and forward flags are actually only for the correct icon.
92
			// Even if they are a part of SyncSendMail object, they won't be streamed.
93
			if ($smartreply || $reply) {
94
				$sm->replyflag = true;
95
			}
96
			else {
97
				$sm->forwardflag = true;
98
			}
99
100
			if (!isset($sm->source->folderid) || !$sm->source->folderid) {
101
				SLog::Write(LOGLEVEL_ERROR, sprintf("SendMail(): No parent folder id while replying or forwarding message:'%s'", ($reply) ? $reply : $forward));
102
			}
103
		}
104
105
		self::$topCollector->AnnounceInformation(sprintf("SendMail(): Sending email with %d bytes", strlen($sm->mime)), true);
106
107
		$statusMessage = '';
108
109
		try {
110
			$status = self::$backend->SendMail($sm);
111
		}
112
		catch (StatusException $se) {
113
			$status = $se->getCode();
114
			$statusMessage = $se->getMessage();
115
		}
116
117
		if ($status != SYNC_COMMONSTATUS_SUCCESS) {
118
			if (self::$decoder->IsWBXML()) {
119
				// TODO check no WBXML on SmartReply and SmartForward
120
				self::$encoder->StartWBXML();
121
				self::$encoder->startTag(SYNC_COMPOSEMAIL_SENDMAIL);
122
				self::$encoder->startTag(SYNC_COMPOSEMAIL_STATUS);
123
				self::$encoder->content($status); // TODO return the correct status
124
				self::$encoder->endTag();
125
				self::$encoder->endTag();
126
			}
127
			else {
128
				throw new HTTPReturnCodeException($statusMessage, HTTP_CODE_500, null, LOGLEVEL_WARN);
129
			}
130
		}
131
132
		return $status;
133
	}
134
}
135