GrommunioIMipPlugin::schedule()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 34
c 0
b 0
f 0
nop 1
dl 0
loc 55
rs 7.6666
nc 15

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 2016 - 2018 Kopano b.v.
6
 * SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH
7
 *
8
 * Sends meeting invitations.
9
 */
10
11
namespace grommunio\DAV;
12
13
use Sabre\CalDAV\Schedule\IMipPlugin;
14
use Sabre\VObject\ITip\Message;
15
16
class GrommunioIMipPlugin extends IMipPlugin {
17
	private $logger;
18
	protected $gDavBackend;
19
20
	/**
21
	 * Constructor.
22
	 */
23
	public function __construct(GrommunioDavBackend $gDavBackend, GLogger $glogger) {
24
		$this->gDavBackend = $gDavBackend;
25
		$this->logger = $glogger;
26
	}
27
28
	/**
29
	 * Sends out meeting invitation.
30
	 *
31
	 * Using the information in iTipMessage to send out a meeting
32
	 * invitation.
33
	 */
34
	public function schedule(Message $iTipMessage) {
35
		$this->logger->trace("method: %s - recipient: %s - significantChange: %d - scheduleStatus: %s - message: %s", $iTipMessage->method, $iTipMessage->recipient, $iTipMessage->significantChange, $iTipMessage->scheduleStatus, $iTipMessage->message->serialize());
36
37
		if (!$iTipMessage->significantChange) {
38
			if (!$iTipMessage->scheduleStatus) {
39
				$iTipMessage->scheduleStatus = "1.0;We got the message, but it's not significant enough to warrant an email";
40
			}
41
42
			return;
43
		}
44
45
		$recipient = preg_replace('!^mailto:!i', '', $iTipMessage->recipient);
46
		$session = $this->gDavBackend->GetSession();
47
		$addrbook = $this->gDavBackend->GetAddressBook();
48
		$store = $this->gDavBackend->GetStore($this->gDavBackend->GetUser());
49
		$storeprops = mapi_getprops($store, [PR_IPM_OUTBOX_ENTRYID, PR_IPM_SENTMAIL_ENTRYID]);
0 ignored issues
show
Bug introduced by
The constant grommunio\DAV\PR_IPM_SENTMAIL_ENTRYID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant grommunio\DAV\PR_IPM_OUTBOX_ENTRYID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
50
		if (!isset($storeprops[PR_IPM_OUTBOX_ENTRYID]) || !isset($storeprops[PR_IPM_SENTMAIL_ENTRYID])) {
51
			/* handle error */
52
			$this->logger->error("no outbox found aborting user: %s", $this->gDavBackend->GetUser());
53
54
			return;
55
		}
56
57
		/* create message and convert */
58
		$outbox = mapi_msgstore_openentry($store, $storeprops[PR_IPM_OUTBOX_ENTRYID]);
59
		$newmessage = mapi_folder_createmessage($outbox);
60
		mapi_icaltomapi($session, $store, $addrbook, $newmessage, $iTipMessage->message->serialize(), false);
0 ignored issues
show
Bug introduced by
The function mapi_icaltomapi was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

60
		/** @scrutinizer ignore-call */ 
61
  mapi_icaltomapi($session, $store, $addrbook, $newmessage, $iTipMessage->message->serialize(), false);
Loading history...
61
		mapi_setprops($newmessage, [PR_SENTMAIL_ENTRYID => $storeprops[PR_IPM_SENTMAIL_ENTRYID], PR_DELETE_AFTER_SUBMIT => false]);
0 ignored issues
show
Bug introduced by
The constant grommunio\DAV\PR_SENTMAIL_ENTRYID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant grommunio\DAV\PR_DELETE_AFTER_SUBMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
62
63
		/* clean the recipients (needed since mapi_icaltomapi does not take IC2M_NO_ORGANIZER) */
64
		$recipientTable = mapi_message_getrecipienttable($newmessage);
65
		$recipientRows = mapi_table_queryallrows($recipientTable, [PR_SMTP_ADDRESS, PR_ROWID]);
0 ignored issues
show
Bug introduced by
The constant grommunio\DAV\PR_SMTP_ADDRESS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant grommunio\DAV\PR_ROWID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
		$removeRecipients = [];
67
		foreach ($recipientRows as $key => $recip) {
68
			if (!isset($recip[PR_SMTP_ADDRESS])) {
69
				continue;
70
			}
71
			if (strcasecmp($recip[PR_SMTP_ADDRESS], $recipient) != 0) {
72
				$removeRecipients[] = $recip;
73
			}
74
		}
75
		if (count($removeRecipients) == count($recipientRows)) {
76
			$this->logger->error("message will have no recipients. List to remove: %s - recipientRows: %s", $removeRecipients, $recipientRows);
77
78
			return;
79
		}
80
		if (count($removeRecipients) > 0) {
81
			mapi_message_modifyrecipients($newmessage, MODRECIP_REMOVE, $removeRecipients);
0 ignored issues
show
Bug introduced by
The constant grommunio\DAV\MODRECIP_REMOVE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
82
		}
83
84
		/* save message and send */
85
		mapi_savechanges($newmessage);
86
		mapi_message_submitmessage($newmessage);
87
		$this->logger->info("email sent, recipient: %s", $recipient);
88
		$iTipMessage->scheduleStatus = '1.1;Scheduling message sent via iMip';
89
	}
90
}
91