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